HttpClient之配置ssl,采用绕过证书验证实现https

网址:http://blog.csdn.net/xiaoxian8023/article/details/49865335





  1. public static void main(String[] args) throws ParseException, IOException, KeyManagementException, NoSuchAlgorithmException, HttpProcessException {  
  2.     String url = "https://sso.tgb.com:8443/cas/login";  
  3.     String body = send(url, null"utf-8");  
  4.     System.out.println("交易响应结果:");  
  5.     System.out.println(body);  
  6.     System.out.println("-----------------------------------");  


  1. /** 
  2.  * 绕过验证 
  3.  *   
  4.  * @return 
  5.  * @throws NoSuchAlgorithmException  
  6.  * @throws KeyManagementException  
  7.  */  
  8. public static SSLContext createIgnoreVerifySSL() throws NoSuchAlgorithmException, KeyManagementException {  
  9.     SSLContext sc = SSLContext.getInstance("SSLv3");  
  10.   
  11.     // 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法  
  12.     X509TrustManager trustManager = new X509TrustManager() {  
  13.         @Override  
  14.         public void checkClientTrusted(  
  15.                 java.security.cert.X509Certificate[] paramArrayOfX509Certificate,  
  16.                 String paramString) throws CertificateException {  
  17.         }  
  18.   
  19.         @Override  
  20.         public void checkServerTrusted(  
  21.                 java.security.cert.X509Certificate[] paramArrayOfX509Certificate,  
  22.                 String paramString) throws CertificateException {  
  23.         }  
  24.   
  25.         @Override  
  26.         public java.security.cert.X509Certificate[] getAcceptedIssuers() {  
  27.             return null;  
  28.         }  
  29.     };  
  30.   
  31.     sc.init(nullnew TrustManager[] { trustManager }, null);  
  32.     return sc;  

  1. /** 
  2.  * 模拟请求 
  3.  *  
  4.  * @param url       资源地址 
  5.  * @param map   参数列表 
  6.  * @param encoding  编码 
  7.  * @return 
  8.  * @throws NoSuchAlgorithmException  
  9.  * @throws KeyManagementException  
  10.  * @throws IOException  
  11.  * @throws ClientProtocolException  
  12.  */  
  13. public static String send(String url, Map<String,String> map,String encoding) throws KeyManagementException, NoSuchAlgorithmException, ClientProtocolException, IOException {  
  14.     String body = "";  
  15.     //采用绕过验证的方式处理https请求  
  16.     SSLContext sslcontext = createIgnoreVerifySSL();  
  17.       
  18.        // 设置协议http和https对应的处理socket链接工厂的对象  
  19.        Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()  
  20.            .register("http", PlainConnectionSocketFactory.INSTANCE)  
  21.            .register("https"new SSLConnectionSocketFactory(sslcontext))  
  22.            .build();  
  23.        PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);  
  24.        HttpClients.custom().setConnectionManager(connManager);  
  25.   
  26.        //创建自定义的httpclient对象  
  27.     CloseableHttpClient client = HttpClients.custom().setConnectionManager(connManager).build();  
  28. /       CloseableHttpClient client = HttpClients.createDefault();  
  29.       
  30.     //创建post方式请求对象  
  31.     HttpPost httpPost = new HttpPost(url);  
  32.       
  33.     //装填参数  
  34.     List<NameValuePair> nvps = new ArrayList<NameValuePair>();  
  35.     if(map!=null){  
  36.         for (Entry<String, String> entry : map.entrySet()) {  
  37.             nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));  
  38.         }  
  39.     }  
  40.     //设置参数到请求对象中  
  41.     httpPost.setEntity(new UrlEncodedFormEntity(nvps, encoding));  
  42.   
  43.     System.out.println("请求地址:"+url);  
  44.     System.out.println("请求参数:"+nvps.toString());  
  45.       
  46.     //设置header信息  
  47.     //指定报文头【Content-type】、【User-Agent】  
  48.     httpPost.setHeader("Content-type""application/x-www-form-urlencoded");  
  49.     httpPost.setHeader("User-Agent""Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");  
  50.       
  51.     //执行请求操作,并拿到结果(同步阻塞)  
  52.     CloseableHttpResponse response = client.execute(httpPost);  
  53.     //获取结果实体  
  54.     HttpEntity entity = response.getEntity();  
  55.     if (entity != null) {  
  56.         //按指定编码转换结果实体为String类型  
  57.         body = EntityUtils.toString(entity, encoding);  
  58.     }  
  59.     EntityUtils.consume(entity);  
  60.     //释放链接  
  61.     response.close();  
  62.        return body;  
  63. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值