• 04_HttpClient发送Https请求


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

     

  • 相关阅读:
    HADOOP docker(六):hive简易使用指南
    HADOOP docker(四):安装hive
    HADOOP docker(二):HDFS 高可用原理
    Python 自用代码(调整日期格式)
    Python 自用代码(某方标准类网页源代码清洗)
    python合并多个csv文件并去重
    Python连接MySQL乱码(中文变问号)
    Shell实现循环执行curl向Solr导入json文件
    Python 自用代码(拆分txt文件)
    shell报错:未预期的符号***附近有语法错误
  • 原文地址:https://www.cnblogs.com/HigginCui/p/6117790.html
Copyright © 2020-2023  润新知