javax.net.ssl.SSLHandshakeException

点击打开链接

以下是网上搜到的解决方案,在初始化httpclient实例前加上两行:


  1. ProtocolSocketFactory fcty = new MySecureProtocolSocketFactory();  
  2. Protocol.registerProtocol("https"new Protocol("https", fcty, 443));  
  3. httpClient = new HttpClient(); 



  1. package com.jingshou;  
  2.   
  3. import java.io.IOException;  
  4. import java.net.InetAddress;  
  5. import java.net.Socket;  
  6. import java.net.UnknownHostException;  
  7.   
  8. import javax.net.ssl.SSLContext;  
  9. import javax.net.ssl.TrustManager;  
  10.   
  11. import org.apache.commons.httpclient.ConnectTimeoutException;  
  12. import org.apache.commons.httpclient.HttpClientError;  
  13. import org.apache.commons.httpclient.params.HttpConnectionParams;  
  14. import org.apache.commons.httpclient.protocol.ControllerThreadSocketFactory;  
  15. import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;  
  16.   
  17. public class MySecureProtocolSocketFactory implements  
  18.         SecureProtocolSocketFactory {  
  19.   
  20.     private SSLContext sslContext = null;  
  21.   
  22.     /** 
  23.      * Constructor for MySecureProtocolSocketFactory. 
  24.      */  
  25.     public MySecureProtocolSocketFactory() {  
  26.     }  
  27.   
  28.     /** 
  29.      *  
  30.      * @return 
  31.      */  
  32.     private static SSLContext createEasySSLContext() {  
  33.         try {  
  34.             SSLContext context = SSLContext.getInstance("SSL");  
  35.             context.init(nullnew TrustManager[] { new MyX509TrustManager() },  
  36.                     null);  
  37.             return context;  
  38.         } catch (Exception e) {  
  39.             throw new HttpClientError(e.toString());  
  40.         }  
  41.     }  
  42.   
  43.     /** 
  44.      *  
  45.      * @return 
  46.      */  
  47.     private SSLContext getSSLContext() {  
  48.         if (this.sslContext == null) {  
  49.             this.sslContext = createEasySSLContext();  
  50.         }  
  51.         return this.sslContext;  
  52.     }  
  53.   
  54.     /* 
  55.      * (non-Javadoc) 
  56.      *  
  57.      * @see org.apache.commons.httpclient.protocol.ProtocolSocketFactory#createSocket(java.lang.String, 
  58.      *      int, java.net.InetAddress, int) 
  59.      */  
  60.     public Socket createSocket(String host, int port, InetAddress clientHost,  
  61.             int clientPort) throws IOException, UnknownHostException {  
  62.   
  63.         return getSSLContext().getSocketFactory().createSocket(host, port,  
  64.                 clientHost, clientPort);  
  65.     }  
  66.   
  67.     /* 
  68.      * (non-Javadoc) 
  69.      *  
  70.      * @see org.apache.commons.httpclient.protocol.ProtocolSocketFactory#createSocket(java.lang.String, 
  71.      *      int, java.net.InetAddress, int, 
  72.      *      org.apache.commons.httpclient.params.HttpConnectionParams) 
  73.      */  
  74.     public Socket createSocket(final String host, final int port,  
  75.             final InetAddress localAddress, final int localPort,  
  76.             final HttpConnectionParams params) throws IOException,  
  77.             UnknownHostException, ConnectTimeoutException {  
  78.         if (params == null) {  
  79.             throw new IllegalArgumentException("Parameters may not be null");  
  80.         }  
  81.         int timeout = params.getConnectionTimeout();  
  82.         if (timeout == 0) {  
  83.             return createSocket(host, port, localAddress, localPort);  
  84.         } else {              
  85.             return ControllerThreadSocketFactory.createSocket(this, host, port,  
  86.                     localAddress, localPort, timeout);  
  87.         }  
  88.     }  
  89.   
  90.     /* 
  91.      * (non-Javadoc) 
  92.      *  
  93.      * @see SecureProtocolSocketFactory#createSocket(java.lang.String,int) 
  94.      */  
  95.     public Socket createSocket(String host, int port) throws IOException,  
  96.             UnknownHostException {  
  97.         return getSSLContext().getSocketFactory().createSocket(host, port);  
  98.     }  
  99.   
  100.     /* 
  101.      * (non-Javadoc) 
  102.      *  
  103.      * @see SecureProtocolSocketFactory#createSocket(java.net.Socket,java.lang.String,int,boolean) 
  104.      */  
  105.     public Socket createSocket(Socket socket, String host, int port,  
  106.             boolean autoClose) throws IOException, UnknownHostException {  
  107.         return getSSLContext().getSocketFactory().createSocket(socket, host,  
  108.                 port, autoClose);  
  109.     }  

MyX509TrustManager类:
  1. package com.jingshou;  
  2.   
  3. import java.security.cert.CertificateException;  
  4. import java.security.cert.X509Certificate;  
  5.   
  6. import javax.net.ssl.X509TrustManager;  
  7.   
  8. public class MyX509TrustManager implements X509TrustManager {  
  9.   
  10.     /* (non-Javadoc) 
  11.      * @see javax.net.ssl.X509TrustManager#checkClientTrusted(java.security.cert.X509Certificate[], java.lang.String) 
  12.      */  
  13.     public void checkClientTrusted(X509Certificate[] arg0, String arg1)  
  14.         throws CertificateException {  
  15.   
  16.     }  
  17.   
  18.     /* (non-Javadoc) 
  19.      * @see javax.net.ssl.X509TrustManager#checkServerTrusted(java.security.cert.X509Certificate[], java.lang.String) 
  20.      */  
  21.     public void checkServerTrusted(X509Certificate[] arg0, String arg1)  
  22.         throws CertificateException {  
  23.   
  24.     }  
  25.   
  26.     /* (non-Javadoc) 
  27.      * @see javax.net.ssl.X509TrustManager#getAcceptedIssuers() 
  28.      */  
  29.     public X509Certificate[] getAcceptedIssuers() {  
  30.         return null;  
  31.     }  
  32.   


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值