HttpsURLConnection请求https,抛出javax.net.ssl.SSLException: hostname in certificate didn't match:

解决办法:

try {
	SSLContext sc = SSLContext.getInstance("TLS");
	sc.init(null, new TrustManager[]{new X509TrustManager() {
		@Override
		public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
		}

		@Override
		public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
		}

		@Override
		public X509Certificate[] getAcceptedIssuers() {
			return null;
		}
	}}, new SecureRandom());

	HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
	HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
		@Override
		public boolean verify(String arg0, SSLSession arg1) {
			return true;
		}
	});
} catch (Exception e) {
	e.printStackTrace();
}



你可能感兴趣的:(Android)