开发者社区> 问答> 正文

httpclient请求报错javax.net.ssl.SSLException

有没有大神知道如何解决
javax.net.ssl.SSLException: Received fatal alert: protocol_version

展开
收起
蛮大人123 2016-03-18 11:36:01 6585 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪
    public class HttpClientHelper {
    
        private static HttpClient httpClient;
    
        private HttpClientHelper() {
        }
    
        public static synchronized HttpClient getHttpClient() {
    
            if (null == httpClient) {
                // 初始化工作
                try {
                    KeyStore trustStore = KeyStore.getInstance(KeyStore
                            .getDefaultType());
                    trustStore.load(null, null);
                    SSLSocketFactory sf = new SSLSocketFactoryEx(trustStore);
                    sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);  //允许所有主机的验证
    
                    HttpParams params = new BasicHttpParams();
    
                    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
                    HttpProtocolParams.setContentCharset(params,
                            HTTP.DEFAULT_CONTENT_CHARSET);
                    HttpProtocolParams.setUseExpectContinue(params, true);
    
                    // 设置连接管理器的超时
                    ConnManagerParams.setTimeout(params, 10000);
                    // 设置连接超时
                    HttpConnectionParams.setConnectionTimeout(params, 10000);
                    // 设置socket超时
                    HttpConnectionParams.setSoTimeout(params, 10000);
    
                    // 设置http https支持
                    SchemeRegistry schReg = new SchemeRegistry();
                    schReg.register(new Scheme("http", PlainSocketFactory
                            .getSocketFactory(), 80));
                    schReg.register(new Scheme("https", sf, 443));
    
                    ClientConnectionManager conManager = new ThreadSafeClientConnManager(
                            params, schReg);
    
                    httpClient = new DefaultHttpClient(conManager, params);
                } catch (Exception e) {
                    e.printStackTrace();
                    return new DefaultHttpClient();
                }
            }
            return httpClient;
        }
    
    }
    
    class SSLSocketFactoryEx extends SSLSocketFactory {
    
        SSLContext sslContext = SSLContext.getInstance("TLS");
    
        public SSLSocketFactoryEx(KeyStore truststore)
                throws NoSuchAlgorithmException, KeyManagementException,
                KeyStoreException, UnrecoverableKeyException {
            super(truststore);
    
            TrustManager tm = new X509TrustManager() {
    
                @Override
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
    
                @Override
                public void checkClientTrusted(
                        java.security.cert.X509Certificate[] chain, String authType)
                        throws java.security.cert.CertificateException {
    
                }
    
                @Override
                public void checkServerTrusted(
                        java.security.cert.X509Certificate[] chain, String authType)
                        throws java.security.cert.CertificateException {
    
                }
            };
    
            sslContext.init(null, new TrustManager[] { tm }, null);
        }
    
        @Override
        public Socket createSocket(Socket socket, String host, int port,
                boolean autoClose) throws IOException, UnknownHostException {
            return sslContext.getSocketFactory().createSocket(socket, host, port,
                    autoClose);
        }
    
        @Override
        public Socket createSocket() throws IOException {
            return sslContext.getSocketFactory().createSocket();
        }
    }
    2019-07-17 19:06:17
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载