Android 实现 HttpClient 请求Https

 

如题,默认下,HttpClient是不能请求Https的,需要自己获取


  1. private static final int SET_CONNECTION_TIMEOUT = 5 * 1000;  
  2.     private static final int SET_SOCKET_TIMEOUT = 20 * 1000;  
  3.       
  4.     public static HttpClient getNewHttpClient() {  
  5.         try {  
  6.             KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());  
  7.             trustStore.load(null, null);  
  8.   
  9.             SSLSocketFactory sf = new MySSLSocketFactory(trustStore);  
  10.             sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);  
  11.   
  12.             HttpParams params = new BasicHttpParams();  
  13.   
  14.             HttpConnectionParams.setConnectionTimeout(params, 10000);  
  15.             HttpConnectionParams.setSoTimeout(params, 10000);  
  16.   
  17.             HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);  
  18.             HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);  
  19.   
  20.             SchemeRegistry registry = new SchemeRegistry();  
  21.             registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));  
  22.             registry.register(new Scheme("https", sf, 443));  
  23.   
  24.             ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);  
  25.   
  26.             HttpConnectionParams.setConnectionTimeout(params, SET_CONNECTION_TIMEOUT);  
  27.             HttpConnectionParams.setSoTimeout(params, SET_SOCKET_TIMEOUT);  
  28.             HttpClient client = new DefaultHttpClient(ccm, params);  
  29.   
  30.             return client;  
  31.         } catch (Exception e) {  
  32.             return new DefaultHttpClient();  
  33.         }  
  34.     }  


下面是MySSLSocketFactory类

    1. private static class MySSLSocketFactory extends SSLSocketFactory {  
    2.         SSLContext sslContext = SSLContext.getInstance("TLS");  
    3.   
    4.         public MySSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException,  
    5.                 KeyManagementException, KeyStoreException, UnrecoverableKeyException {  
    6.             super(truststore);  
    7.   
    8.             TrustManager tm = new X509TrustManager() {  
    9.                 public void checkClientTrusted(X509Certificate[] chain, String authType)  
    10.                         throws CertificateException {  
    11.                 }  
    12.   
    13.                 public void checkServerTrusted(X509Certificate[] chain, String authType)  
    14.                         throws CertificateException {  
    15.                 }  
    16.   
    17.                 public X509Certificate[] getAcceptedIssuers() {  
    18.                     return null;  
    19.                 }  
    20.             };  
    21.   
    22.             sslContext.init(null, new TrustManager[] { tm }, null);  
    23.         }  
    24.   
    25.         @Override  
    26.         public Socket createSocket(Socket socket, String host, int port, boolean autoClose)  
    27.                 throws IOException, UnknownHostException {  
    28.             return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);  
    29.         }  
    30.   
    31.         @Override  
    32.         public Socket createSocket() throws IOException {  
    33.             return sslContext.getSocketFactory().createSocket();  
    34.         }  
    35.     } 

Android 实现 HttpClient 请求Https,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。