Httpclient for Android requests https

Source: Internet
Author: User

For example, by default, httpclient cannot request 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();}}

The following is the mysslsocketfactory class

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);}@Overridepublic Socket createSocket(Socket socket, String host, int port, boolean autoClose)throws IOException, UnknownHostException {return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);}@Overridepublic Socket createSocket() throws IOException {return sslContext.getSocketFactory().createSocket();}}

You can obtain the httpclient object through the above method to request HTTPS.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.