Android 連結https出現 javax.net.ssl.SSLException: hostname in certificate didn't match:
阿新 • • 發佈:2019-02-17
轉載地址:http://blog.csdn.net/trbbadboy/article/details/11562511
丟擲的錯誤是這樣的:
[plain] view plaincopyprint?- javax.net.ssl.SSLException: hostname in certificate didn't match: <test.rigbee.cn> != <rigbee.cn> OR <rigbee.cn> OR <www.rigbee.cn>
- at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:185)
- at org.apache.http.conn.ssl.BrowserCompatHostnameVerifier.verify(BrowserCompatHostnameVerifier.java:54)
- at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:114)
- at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:95)
- at org.apache.http.conn.ssl.SSLSocketFactory.createSocket(SSLSocketFactory.java:381)
- at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:165)
- at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
- at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
- at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
- at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
- at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
- at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
嘗試過設定HttpsURLConnection.setDefaultHostnameVerifier(),不成功,估計是org.apache.http有自己的一套SSL東西,修改org.apache.http的主機名驗證解決問題。
增加則用的一行:
[java] view plaincopyprint?- SSLSocketFactory.getSocketFactory().setHostnameVerifier(new AllowAllHostnameVerifier());
同時import一下: [java] view plaincopyprint?
- import org.apache.http.conn.ssl.AllowAllHostnameVerifier;
- import org.apache.http.conn.ssl.SSLSocketFactory;
如果你使用org.apache.http的話上面的就夠了。
另外如果你使用原始的URLConnect的話請這樣設定一下:
[java] view plaincopyprint?- try {
- SSLContext sc = SSLContext.getInstance("TLS");
- sc.init(null, new TrustManager[] { new X509TrustManager() {
- @Override
- publicvoid checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
- }
- @Override
- publicvoid checkServerTrusted(X509Certificate[] chain, String authType)
- throws CertificateException {
- }
- @Override
- public X509Certificate[] getAcceptedIssuers() {
- returnnull;
- }
- } }, new SecureRandom());
- HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
- HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
- @Override
- publicboolean verify(String arg0, SSLSession arg1) {
- returntrue;
- }
- });
- } catch (Exception e) {
- e.printStackTrace();
- }
之後就可以使用了:
[java] view plaincopyprint?- String https = "https://www.google.com.hk";
- try {
- HttpsURLConnection conn = (HttpsURLConnection) new URL(https).openConnection();
- conn.setDoOutput(true);
- conn.setDoInput(true);
- conn.connect();
- BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
- StringBuffer sb = new StringBuffer();
- String line;
- while ((line = br.readLine()) != null)
- sb.append(line);
- Log(sb.toString());
- } catch (Exception e) {
- e.printStackTrace();
- }