android httpClient 支持HTTPS的处理方式

问题:
项目中Android https或http请求地址重定向为HTTPS的地址,相信很多人都遇到了这个异常(无终端认证): javax.NET.ssl.SSLPeerUnverifiedException: No peer certificate

解决过程:
1.没遇到过的问题,搜索吧,少年

log里出现这个异常,作者第一次遇到,不知道啥意思。看下字面意思,是ssl协议中没有终端认证。SSL?作者没用到ssl协议呀,只是通过httpClient请求一个重定向https的地址。
好吧,google下,知道了个差不多情况的帖子,http://www.eoeandroid.com/thread-161747-1-1.html。恩恩,一个不错的帖子,给出了个解决方案。照着来试下。添加个继承SSLSocketFactory的
自定义类。并在初始化httpclient支持https时,注册进去。看下面代码:

public class HttpClientHelper {
02

03
    private static HttpClient httpClient;
04

05
    private HttpClientHelper() {
06
    }
07

08
    public static synchronized HttpClient getHttpClient() {
09

10
        if (null == httpClient) {
11
            // 初始化工作
12
            try {
13
                KeyStore trustStore = KeyStore.getInstance(KeyStore
14
                        .getDefaultType());
15
                trustStore.load(null, null);
16
                SSLSocketFactory sf = new SSLSocketFactoryEx(trustStore);
17
                sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);  //允许所有主机的验证
18

19
                HttpParams params = new BasicHttpParams();
20

21
                HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
22
                HttpProtocolParams.setContentCharset(params,
23
                        HTTP.DEFAULT_CONTENT_CHARSET);
24
                HttpProtocolParams.setUseExpectContinue(params, true);
25

26
                // 设置连接管理器的超时
27
                ConnManagerParams.setTimeout(params, 10000);
28
                // 设置连接超时
29
                HttpConnectionParams.setConnectionTimeout(params, 10000);
30
                // 设置socket超时
31
                HttpConnectionParams.setSoTimeout(params, 10000);
32

33
                // 设置http https支持
34
                SchemeRegistry schReg = new SchemeRegistry();
35
                schReg.register(new Scheme("http", PlainSocketFactory
36
                        .getSocketFactory(), 80));
37
                schReg.register(new Scheme("https", sf, 443));
38

39
                ClientConnectionManager conManager = new ThreadSafeClientConnManager(
40
                        params, schReg);
41

42
                httpClient = new DefaultHttpClient(conManager, params);
43
            } catch (Exception e) {
44
                e.printStackTrace();
45
                return new DefaultHttpClient();
46
            }
47
        }
48
        return httpClient;
49
    }
50

51
}
52

53
class SSLSocketFactoryEx extends SSLSocketFactory {
54

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

57
    public SSLSocketFactoryEx(KeyStore truststore)
58
            throws NoSuchAlgorithmException, KeyManagementException,
59
            KeyStoreException, UnrecoverableKeyException {
60
        super(truststore);
61

62
        TrustManager tm = new X509TrustManager() {
63

64
            @Override
65
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
66
                return null;
67
            }
68

69
            @Override
70
            public void checkClientTrusted(
71
                    java.security.cert.X509Certificate[] chain, String authType)
72
                    throws java.security.cert.CertificateException {
73

74
            }
75

76
            @Override
77
            public void checkServerTrusted(
78
                    java.security.cert.X509Certificate[] chain, String authType)
79
                    throws java.security.cert.CertificateException {
80

81
            }
82
        };
83

84
        sslContext.init(null, new TrustManager[] { tm }, null);
85
    }
86

87
    @Override
88
    public Socket createSocket(Socket socket, String host, int port,
89
            boolean autoClose) throws IOException, UnknownHostException {
90
        return sslContext.getSocketFactory().createSocket(socket, host, port,
91
                autoClose);
92
    }
93
94
   @Override
95
   public Socket createSocket() throws IOException {
96
        return sslContext.getSocketFactory().createSocket();
97
    }
98
}

http://blog.csdn.net/lihenair/article/details/17441169

你可能感兴趣的:(ssl,android,移动开发-Android)