欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

httpclient 无信任证书使用https

程序员文章站 2022-07-02 09:34:14
...

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

Java代码  httpclient 无信任证书使用https
            
    
    博客分类: JAVA httpclienthttps 
  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代码  httpclient 无信任证书使用https
            
    
    博客分类: JAVA httpclienthttps 
  1. import java.io.IOException;  
  2. import java.net.InetAddress;  
  3. import java.net.InetSocketAddress;  
  4. import java.net.Socket;  
  5. import java.net.SocketAddress;  
  6. import java.net.UnknownHostException;  
  7. import java.security.KeyManagementException;  
  8. import java.security.NoSuchAlgorithmException;  
  9. import java.security.cert.CertificateException;  
  10. import java.security.cert.X509Certificate;  
  11.   
  12. import javax.net.SocketFactory;  
  13. import javax.net.ssl.SSLContext;  
  14. import javax.net.ssl.TrustManager;  
  15. import javax.net.ssl.X509TrustManager;  
  16.   
  17. import org.apache.commons.httpclient.ConnectTimeoutException;  
  18. import org.apache.commons.httpclient.params.HttpConnectionParams;  
  19. import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;  
  20.   
  21.   
  22.   
  23. /** 
  24.  * author by lpp 
  25.  * 
  26.  * created at 2010-7-26 上午09:29:33 
  27.  */  
  28. public class MySSLProtocolSocketFactory implements ProtocolSocketFactory {  
  29.   
  30.   private SSLContext sslcontext = null;   
  31.    
  32.   private SSLContext createSSLContext() {   
  33.       SSLContext sslcontext=null;   
  34.       try {   
  35.           sslcontext = SSLContext.getInstance("SSL");   
  36.           sslcontext.init(nullnew TrustManager[]{new TrustAnyTrustManager()}, new java.security.SecureRandom());   
  37.       } catch (NoSuchAlgorithmException e) {   
  38.           e.printStackTrace();   
  39.       } catch (KeyManagementException e) {   
  40.           e.printStackTrace();   
  41.       }   
  42.       return sslcontext;   
  43.   }   
  44.    
  45.   private SSLContext getSSLContext() {   
  46.       if (this.sslcontext == null) {   
  47.           this.sslcontext = createSSLContext();   
  48.       }   
  49.       return this.sslcontext;   
  50.   }   
  51.    
  52.   public Socket createSocket(Socket socket, String host, int port, boolean autoClose)   
  53.           throws IOException, UnknownHostException {   
  54.       return getSSLContext().getSocketFactory().createSocket(   
  55.               socket,   
  56.               host,   
  57.               port,   
  58.               autoClose   
  59.           );   
  60.   }   
  61.   
  62.   public Socket createSocket(String host, int port) throws IOException,   
  63.           UnknownHostException {   
  64.       return getSSLContext().getSocketFactory().createSocket(   
  65.               host,   
  66.               port   
  67.           );   
  68.   }   
  69.    
  70.    
  71.   public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort)   
  72.           throws IOException, UnknownHostException {   
  73.       return getSSLContext().getSocketFactory().createSocket(host, port, clientHost, clientPort);   
  74.   }   
  75.   
  76.   public Socket createSocket(String host, int port, InetAddress localAddress,   
  77.           int localPort, HttpConnectionParams params) throws IOException,   
  78.           UnknownHostException, ConnectTimeoutException {   
  79.       if (params == null) {   
  80.           throw new IllegalArgumentException("Parameters may not be null");   
  81.       }   
  82.       int timeout = params.getConnectionTimeout();   
  83.       SocketFactory socketfactory = getSSLContext().getSocketFactory();   
  84.       if (timeout == 0) {   
  85.           return socketfactory.createSocket(host, port, localAddress, localPort);   
  86.       } else {   
  87.           Socket socket = socketfactory.createSocket();   
  88.           SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);   
  89.           SocketAddress remoteaddr = new InetSocketAddress(host, port);   
  90.           socket.bind(localaddr);   
  91.           socket.connect(remoteaddr, timeout);   
  92.           return socket;   
  93.       }   
  94.   }   
  95.    
  96.   //自定义私有类   
  97.   private static class TrustAnyTrustManager implements X509TrustManager {   
  98.       
  99.       public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {   
  100.       }   
  101.   
  102.       public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {   
  103.       }   
  104.   
  105.       public X509Certificate[] getAcceptedIssuers() {   
  106.           return new X509Certificate[]{};   
  107.       }   
  108.   }     
  109.   
  110.   
  111. }  
相关标签: httpclient https