---
title: "httpclient 无信任证书使用https"
author: "paynexss"
date: "2017-02-13T21:36:05Z"
tags: ["HTTPS", "HttpClient"]
categories: ["编程语言"]
slug: "httpclient-https"
draft: false
lastmod: "2017-02-13T21:36:05Z"
id: 2356905
url: "https://paynexss.iteye.com/blog/2356905"
---

1、当不需要使用任何证书访问https网页时，只需配置信任任何证书

Java代码  [![收藏代码](http://hougbin.iteye.com/images/icon_star.png)]()

1. HttpClient http = new HttpClient();
2. String url = "https://payment.cib.com.cn/payment/api/rest";
3. Protocol myhttps = new Protocol("https", new MySSLProtocolSocketFactory(), 443);
4. Protocol.registerProtocol("https", myhttps);
5. PostMethod post = new PostMethod(url);

 

 

2、其中信任任何证书的类

Java代码  [![收藏代码](http://hougbin.iteye.com/images/icon_star.png)]()

01. import java.io.IOException;
02. import java.net.InetAddress;
03. import java.net.InetSocketAddress;
04. import java.net.Socket;
05. import java.net.SocketAddress;
06. import java.net.UnknownHostException;
07. import java.security.KeyManagementException;
08. import java.security.NoSuchAlgorithmException;
09. import java.security.cert.CertificateException;
10. import java.security.cert.X509Certificate;
11. import javax.net.SocketFactory;
12. import javax.net.ssl.SSLContext;
13. import javax.net.ssl.TrustManager;
14. import javax.net.ssl.X509TrustManager;
15. import org.apache.commons.httpclient.ConnectTimeoutException;
16. import org.apache.commons.httpclient.params.HttpConnectionParams;
17. import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
18. /\**
19. * author by lpp
20. \*
21. * created at 2010-7-26 上午09:29:33
22. \*/
23. public class MySSLProtocolSocketFactory implements ProtocolSocketFactory {
24. private SSLContext sslcontext = null;
25. private SSLContext createSSLContext() {
26. SSLContext sslcontext=null;
27. try {
28. sslcontext = SSLContext.getInstance("SSL");
29. sslcontext.init(null, new TrustManager\[]{new TrustAnyTrustManager()}, new java.security.SecureRandom());
30. } catch (NoSuchAlgorithmException e) {
31. e.printStackTrace();
32. } catch (KeyManagementException e) {
33. e.printStackTrace();
34. }
35. return sslcontext;
36. }
37. private SSLContext getSSLContext() {
38. if (this.sslcontext == null) {
39. this.sslcontext = createSSLContext();
40. }
41. return this.sslcontext;
42. }
43. public Socket createSocket(Socket socket, String host, int port, boolean autoClose)
44. throws IOException, UnknownHostException {
45. return getSSLContext().getSocketFactory().createSocket(
46. socket,
47. host,
48. port,
49. autoClose
50. );
51. }
52. public Socket createSocket(String host, int port) throws IOException,
53. UnknownHostException {
54. return getSSLContext().getSocketFactory().createSocket(
55. host,
56. port
57. );
58. }
59. public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort)
60. throws IOException, UnknownHostException {
61. return getSSLContext().getSocketFactory().createSocket(host, port, clientHost, clientPort);
62. }
63. public Socket createSocket(String host, int port, InetAddress localAddress,
64. int localPort, HttpConnectionParams params) throws IOException,
65. UnknownHostException, ConnectTimeoutException {
66. if (params == null) {
67. throw new IllegalArgumentException("Parameters may not be null");
68. }
69. int timeout = params.getConnectionTimeout();
70. SocketFactory socketfactory = getSSLContext().getSocketFactory();
71. if (timeout == 0) {
72. return socketfactory.createSocket(host, port, localAddress, localPort);
73. } else {
74. Socket socket = socketfactory.createSocket();
75. SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
76. SocketAddress remoteaddr = new InetSocketAddress(host, port);
77. socket.bind(localaddr);
78. socket.connect(remoteaddr, timeout);
79. return socket;
80. }
81. }
82. //自定义私有类
83. private static class TrustAnyTrustManager implements X509TrustManager {
84. public void checkClientTrusted(X509Certificate\[] chain, String authType) throws CertificateException {
85. }
86. public void checkServerTrusted(X509Certificate\[] chain, String authType) throws CertificateException {
87. }
88. public X509Certificate\[] getAcceptedIssuers() {
89. return new X509Certificate\[]{};
90. }
91. }
92. }