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();
  }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章