HttpClient使用不当,服务挂了!是时候系统学习一下了

程序新视界

共 11098字,需浏览 23分钟

 · 2021-12-24

背景

最近发生了两件事,觉得有必要系统的学习一下Apache的HttpClient了。

事件一:联调微信支付接口,用到HttpClient,花时间整理了一番。如果有一篇文章,读一读就可以掌握HttpClient 80%的内容,再有可以直接用的Demo,下次再遇到是不是就可以非常容易集成了?这篇便是这篇文章的目标之一。

事件二:上家公司同事发消息求助,说系统JVM溢出,找不到原因不了。查看了发来的日志文件,基本定位是HttpClient调用三方接口时内存溢出导致的。

无论出于哪种原因,HTTP调用的熟练使用都是必不可少的,今天就来一起系统学习一下,查漏补缺。

HttpClient

HTTP协议的重要性不言而喻,它是现在Internet中使用最多,最重要的协议了。虽然JDK中已经提供了HTTP协议的基本功能,但对于大部分应用来说,这套API还是不够丰富和灵活。

HttpClient是用来编程实现HTTP调用的一款框架,它是Apache Jakarta Common下的子项目,相比传统JDK自带的URLConnection,增加了易用性和灵活性。

HttpClient不仅使客户端发送Http请求变得更加容易,而且也方便了开发人员测试接口(基于Http协议的),即提高了开发的效率,也方便提高代码的健壮性。

目前主流的SpringCloud框架,服务与服务之间的调用也全部是基于HttpClient来实现的。因此,系统的学习一下HttpClient,还是非常有必要的。

HttpClient功能及特性

HttpClient主要提供了以下功能及特性实现:

  • 基于标准、纯净的java语言。实现了HTTP 1.0和HTTP 1.1;
  • 以可扩展的面向对象的结构实现了HTTP全部的方法(GET、 POST、PUT、DELETE、HEAD、OPTIONS、TRACE)等。
  • 支持HTTPS协议。
  • 通过HTTP代理建立透明的连接。
  • 利用CONNECT方法通过HTTP代理建立隧道的HTTPs连接。
  • Basic, Digest, NTLMv1, NTLMv2, NTLM2 Session, SNPNEGO/Kerberos认证方案。
  • 插件式的自定义认证方案。
  • 便携可靠的套接字工厂使它更容易的使用第三方解决方案。
  • 连接管理器支持多线程应用。支持设置最大连接数,同时支持设置每个主机的最大连接数,发现并关闭过期的连接。
  • 自动处理Set-Cookie中的Cookie。
  • 插件式的自定义Cookie策略。
  • Request的输出流可以避免流中内容直接缓冲到Socket服务器。
  • Response的输入流可以有效的从Socket服务器直接读取相应内容。
  • 在HTTP 1.0和HTTP1.1中利用KeepAlive保持持久连接。
  • 直接获取服务器发送的response code和 headers。
  • 设置连接超时的能力。
  • 实验性的支持HTTP1.1 response caching。
  • 源代码基于Apache License 可免费获取。
  • 支持自动(跳转)转向;

关于以上特性,了解即可,用到时再进行深入学习和实践。

HttpClient使用步骤

使用HttpClient来发送请求、接收响应通常有以下步骤:

  • 引入依赖:项目中通过Maven等形式引入HttpClient依赖类库。
  • 创建HttpClient对象。
  • 创建请求方法实例:GET请求创建HttpGet对象,POST请求创建HttpPost对象,并在对象构建时指定请求URL。
  • 设置请求参数:调用HttpGetHttpPost共同的setParams(HetpParams params)方法来添加请求参数;HttpPost也可调用setEntity(HttpEntity entity)方法来设置请求参数。
  • 发送请求:调用HttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个HttpResponse
  • 获取响应结果:调用HttpResponsegetAllHeaders()getHeaders(String name)等方法获取服务器的响应头;调用HttpResponsegetEntity()方法可获取HttpEntity对象,该对象包装了服务器的响应内容。
  • 释放连接:无论执行方法是否成功,都必须释放连接。

以上便是使用HttpClient的核心步骤:引入依赖、创建HttpClient对象、创建请求实例、设置请求参数、发送请求、获取请求结果、释放连接。

文章刚开始提到的事件二,便是由于释放连接不当导致连接累积导致内存溢出。

了解了HttpClient的使用步骤,就可以具体的代码实现了。

实例代码实战

在项目中引入HttpClient依赖:


    org.apache.httpcomponents
    httpclient
    4.5.13

Get请求示例

先以Get请求为例,展示一下调用百度搜索Java关键字:

 @Test
 public void testGet() throws IOException {
  //1、构建HttpClient对象
  CloseableHttpClient httpClient = HttpClients.createDefault();
  //2、创建HttpGet,声明get请求
  HttpGet httpGet = new HttpGet("http://www.baidu.com/s?wd=java");
  //3、发送请求
  CloseableHttpResponse response = httpClient.execute(httpGet);
  //4.判断状态码
  if (response.getStatusLine().getStatusCode() == 200) {
   HttpEntity entity = response.getEntity();
   // 使用工具类EntityUtils,从响应中取出实体表示的内容并转换成字符串
   String string = EntityUtils.toString(entity, "utf-8");
   System.out.println(string);
  }
  // 5、关闭资源
  response.close();
  httpClient.close();
 }

执行上述代码,HttpClient调用成功,控制台会打印出百度返回结果的HTML信息。这个过程也遵循了上面说到的HttpClient的使用步骤。

上述代码看似能够正常使用,但在执行的过程中如果出现异常,则会出现连接无法正常释放,导致内存溢出问题

对上述代码进行改进:

 @Test
 public void testGet() {

  CloseableHttpClient httpClient = null;
  CloseableHttpResponse response = null;

  try {
   //1、构建HttpClient对象
   httpClient = HttpClients.createDefault();
   //2、创建HttpGet,声明get请求
   HttpGet httpGet = new HttpGet("http://www.baidu.com/s?wd=java");
   //3、发送请求
   response = httpClient.execute(httpGet);
   //4.判断状态码
   if (response.getStatusLine().getStatusCode() == 200) {
    HttpEntity entity = response.getEntity();
    // 使用工具类EntityUtils,从响应中取出实体表示的内容并转换成字符串
    String string = EntityUtils.toString(entity, "utf-8");
    System.out.println(string);
   }
  } catch (Exception e) {
   // 打印堆栈信息,进行异常情况处理;
  } finally {
   // 5、关闭资源
   if (response != null) {
    try {
     response.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
   if (httpClient != null) {
    try {
     httpClient.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
 }

虽然代码复杂了一些,但此时无论是否出现异常,都可以将连接进行正常的关闭,避免内存溢出。

在上述代码中,其中HttpGet的参数是直接拼接到HTTP连接后面的,当然也可以通过URI来构建,代码实现如下:

HttpGet httpGet = new HttpGet("http://www.baidu.com/s?wd=java");

// 上述实现等价于下面的实现;

URI uri = new URIBuilder("http://www.baidu.com/s").setParameter("wd","java").build();
HttpGet httpGet = new HttpGet(uri);

当然,针对资源释放部分,还可以利用Java 8提供的try-with-resources语法糖来进行简化代码。

Post请求示例

下面的实例中的Post请求相对Get请求,多了添加Header参数和Http的Entity参数:

 @Test
 public void testPost(){
  CloseableHttpClient httpClient = null;
  CloseableHttpResponse response = null;
  try {
   //1.打开浏览器
   httpClient = HttpClients.createDefault();
   //2.声明get请求
   HttpPost httpPost = new HttpPost("https://www.oschina.net/");
   //3.网站为了防止恶意攻击,在post请求中都限制了浏览器才能访问
   httpPost.addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36");
   //4.判断状态码
   List parameters = new ArrayList<>(0);
   parameters.add(new BasicNameValuePair("scope""project"));
   parameters.add(new BasicNameValuePair("q""java"));

   UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters,"UTF-8");
   httpPost.setEntity(formEntity);
   //5.发送请求
   response = httpClient.execute(httpPost);
   if(response.getStatusLine().getStatusCode()==200){
    HttpEntity entity = response.getEntity();
    String string = EntityUtils.toString(entity, "utf-8");
    System.out.println(string);
   }
  } catch (Exception e){
   // 打印堆栈信息,进行异常情况处理;
  } finally {
   // 5、关闭资源
   if (response != null) {
    try {
     response.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
   if (httpClient != null) {
    try {
     httpClient.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
 }

Post请求部分与Get请求的关键区别在于构建的请求对象不同,传输的参数不再局限于URL的拼接,还可以基于Entity来进行传输。我们在实践的过程中,大多数也是将数据放在Entity中基于JSON等格式进行传输。

HttpClient 超时配置

正常来说上面的代码已经基本满足了业务需求,但还是有需要完善的地方,特别是针对HTTP请求超时情况的处理。

HttpClient对此提供了setConfig(RequestConfig config)方法来为请求配置超时时间等,部分核心代码如下:

// 设置配置请求参数(没有可忽略)
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 连接主机服务超时时间
 .setConnectionRequestTimeout(35000)// 请求超时时间
 .setSocketTimeout(60000)// 数据读取超时时间
 .build();
// 为httpGet实例设置配置
httpGet.setConfig(requestConfig);

关于上述配置的重要性,也是不容忽视的。否则可能会导致请求阻塞,影响性能等问题。

HttpClient工具类封装

看完上述使用,是不是发现HttpClient的使用非常简单、便捷?其实,还可以根据具体是使用场景,进一步进行封装,封装成工具类,业务使用时直接调用即可。

关于HttpClientUtil的封装有很多方式,这里提供一种封装,仅供参考:

import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * http请求客户端
 *
 * @author zzs
 */
public class HttpClientUtil {

 private static RequestConfig requestConfig = null;

 private HttpClientUtil() {
 }

 static {
  //设置http的状态参数
  requestConfig = RequestConfig.custom()
    .setSocketTimeout(5000)
    .setConnectTimeout(5000)
    .setConnectionRequestTimeout(5000)
    .build();
  // TODO 补充其他配置
 }

 public static String doGet(String url, Map param) {
  // 创建Httpclient对象
  CloseableHttpClient httpClient = HttpClients.createDefault();

  String resultString = "";
  CloseableHttpResponse response = null;
  try {
   // 创建uri
   URIBuilder builder = new URIBuilder(url);
   if (param != null) {
    for (String key : param.keySet()) {
     builder.addParameter(key, param.get(key));
    }
   }
   URI uri = builder.build();
   // 创建http GET请求
   HttpGet httpGet = new HttpGet(uri);
   httpGet.setConfig(requestConfig);
   // 执行请求
   response = httpClient.execute(httpGet);
   // 判断返回状态是否为200
   if (response.getStatusLine().getStatusCode() == 200) {
    resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
   }
  } catch (Exception e) {
   // TODO 完善异常处理
   e.printStackTrace();
  } finally {
   try {
    if (response != null) {
     response.close();
    }
    if (httpClient != null) {
     httpClient.close();
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  return resultString;
 }

 public static String doGet(String url) {
  return doGet(url, null);
 }

 public static String doPost(String url, Map param) {
  // 创建Httpclient对象
  CloseableHttpClient httpClient = HttpClients.createDefault();
  CloseableHttpResponse response = null;
  String resultString = "";
  try {
   // 创建Http Post请求
   HttpPost httpPost = new HttpPost(url);
   httpPost.setConfig(requestConfig);
   // 创建参数列表
   if (param != null) {
    List paramList = new ArrayList<>();
    for (String key : param.keySet()) {
     paramList.add(new BasicNameValuePair(key, (String) param.get(key)));
    }
    // 模拟表单
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);
    httpPost.setEntity(entity);
   }
   // 执行http请求
   response = httpClient.execute(httpPost);
   resultString = EntityUtils.toString(response.getEntity(), "utf-8");
  } catch (Exception e) {
   // TODO 完善异常处理
   e.printStackTrace();
  } finally {
   try {
    if (response != null) {
     response.close();
    }
    if (httpClient != null) {
     httpClient.close();
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
  }

  return resultString;
 }

 public static String doPost(String url) {
  return doPost(url, null);
 }

 public static String doPostJson(String url, String json, String token_header) throws Exception {
  // 创建Httpclient对象
  CloseableHttpClient httpClient = HttpClients.createDefault();
  CloseableHttpResponse response = null;
  String resultString = "";
  try {
   // 创建Http Post请求
   HttpPost httpPost = new HttpPost(url);
   httpPost.setConfig(requestConfig);
   // 创建请求内容
   httpPost.setHeader("HTTP Method""POST");
   httpPost.setHeader("Connection""Keep-Alive");
   httpPost.setHeader("Content-Type""application/json;charset=utf-8");
   httpPost.setHeader("x-authentication-token", token_header);

   StringEntity entity = new StringEntity(json);

   entity.setContentType("application/json;charset=utf-8");
   httpPost.setEntity(entity);

   // 执行http请求
   response = httpClient.execute(httpPost);
   if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
    resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
   }
  } catch (Exception e) {
   // TODO 完善异常处理
   e.printStackTrace();
  } finally {
   try {
    if (response != null) {
     response.close();
    }
    if (httpClient != null) {
     httpClient.close();
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  return resultString;
 }
}

上述代码满足了基本的功能,如果有特殊功能则可进一步扩展。同时,static代码块中可进一步完善RequestConfig的参数配置和其他配置的初始化。另外,针对异常处理部分,也看根据具体的业务场景选择:直接抛出异常、打印日志、抛出自定义异常等方式进行处理。

小结

本篇文章我们学习了HttpClient及其基本使用,同时以代码的形式展示了最佳实践、封装、改进以及其中会遇到的问题。掌握本篇内容基本可以满足80%的日常使用场景了。当然,还有一些针对HTTPs请求、连接池配置、异步处理等特定使用,则需要读者在实践的过程中有针对性的自行探索了。

往期推荐

亲身经历,大龄程序员找工作,为什么这么难!

CentOS 将于年底终止!Linux服务器该何去何从?

我在美团的八年

大厂都在用EhCache,它到底比Redis强在哪里?

Dao,Service,Controller,Util,Model代表着什么,为什么这么划分?

Log4j史诗级漏洞,从原理到实战,只用3个实例就搞明白!


如果你觉得这篇文章不错,那么,下篇通常会更好。添加微信好友,可备注“加群”(微信号:zhuan2quan)

一篇文章就看透技术本质的人,
  和花一辈子都看不清的人,
  注定是截然不同的搬砖生涯。
▲ 按关注”程序新视界“,洞察技术内幕
浏览 40
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报