Apache HttpClient 如何进行https连接
来源:互联网 发布:淘宝助理文件夹 编辑:程序博客网 时间:2024/04/28 09:41
HttpGet httpGet = new HttpGet("https://www.signpost.com/login");HttpClient client = new DefaultHttpClient();HttpResponse response = client.execute(httpGet);HttpEntity entity = response.getEntity();在HttpClient,如果直接使用HttpGet 和DefaultHttpClient进行连接会报如下异常javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated
这是因为HttpClient 进行https连接时需要一个合法的SSL Certificate
解决办法(http://javaskeleton.blogspot.com/2010/07/avoiding-peer-not-authenticated-with.html)1. 创建一个新的自定义的信任管理:TrustManager,从而所有的Certificate
HttpClient client = new DefaultHttpClient();X509TrustManager tm = new X509TrustManager() {//在原始的TrustManger中,如果certificate是非法,则会抛出CertificateException //这里,无论是合法还是非法的,都不抛异常,跳过检查public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {} public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {} public X509Certificate[] getAcceptedIssuers() {return null;}};2. 有了新的TrustManger后,需要添加到client中。 这里需要SSL sockets,这是HttpClient进行Https连接时,使用的Socket。
SSLContext ctx = SSLContext.getInstance("TLS");ctx.init(null, new TrustManager[]{tm}, null);SSLSocketFactory ssf = new SSLSocketFactory(ctx);//这里需要忽略掉HostName的比较,否则访问一些网站时,会报异常ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);3. 利用新得到的SSLSocket,创建一个新的能进行Https访问的client
ClientConnectionManager ccm = client.getConnectionManager();SchemeRegistry sr = ccm.getSchemeRegistry();sr.register(new Scheme("https", ssf, 443));client = new DefaultHttpClient(ccm, client.getParams());
测试:import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;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 org.apache.http.HttpEntity;import org.apache.http.HttpHost;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.conn.ClientConnectionManager;import org.apache.http.conn.params.ConnRoutePNames;import org.apache.http.conn.scheme.Scheme;import org.apache.http.conn.scheme.SchemeRegistry;import org.apache.http.conn.ssl.SSLSocketFactory;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.params.CoreConnectionPNames;public class HttpsClient {private static X509TrustManager tm = new X509TrustManager() {public void checkClientTrusted(X509Certificate[] xcs, String string)throws CertificateException {}public void checkServerTrusted(X509Certificate[] xcs, String string)throws CertificateException {}public X509Certificate[] getAcceptedIssuers() {return null;}};@SuppressWarnings("deprecation")public static HttpClient getInstance() throws KeyManagementException,NoSuchAlgorithmException {HttpClient client = new DefaultHttpClient();SSLContext ctx = SSLContext.getInstance("TLS");ctx.init(null, new TrustManager[] { tm }, null);SSLSocketFactory ssf = new SSLSocketFactory(ctx);ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);ClientConnectionManager ccm = client.getConnectionManager();SchemeRegistry sr = ccm.getSchemeRegistry();sr.register(new Scheme("https", ssf, 443));client = new DefaultHttpClient(ccm, client.getParams());return client;}public static void main(String[] args) throws KeyManagementException,NoSuchAlgorithmException, IllegalStateException, IOException {HttpClient httpsClient = HttpsClient.getInstance();HttpHost proxy = new HttpHost("172.28.8.246", 8080);httpsClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);httpsClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,20000);HttpGet httpGet = new HttpGet("https://www.signpost.com/login");HttpResponse response = httpsClient.execute(httpGet);HttpEntity entity = response.getEntity();BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));StringBuffer content = new StringBuffer();for (String line; (line = br.readLine()) != null;) {content.append(line + "\r\n");}System.err.println(content.toString());}}
最后,注意一下导入的包,别混淆了
- Apache HttpClient 如何进行https连接
- httpClient 连接 https + 证书连接
- httpclient来进行https访问
- 使用apache的httpcomponents中的httpclient 4.5对https进行访问
- android如何使用bks和apache服务器进行https通信
- java使用Apache HttpClient发送https请求创建SSL连接方法实现
- apache配置https连接
- 使用httpclient来进行https访问
- HttpClient进行POST请求(HTTPS方式)
- JAVA利用HttpClient进行POST请求(HTTPS)
- JAVA利用HttpClient进行HTTPS接口调用
- JAVA利用HttpClient进行HTTPS接口调用
- JAVA利用HttpClient进行HTTPS接口调用
- apache commons httpClient 保持session连接
- apache httpclient 连接 IIB,发送XML请求
- HTTPS HTTPClient
- httpclient https
- Andorid 中使用Https/SSL Apache HttpClient(DefaultHttpClient)
- 如何在不改变图像质量的情况下压缩图像的文件大小
- What is the best free memory leak detector for a C/C++ program and its plug-in DLLs
- Oracle 分析函数的使用
- 删除单链表中指定的节点
- ServiceTest
- Apache HttpClient 如何进行https连接
- 算法之美
- 民营企业IT项目管理之路
- connection closed gracefully问题
- 【Linux】 多线程编程(信号量实现同步)
- 修改MBR,解决安装Windows时Error 17错误。
- Windows Phone 7 网络编程之webclient和httpwebrequest的使用
- 程序员技术练级攻略
- sleep()和wait()的区别