现在的位置: 首页 > 综合 > 正文

java通过代理访问外网https

2013年09月19日 ⁄ 综合 ⁄ 共 1491字 ⁄ 字号 评论关闭

//通过代理访问外网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;
  }
  }

抱歉!评论已关闭.