HttpClient如何进行ssl连接呢?

[复制链接]
发表于 2022-8-31 08:43:47 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
转自:
http://www.java265.com/JavaCourse/202204/2939.html
HttpClient是一个java语言编写的包,
我们使用HttpClient可以非常方便的发送Http请求
它使基于Http协议请求内容变得非常简单
HttpClient是Apache Jakarta Common下的子项目 它里面封装了很多使用http协议访问的工具,可用于高效访问http
 
下文笔者讲述基于HttpClient进行ss的示例分享,如下所示:
  1. HttpClient进行ssl连接的示例分享,如下所示
  2. 实现思路:
  3.     1.定义一个keyStore对象,并读取证书信息
  4.         2.声明一个SSLContext
  5.         3.定义一个SSLConnectionSocketFactory工厂
  6.         4.定义一个HttpGet请求体
  7.         5.execute执行运行
  8.         6.获取返回信息
  9.         7.关闭连接
复制代码
  1.   /**
  2.      * HttpClient连接SSL
  3.      */   
  4.     public void ssl() {   
  5.         CloseableHttpClient httpclient = null;   
  6.         try {   
  7.             KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());   
  8.             FileInputStream instream = new FileInputStream(new File("d:\\tomcat.keystore"));   
  9.             try {   
  10.                 // 加载keyStore d:\\tomcat.keystore     
  11.                 trustStore.load(instream, "123456".toCharArray());   
  12.             } catch (CertificateException e) {   
  13.                 e.printStackTrace();   
  14.             } finally {   
  15.                 try {   
  16.                     instream.close();   
  17.                 } catch (Exception ignore) {   
  18.                 }   
  19.             }   
  20.             // 相信自己的CA和所有自签名的证书   
  21.             SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(trustStore, new TrustSelfSignedStrategy()).build();   
  22.             // 只允许使用TLSv1协议   
  23.             SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null,   
  24.                     SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);   
  25.             httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();   
  26.             // 创建http请求(get方式)   
  27.             HttpGet httpget = new HttpGet("https://www.java265.com");   
  28.             System.out.println("executing request" + httpget.getRequestLine());   
  29.             CloseableHttpResponse response = httpclient.execute(httpget);   
  30.             try {   
  31.                 HttpEntity entity = response.getEntity();   
  32.                 System.out.println("----------------------------------------");   
  33.                 System.out.println(response.getStatusLine());   
  34.                 if (entity != null) {   
  35.                     System.out.println("Response content length: " + entity.getContentLength());   
  36.                     System.out.println(EntityUtils.toString(entity));   
  37.                     EntityUtils.consume(entity);   
  38.                 }   
  39.             } finally {   
  40.                 response.close();   
  41.             }   
  42.         } catch (ParseException e) {   
  43.             e.printStackTrace();   
  44.         } catch (IOException e) {   
  45.             e.printStackTrace();   
  46.         } catch (KeyManagementException e) {   
  47.             e.printStackTrace();   
  48.         } catch (NoSuchAlgorithmException e) {   
  49.             e.printStackTrace();   
  50.         } catch (KeyStoreException e) {   
  51.             e.printStackTrace();   
  52.         } finally {   
  53.             if (httpclient != null) {   
  54.                 try {   
  55.                     httpclient.close();   
  56.                 } catch (IOException e) {   
  57.                     e.printStackTrace();   
  58.                 }   
  59.             }   
  60.         }   
  61.     }
复制代码
参考资料:http://www.java265.com/JavaCourse/202204/2934.html HttpUtils工具类
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表