您的位置:首页 > 理论基础 > 计算机网络

调用https接口抛出javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path

2018-04-02 17:40 525 查看
问题:javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target        前几日,本小白在工作的时候,做到一个需求,就是请求一个https的接口,但是呢碰到一个很奇怪的问题就是,我本地可以完美调通,但是提交代码后没有一个能调通。。。。在网上找了一圈都不好使,最后找到这样的一个方法,直接上硬菜。解决方法:1,先搞一个 自己的https协议类 MySSLSocketFactory.javaimport 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 {static {System.out.println(">>>>in MySSLSocketFactory>>");}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[] {};}}2,请求的类import java.io.IOException;import java.io.InputStream;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.HttpException;import org.apache.commons.httpclient.HttpMethod;import org.apache.commons.httpclient.methods.PostMethod;import org.apache.commons.httpclient.protocol.Protocol;public class HttpsRequest {/**** @param url* @return*/public static String post(String url) {//增加下面两行代码Protocol myhttps = new Protocol("https", new MySSLSocketFactory(), 443);Protocol.registerProtocol("https", myhttps);HttpClient client = new HttpClient();HttpMethod post = new PostMethod(url);try {client.executeMethod(post);byte[] responseBody = post.getResponseBody();String result = new String(responseBody,"GBK");return result;} catch (HttpException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {post.releaseConnection();}return null;}
3,另一个类import java.io.IOException;import java.io.InputStream;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.HttpException;import org.apache.commons.httpclient.HttpMethod;import org.apache.commons.httpclient.methods.PostMethod;import org.apache.commons.httpclient.protocol.Protocol;public class HttpsRequest {/**** @param url* @return*/public static String post(String url) {//增加下面两行代码Protocol myhttps = new Protocol("https", new MySSLSocketFactory(), 443);Protocol.registerProtocol("https", myhttps);HttpClient client = new HttpClient();HttpMethod post = new PostMethod(url);try {client.executeMethod(post);byte[] responseBody = post.getResponseBody();String result = new String(responseBody,"GBK");return result;} catch (HttpException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {post.releaseConnection();}return null;}OK,到这里在你的请求上面加上这两行代码就完美解决https出现的报错的问题了这两行!!!!!!Protocol myhttps = new Protocol("https", new MySSLSocketFactory(), 443);Protocol.registerProtocol("https", myhttps);有更好的方案的大佬请多多指导,谢谢啦
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐