码迷,mamicode.com
首页 > 移动开发 > 详细

Android 实现 HttpClient 请求Https

时间:2014-07-11 11:03:20      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:android   blog   http   java   strong   os   

 

如题,默认下,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,布布扣,bubuko.com

Android 实现 HttpClient 请求Https

标签:android   blog   http   java   strong   os   

原文地址:http://www.cnblogs.com/xgjblog/p/3834049.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!