歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android 實現 HttpClient 請求Https

Android 實現 HttpClient 請求Https,默認情況下,HttpClient是不能請求Https的,需要自己獲取。

private static final int SET_CONNECTION_TIMEOUT = 5 * 1000;
 private static final int SET_SOCKET_TIMEOUT = 20 * 1000;
 
 public static HttpClient getNewHttpClient() {
  try {
   KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
   trustStore.load(null, null);

   SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
   sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

   HttpParams params = new BasicHttpParams();

   HttpConnectionParams.setConnectionTimeout(params, 10000);
   HttpConnectionParams.setSoTimeout(params, 10000);

   HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
   HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

   SchemeRegistry registry = new SchemeRegistry();
   registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
   registry.register(new Scheme("https", sf, 443));

   ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

   HttpConnectionParams.setConnectionTimeout(params, SET_CONNECTION_TIMEOUT);
   HttpConnectionParams.setSoTimeout(params, SET_SOCKET_TIMEOUT);
   HttpClient client = new DefaultHttpClient(ccm, params);

   return client;
  } catch (Exception e) {
   return new DefaultHttpClient();
  }
 }

下面是MySSLSocketFactory類

private static class MySSLSocketFactory extends SSLSocketFactory {
  SSLContext sslContext = SSLContext.getInstance("TLS");

  public MySSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException,
    KeyManagementException, KeyStoreException, UnrecoverableKeyException {
   super(truststore);

   TrustManager tm = new X509TrustManager() {
    public void checkClientTrusted(X509Certificate[] chain, String authType)
      throws CertificateException {
    }

    public void checkServerTrusted(X509Certificate[] chain, String authType)
      throws CertificateException {
    }

    public X509Certificate[] getAcceptedIssuers() {
     return null;
    }
   };

   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();
  }
 }

通過上面的方法獲得HttpClient對象就可以請求Https了。

Ubuntu 14.04 x64配置Android 4.4 kitkat編譯環境的方法 http://www.linuxidc.com/Linux/2014-05/101148.htm

Ubuntu 12.04搭建Android開發環境 http://www.linuxidc.com/Linux/2012-09/69961.htm

Ubuntu 14.04 配置 Android SDK 開發環境 http://www.linuxidc.com/Linux/2014-05/101039.htm

64位Ubuntu 11.10下Android開發環境的搭建(JDK+Eclipse+ADT+Android SDK詳細) http://www.linuxidc.com/Linux/2013-06/85303.htm

Ubuntu 12.10 x64 安裝 Android SDK http://www.linuxidc.com/Linux/2013-03/82005.htm

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved