HttpsURLConnection 使用样例

private void testHttpConnetion() {
  try {
    //https://icore-pts.pingan.com.cn/ebusiness/login.do
    URL url = new URL("https://icore-pts.pingan.com.cn/ebusiness/login.do");
    HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
    con.setRequestMethod("GET");
    con.setConnectTimeout(30000);
    con.setDoOutput(true);
    con.setDoInput(true);
    con.setUseCaches(false);
    //Host: icorepnbs.pingan.com.cn
    con.setRequestProperty("Host","icorepnbs.pingan.com.cn");
    con.setRequestProperty("Origin","https://icorepnbs.pingan.com.cn");
    //Accept: */*
    con.setRequestProperty("Accept","*/*");
    //Accept-Encoding: gzip, deflate, br
    con.setRequestProperty("Accept-Encoding","gzip, deflate, br");
    //Accept-Language: zh-CN,zh;q=0.9
    con.setRequestProperty("Accept-Language","zh-CN,zh;q=0.9");
    //User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
    con.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
    //X-Requested-With: ShockwaveFlash/29.0.0.171
    con.setRequestProperty("X-Requested-With","ShockwaveFlash/29.0.0.171");
    String cookies = "";
    List<Cookie> cookies1 = getHttpExecutor().getCookieStore().getCookies();
    for (Cookie cookie : cookies1) {
      cookies += cookie.getName() + "=" + cookie.getValue() + ";";
    }

    String substring = cookies.substring(0, cookies.length() - 1);
    con.setRequestProperty("Cookie", substring);

    TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
      public java.security.cert.X509Certificate[] getAcceptedIssuers() {
        return new java.security.cert.X509Certificate[]{};
      }

      public void checkClientTrusted(X509Certificate[] chain, String authType)
          throws CertificateException {
      }

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

    try {
      SSLContext sc = SSLContext.getInstance("TLS");
      sc.init(null, trustAllCerts, new java.security.SecureRandom());
      SSLSocketFactory newFactory = sc.getSocketFactory();
      con.setSSLSocketFactory(newFactory);
    } catch (Exception e) {
      e.printStackTrace();
    }

    int code = con.getResponseCode();
    if(code == 200) {
      System.out.println("调用成功");
      InputStream inputStream2 = con.getInputStream();
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      byte[] data = new byte[2048];
      int len = 0;
      while ((len = inputStream2.read(data)) != -1) {
        bos.write(data, 0, len);
      }
      inputStream2.close();
      String content = bos.toString();
      System.out.println(content);
    }
    StringBuffer buffer = new StringBuffer();
    BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8));
    System.out.println(br.toString());
  } catch (IOException e) {
    e.printStackTrace();
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值