System.setProperty( "java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol" );
URL url = new URL("https://xxx.xxx.xxx");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
-----------------------------------------------------------------------------------------------
//非受信認的憑證方式
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager(){
public java.security.cert.X509Certificate[] getAcceptedIssuers(){
return null;
}
public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType){}
public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType){}
}};
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
//不判斷Hostname是否正確
HostnameVerifier hv = new HostnameVerifier(){
public boolean verify(String hostname, SSLSession session) {return true;}
};
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(hv);
URL url = new URL("https://xxx.xxx.xxx");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
沒有留言:
張貼留言