Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5367902
  • 博文数量: 893
  • 博客积分: 17977
  • 博客等级: 上将
  • 技术积分: 8671
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-26 09:59
个人简介

一个好老好老的老程序员了。

文章分类

全部博文(893)

文章存档

2020年(10)

2019年(40)

2018年(88)

2017年(130)

2015年(5)

2014年(12)

2013年(41)

2012年(36)

2011年(272)

2010年(1)

2009年(53)

2008年(65)

2007年(47)

2006年(81)

2005年(12)

分类: Java

2011-05-31 18:05:58

下載函式庫: HttpClient 4.1.1 (GA)


預先安裝函式庫:commons-logging-1.1.1.jar, httpcore-4.1.jar, httpclient-4.1.1.jar

這裡我以Servlet 3.0標準來當做練習,請在Tomcat 7.0以上版本執行:
HttpClientServlet.java:
package com.andowson.httpclient; import java.io.IOException; import java.io.PrintWriter; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import javax.servlet.Servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.http.HttpEntity; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpGet; import org.apache.http.conn.params.ConnRoutePNames; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; /** * Servlet implementation class HttpClientServlet */ @WebServlet("/HttpClientServlet") public class HttpClientServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public HttpClientServlet() { super(); } /** * @see Servlet#getServletInfo() */ public String getServletInfo() { return "HttpClient Servlet"; } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String url = request.getParameter("url"); if (url == null) { url = ""; } DefaultHttpClient httpclient = new DefaultHttpClient(); HttpHost proxy = new HttpHost("10.160.3.88", 8080); httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); try { TrustManager easyTrustManager = new X509TrustManager() { @Override public void checkClientTrusted( X509Certificate[] chain, String authType) throws CertificateException { // Oh, I am easy! } @Override public void checkServerTrusted( X509Certificate[] chain, String authType) throws CertificateException { // Oh, I am easy! } @Override public X509Certificate[] getAcceptedIssuers() { return null; } }; SSLContext sslcontext = SSLContext.getInstance("TLS"); sslcontext.init(null, new TrustManager[] { easyTrustManager }, null); SSLSocketFactory socketFactory = new SSLSocketFactory(sslcontext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); Scheme sch = new Scheme("https", 443, socketFactory); httpclient.getConnectionManager().getSchemeRegistry().register(sch); HttpGet httpget = new HttpGet(url); System.out.println("executing request " + httpget.getRequestLine()); HttpResponse resp = httpclient.execute(httpget); HttpEntity entity = resp.getEntity(); System.out.println("----------------------------------------"); System.out.println(resp.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } EntityUtils.consume(entity); response.setContentType(entity.getContentType().toString()); PrintWriter out = response.getWriter(); ResponseHandler responseHandler = new BasicResponseHandler(); String responseBody = httpclient.execute(httpget, responseHandler); System.out.println("----------------------------------------"); System.out.println(responseBody); out.println(responseBody); System.out.println("----------------------------------------"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (KeyManagementException e) { e.printStackTrace(); } finally { httpclient.getConnectionManager().shutdown(); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }

修改web.xml,加入下列項目:
HttpClientServlet com.andowson.httpclient.HttpClientServlet HttpClientServlet /servlet/httpclient

說明:
63-64行是如果伺服器需要透過Proxy才能連到Internet(例如公司內部網站)執行時需透過Proxy的設定方式,如果伺服器可以直接連接到Internet時這兩行需要mark為註解掉。

參考資料:
 文件名称 HttpClientServlet.java [Disk]
 描述 HttpClientServlet.java
 文件大小 4 Kbytes
 下载次数:  0 次
阅读(1414) | 评论(0) | 转发(0) |
0

上一篇:basp21

下一篇:basp21下载.

给主人留下些什么吧!~~