【實例 帶Cookie訪問HTTPS類型的 建信基金 的某一頁面)】
/** * 創建一個可以訪問Https類型URL的工具類,返回一個CloseableHttpClient實例 */ public static CloseableHttpClient createSSLClientDefault(){ try { SSLContext sslContext=new SSLContextBuilder().loadTrustMaterial( null,new TrustStrategy() { //信任所有 public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { return true; } }).build(); SSLConnectionSocketFactory sslsf=new SSLConnectionSocketFactory(sslContext); return HttpClients.custom().setSSLSocketFactory(sslsf).build(); } catch (KeyManagementException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (KeyStoreException e) { e.printStackTrace(); } return HttpClients.createDefault(); } /** * @throws IOException * @throws ClientProtocolException * */ public static void main(String[] args) throws ClientProtocolException, IOException { //從工具方法中獲得對應的可以訪問Https的httpClient CloseableHttpClient httpClient =createSSLClientDefault(); HttpGet httpGet=new HttpGet("https://etrade.ccbfund.cn/etrading/tradereq/main.do?method=doInit&isHome=1&menuId=10000"); //自己先在瀏覽器登錄一下,自行復制具體的Cookie httpGet.setHeader("Cookie", "HS_ETS_SID=4jSFY2wWwT0gPrWJ45ly!-1286216704; Null=31111111.51237.0000; logtype=2; certtype=0; certNo=33****************; isorgloginpage_cookie=0; hs_etrading_customskin=app_css"); //設置代理,方便Fiddle捕獲具體信息 RequestConfig config=RequestConfig.custom() .setProxy(HttpHost.create("127.0.0.1:8888")) .build(); httpGet.setConfig(config); //執行get請求,獲得對應的響應實例 CloseableHttpResponse response=httpClient.execute(httpGet); //打印響應的到的html正文 HttpEntity entity =response.getEntity(); String html=EntityUtils.toString(entity); System.out.println(html); //關閉連接 response.close(); httpClient.close(); }
【Fiddler中抓取的內容如下】
【正常登錄訪問https://etrade.ccbfund.cn/etrading/tradereq/main.do?method=doInit&isHome=1&menuId=10000的頁面如下】
【未登錄情況訪問https://etrade.ccbfund.cn/etrading/tradereq/main.do?method=doInit&isHome=1&menuId=10000的頁面如下】