下載函式庫: 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 |
|
描述 |
HttpClientServlet.java |
文件大小 |
4 Kbytes |
下载次数: |
0 次 |
阅读(2502) | 评论(0) | 转发(0) |