httpclient https
发布日期:2021-09-21 12:44:34
浏览次数:38
分类:技术文章
本文共 3726 字,大约阅读时间需要 12 分钟。
两种方式 1.添加网站证书到密钥库 导入网站证书,并放入jdk1.7.0_51\jre\lib\security目录中 进入安装java所在目录,我的是如下的路径 C:\Program Files\Java\jdk1.7.0_51\jre\lib\security> 执行C:\Program Files\Java\jdk1.7.0_51\jre\lib\security>keytool -import -file zhaopin.cer -keystore cacerts -alias 别名 如果需要输入密码,默认为changeit 2.java类信任所有证书import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketAddress; import java.net.UnknownHostException; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import javax.net.SocketFactory; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import org.apache.commons.httpclient.ConnectTimeoutException; import org.apache.commons.httpclient.params.HttpConnectionParams; import org.apache.commons.httpclient.protocol.ProtocolSocketFactory; public class MySSLSocketFactory implements ProtocolSocketFactory { private SSLContext sslcontext = null; private SSLContext createSSLContext() { SSLContext sslcontext = null; try { sslcontext = SSLContext.getInstance("SSL"); sslcontext.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new java.security.SecureRandom()); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (KeyManagementException e) { e.printStackTrace(); } return sslcontext; } private SSLContext getSSLContext() { if (this.sslcontext == null) { this.sslcontext = createSSLContext(); } return this.sslcontext; } public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException { return getSSLContext().getSocketFactory().createSocket(socket, host, port, autoClose); } public Socket createSocket(String host, int port) throws IOException, UnknownHostException { return getSSLContext().getSocketFactory().createSocket(host, port); } public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort) throws IOException, UnknownHostException { return getSSLContext().getSocketFactory().createSocket(host, port, clientHost, clientPort); } public Socket createSocket(String host, int port, InetAddress localAddress, int localPort, HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException { if (params == null) { throw new IllegalArgumentException("Parameters may not be null"); } int timeout = params.getConnectionTimeout(); SocketFactory socketfactory = getSSLContext().getSocketFactory(); if (timeout == 0) { return socketfactory.createSocket(host, port, localAddress, localPort); } else { Socket socket = socketfactory.createSocket(); SocketAddress localaddr = new InetSocketAddress(localAddress, localPort); SocketAddress remoteaddr = new InetSocketAddress(host, port); socket.bind(localaddr); socket.connect(remoteaddr, timeout); return socket; } } private static class TrustAnyTrustManager implements X509TrustManager { public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[] {}; } } }调用httpclient之前执行 Protocol.registerProtocol("https", new Protocol("https", new MySSLSocketFactory(), 443)); 在httpclient模拟请求的时候,如果需要保留cookie,需要使用httpclient 提供的获取cookie的方法,不要自己从header中拿Set-Cookie头信息,以免无法正常模拟请求失败。
转载地址:https://blog.csdn.net/dogwwwservice/article/details/84741712 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
很好
[***.229.124.182]2024年04月19日 19时03分06秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
解释Spring支持的几种bean的作用域。
2021-06-29
Spring框架中都用到了哪些设计模式?
2021-06-29
什么是MyBatis?
2021-06-29
MyBatis的优点和缺点
2021-06-29
#{}和${}的区别是什么?
2021-06-29
LeetCode题解(1508):子数组和排序后的区间和(Python)
2021-06-29
LeetCode题解(1509):三次操作后最大值与最小值的最小差(Python)
2021-06-29
LeetCode题解(1510):石子游戏IV(Python)
2021-06-29
LeetCode题解(1497):检查数组对是否可以被k整除(Python)
2021-06-29
LeetCode题解(0781):森林中的兔子(Python)
2021-06-29
LeetCode题解(0930):和相同的二元子数组(Python)
2021-06-29
LeetCode题解(0939):最小面积矩形(Python)
2021-06-29
LeetCode题解(0954):二倍数对数组(Python)
2021-06-29
LeetCode题解(0957):N天后的牢房(Python)
2021-06-29
LeetCode题解(0974):和可被K整除的子数组(Python)
2021-06-29
LeetCode题解(0981):基于时间的键值存储(Python)
2021-06-29
LeetCode题解(0992):K个不同呢的子数组(Python)
2021-06-29
LeetCode题解(1001):网格照明(Python)
2021-06-29
LeetCode题解(1044):最长重复子串(Python)
2021-06-29
LeetCode题解(1048):最长字符串链(Python)
2021-06-29