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(); } }
HttpsURLConnection 使用样例
最新推荐文章于 2020-12-07 14:35:33 发布