HttpClient请求HTTPS网页报unable to find valid certification

增加一个类

  3import java.io.IOException;
  4import java.net.InetAddress;
  5import java.net.InetSocketAddress;
  6import java.net.Socket;
  7import java.net.SocketAddress;
  8import java.net.UnknownHostException;
  9import java.security.KeyManagementException;
 10import java.security.NoSuchAlgorithmException;
 11import java.security.cert.CertificateException;
 12import java.security.cert.X509Certificate;
 13
 14import javax.net.SocketFactory;
 15import javax.net.ssl.SSLContext;
 16import javax.net.ssl.TrustManager;
 17import javax.net.ssl.X509TrustManager;
 18
 19import org.apache.commons.httpclient.ConnectTimeoutException;
 20import org.apache.commons.httpclient.params.HttpConnectionParams;
 21import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
 22
 23public class MySSLSocketFactory implements SecureProtocolSocketFactory {
 24    static{
 25        System.out.println("loading SSL");
 26    }

 27    private SSLContext sslcontext = null;
 28    
 29    private SSLContext createSSLContext() {
 30        SSLContext sslcontext=null;
 31        try {
 32            sslcontext = SSLContext.getInstance("SSL");
 33            sslcontext.init(nullnew TrustManager[]{new TrustAnyTrustManager()}new java.security.SecureRandom());
 34        }
 catch (NoSuchAlgorithmException e) {
 35            e.printStackTrace();
 36        }
 catch (KeyManagementException e) {
 37            e.printStackTrace();
 38        }

 39        return sslcontext;
 40    }

 41    
 42    private SSLContext getSSLContext() {
 43        if (this.sslcontext == null{
 44            this.sslcontext = createSSLContext();
 45        }

 46        return this.sslcontext;
 47    }

 48    
 49    public Socket createSocket(Socket socket, String host, int port, boolean autoClose)
 50            throws IOException, UnknownHostException {
 51        return getSSLContext().getSocketFactory().createSocket(
 52                socket,
 53                host,
 54                port,
 55                autoClose
 56            );
 57    }

 58
 59    public Socket createSocket(String host, int port) throws IOException,
 60            UnknownHostException {
 61        return getSSLContext().getSocketFactory().createSocket(
 62                host,
 63                port
 64            );
 65    }

 66    
 67    
 68    public Socket createSocket(String host, int port, InetAddress clientHost, int clientPort)
 69            throws IOException, UnknownHostException {
 70        return getSSLContext().getSocketFactory().createSocket(host, port, clientHost, clientPort);
 71    }

 72
 73    public Socket createSocket(String host, int port, InetAddress localAddress,
 74            int localPort, HttpConnectionParams params) throws IOException,
 75            UnknownHostException, ConnectTimeoutException {
 76        if (params == null{
 77            throw new IllegalArgumentException("Parameters may not be null");
 78        }

 79        int timeout = params.getConnectionTimeout();
 80        SocketFactory socketfactory = getSSLContext().getSocketFactory();
 81        if (timeout == 0{
 82            return socketfactory.createSocket(host, port, localAddress, localPort);
 83        }
 else {
 84            Socket socket = socketfactory.createSocket();
 85            SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
 86            SocketAddress remoteaddr = new InetSocketAddress(host, port);
 87            socket.bind(localaddr);
 88            socket.connect(remoteaddr, timeout);
 89            return socket;
 90        }

 91    }

 92    
 93    //自定义私有类
 94    private static class TrustAnyTrustManager implements X509TrustManager {
 95        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
 96        }

 97        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
 98        }

 99        public X509Certificate[] getAcceptedIssuers() {
100            return new X509Certificate[]{};
101        }

102    }

103
104}

105

调用即可

if(postUrl.startsWith("https://"))
  {
   //注册https
   Protocol myhttps = new Protocol("https", new MySSLSocketFactory (), 443);
   Protocol.registerProtocol("https", myhttps);
  }
  
  HttpClient httpClient = new HttpClient();
  httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, charset);

原文地址:https://www.cnblogs.com/yesun/p/1255263.html