zoukankan      html  css  js  c++  java
  • 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的页面如下】

     

  • 相关阅读:
    01-NoSQL概述
    SSM快速整合
    C语言指针传参与C++引用传参,以及尾插法建立单链表使用到的引用
    IP地址相关
    二叉树的先序遍历、中序遍历、后序遍历-C语言描述
    华为5G认证练习题2
    华为5G认证练习题
    华为ICT学堂获取练习题及答案
    C++ cin对象的一些方法
    webpack学习笔记2:新建工程
  • 原文地址:https://www.cnblogs.com/HigginCui/p/6117790.html
Copyright © 2011-2022 走看看