//通過代理訪問外網https
public class Test99999 extends Thread{
public static void main(String[] args) throws Exception {
//設定代理
String proxy = "openproxy.huawei.com";
int port = 8080;
System.setProperty("proxyType", "4");
System.setProperty("proxyPort", Integer.toString(port));
System.setProperty("proxyHost", proxy);
System.setProperty("proxySet", "true");
String url="https://www.smics.com/";
SSLContext sc = SSLContext.getInstance("SSL");
//指定信任https
sc.init(null, new TrustManager[]{new TrustAnyTrustManager()}, new java.security.SecureRandom());
URL console = new URL(url);
HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();
conn.setSSLSocketFactory(sc.getSocketFactory());
conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
conn.connect();
System.out.println("返回結果:"+conn.getResponseMessage());
InputStream is = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String curLine="";
while ((curLine = reader.readLine()) != null) {
System.out.println(curLine);
}
is.close();
}
private static class TrustAnyTrustManager implements X509TrustManager {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[]{};
}
}
private static class TrustAnyHostnameVerifier implements HostnameVerifier {
public boolean verify(String hostname, SSLSession session) {
return true;
}
}