如何在Android的HTTPS连接? [英] How to create an https Connection in Android?

查看:100
本文介绍了如何在Android的HTTPS连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现在很多职位#1,但无法得到解决工作:

如何建立在Android的HTTPS连接?

code是:

 的HttpParams httpParameters =新BasicHttpParams();
    ConnManagerParams.setMaxTotalConnections(httpParameters,25);
    INT timeoutConnection = 10000;
    HttpConnectionParams.setConnectionTimeout(httpParameters,
            timeoutConnection);
    INT timeoutSocket = 10000;
    HttpConnectionParams.setSoTimeout(httpParameters,timeoutSocket);

    SchemeRegistry schemeRegistry =新SchemeRegistry();
    schemeRegistry.register(新计划(HTTP,PlainSocketFactory
            .getSocketFactory(),80));
            schemeRegistry.register(新计划(https开头,SSLSocketFactory的
            .getSocketFactory(),443));

    ClientConnectionManager厘米=新ThreadSafeClientConnManager(
            httpParameters,schemeRegistry);
    HttpClient的=新DefaultHttpClient(厘米,httpParameters);
 

任何帮助,将AP preciated。

解决方案

 的HttpClient的HttpClient = getNewHttpClient();

HTTPGET HTTPGET =新HTTPGET(URL);
            HTT presponse响应= httpclient.execute(HTTPGET);
            HttpEntity实体= response.getEntity();
            是= entity.getContent();

公共HttpClient的getNewHttpClient(){
        尝试 {
            密钥库的trustStore = KeyStore.getInstance(密钥库
                    .getDefaultType());
            trustStore.load(NULL,NULL);

            SSLSocketFactory的SF =新MySSLSocketFactory(的trustStore);
            sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

            的HttpParams PARAMS =新BasicHttpParams();
            HttpProtocolParams.setVersion(参数,可以HttpVersion.HTTP_1_1);
            HttpProtocolParams.setContentCharset(参数,可以HTTP.UTF_8);

            SchemeRegistry注册表=新SchemeRegistry();
            registry.register(新计划(HTTP,PlainSocketFactory
                    .getSocketFactory(),80));
            registry.register(新计划(https开头,SF,443));



            ClientConnectionManager CCM =新ThreadSafeClientConnManager(
                    参数,可以登记);

            返回新DefaultHttpClient(CCM,则params);
        }赶上(例外五){
            返回新DefaultHttpClient();
        }
    }
 

MySSLSocketFactory.java

 公共类MySSLSocketFactory扩展的SSLSocketFactory {
    的SSL连接的SSL连接= SSLContext.getInstance(TLS);

公共MySSLSocketFactory(密钥库信任库)抛出抛出:NoSuchAlgorithmException,KeyManagementException,KeyStoreException,UnrecoverableKeyException {
    超(信任库);

    的TrustManager TM =新X509TrustManager(){
        公共无效checkClientTrusted(x509证书[]链,字符串的authType)抛出CertificateException {
        }

        公共无效checkServerTrusted(x509证书[]链,字符串的authType)抛出CertificateException {
        }

        公共x509证书[] getAcceptedIssuers(){
            返回null;
        }
    };

    sslContext.init(空,新的TrustManager [] {} TM,NULL);
}

@覆盖
公共插座中的createSocket(Socket套接字,字符串主机,INT端口,布尔自动关闭)抛出IOException异常,UnknownHostException异常{
    返回sslContext.getSocketFactory()中的createSocket(插座,主机,端口自动关闭)。
}

@覆盖
公共插座中的createSocket()抛出IOException异常{
    返回sslContext.getSocketFactory()中的createSocket()。
}

}
 

I found lots of posts in Stackoverflow but could not get the solution working for :

How to create an https Connection in Android?

Code is :

            HttpParams httpParameters = new BasicHttpParams();
    ConnManagerParams.setMaxTotalConnections(httpParameters, 25);
    int timeoutConnection = 10000;
    HttpConnectionParams.setConnectionTimeout(httpParameters,
            timeoutConnection);
    int timeoutSocket = 10000;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

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

    ClientConnectionManager cm = new ThreadSafeClientConnManager(
            httpParameters, schemeRegistry);
    httpClient = new DefaultHttpClient(cm, httpParameters);

Any Help would be appreciated.

解决方案

HttpClient httpclient = getNewHttpClient();  

HttpGet httpget = new HttpGet(URL);
            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

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

            return new DefaultHttpClient(ccm, params);
        } catch (Exception e) {
            return new DefaultHttpClient();
        }
    }

MySSLSocketFactory.java

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

}

这篇关于如何在Android的HTTPS连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆