【实例 带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的页面如下】