当前位置:   article > 正文

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

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

上篇文章说道httpclient不能直接访问https的资源,这次就来模拟一下环境,然后配置https测试一下。在前面的文章中,分享了一篇自己生成并在tomcat中配置ssl的文章《Tomcat配置SSL》,大家可以据此来在本地配置https。我已经配置好了,效果是这样滴:

可以看到已经信任该证书(显示浅绿色小锁),浏览器可以正常访问。现在我们用代码测试一下:

  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("-----------------------------------");
  7. }

 

 

发现抛出了异常,我知道的有两种方案(也许还有我不知道的方案),这里介绍第一种方案,也是用的比较多的方案——绕过证书验证。直接看代码吧:

  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. // 实现一个X509TrustManager接口,用于绕过验证,不用修改里面的方法
  11. X509TrustManager trustManager = new X509TrustManager() {
  12. @Override
  13. public void checkClientTrusted(
  14. java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
  15. String paramString) throws CertificateException {
  16. }
  17. @Override
  18. public void checkServerTrusted(
  19. java.security.cert.X509Certificate[] paramArrayOfX509Certificate,
  20. String paramString) throws CertificateException {
  21. }
  22. @Override
  23. public java.security.cert.X509Certificate[] getAcceptedIssuers() {
  24. return null;
  25. }
  26. };
  27. sc.init(null, new TrustManager[] { trustManager }, null);
  28. return sc;
  29. }

然后修改原来的send方法:

  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. // 设置协议http和https对应的处理socket链接工厂的对象
  18. Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
  19. .register("http", PlainConnectionSocketFactory.INSTANCE)
  20. .register("https", new SSLConnectionSocketFactory(sslcontext))
  21. .build();
  22. PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
  23. HttpClients.custom().setConnectionManager(connManager);
  24. //创建自定义的httpclient对象
  25. CloseableHttpClient client = HttpClients.custom().setConnectionManager(connManager).build();
  26. // CloseableHttpClient client = HttpClients.createDefault();
  27. //创建post方式请求对象
  28. HttpPost httpPost = new HttpPost(url);
  29. //装填参数
  30. List<NameValuePair> nvps = new ArrayList<NameValuePair>();
  31. if(map!=null){
  32. for (Entry<String, String> entry : map.entrySet()) {
  33. nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
  34. }
  35. }
  36. //设置参数到请求对象中
  37. httpPost.setEntity(new UrlEncodedFormEntity(nvps, encoding));
  38. System.out.println("请求地址:"+url);
  39. System.out.println("请求参数:"+nvps.toString());
  40. //设置header信息
  41. //指定报文头【Content-type】、【User-Agent】
  42. httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
  43. httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
  44. //执行请求操作,并拿到结果(同步阻塞)
  45. CloseableHttpResponse response = client.execute(httpPost);
  46. //获取结果实体
  47. HttpEntity entity = response.getEntity();
  48. if (entity != null) {
  49. //按指定编码转换结果实体为String类型
  50. body = EntityUtils.toString(entity, encoding);
  51. }
  52. EntityUtils.consume(entity);
  53. //释放链接
  54. response.close();
  55. return body;
  56. }

 

现在再进行测试,发现果然通了。

 

 

下篇介绍另一种方案,应对自己生成的证书,敬请期待。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/451282
推荐阅读
相关标签
  

闽ICP备14008679号