首页>>移动互联网>>Android 无法连接SSL(https)进行网络通信
Android 无法连接SSL(https)进行网络通信
来源: 时间: 2015-02-09 11:52:09

其他系统版本号手机都是好的,甚至在2.2.X系统的上都能正常访问网络,运行在2.3.x系统手机上时,报错:

android javax.net.ssl.SSLPeerUnverifiedException: No peer certificate

解决方法如下:(解决方法转载自:http://lanyan-lan.iteye.com/blog/1522370)

第一步:编写SSLSocketFactoryEx,以代替原有的SSLSocketFactory,代码如下

import org.apache.http.conn.ssl.SSLSocketFactory;

public class SSLSocketFactoryEx extends SSLSocketFactory {

    SSLContext sslContext = SSLContext.getInstance("TLS");

    public SSLSocketFactoryEx(KeyStore truststore)

            throws NoSuchAlgorithmException, KeyManagementException,

            KeyStoreException, UnrecoverableKeyException {

        super(truststore);

        TrustManager tm = new X509TrustManager() {

            public java.security.cert.X509Certificate[] getAcceptedIssuers() {

                return null;

            }

            @Override

            public void checkClientTrusted(

                    java.security.cert.X509Certificate[] chain, String authType)

                    throws java.security.cert.CertificateException {

            }

            @Override

            public void checkServerTrusted(

                    java.security.cert.X509Certificate[] chain, String authType)

                    throws java.security.cert.CertificateException {

            }

        };

        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  getNewHttpClient来代替原有DefaultHttpClient,代码如下:

public static HttpClient getNewHttpClient() {

   try {

       KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());

       trustStore.load(null, null);

       SSLSocketFactory sf = new SSLSocketFactoryEx(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();

   }

}

第三步;接着在项目里把所有获得HttpClient实例通过DefaultHttpClient,都改成从

getNewHttpClient获取

//HttpClient hc = new DefaultHttpClient(httpParams);

//modified by lucky.lan@2012/05/10

HttpClient hc = ConnectionManager.getNewHttpClient() ;

本页地址:http://www.easytd.com/yidonghulianwang/d_15020911264.html

上一篇:Android 创建shortcut快捷方式 下一篇:Android SharePreference的存储和取值