zoukankan      html  css  js  c++  java
  • httpClient 信任所有证书

    转载自:http://hougbin.iteye.com/blog/1196063

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

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

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

       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;  
          
          
          
        /**
         * author by lpp
         *
         * created at 2010-7-26 上午09:29:33
         */  
        public class MySSLProtocolSocketFactory 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[]{};   
              }   
          }     
          
          
        }  

    3.用的jar包

            <dependency>
                <groupId>commons-httpclient</groupId>
                <artifactId>commons-httpclient</artifactId>
             <version>3.1</version>
            </dependency>
  • 相关阅读:
    jquery选择器(复习向)
    如何使元素/文字 垂直居中?
    mvc与mvvm的区别与联系
    python16_day26【crm 增、改、查】
    python16_day25【crm】
    python16_day24【restful、crm表构、认证】
    python16_day23【cmdb前端】
    django 【认证】
    python16_day22【cmdb注释】
    django【F和Q】
  • 原文地址:https://www.cnblogs.com/SunAutumn/p/9075868.html
Copyright © 2011-2022 走看看