当前位置: 首页 > news >正文

[实践总结] 使用Apache HttpClient 4.x进行进行一次Http请求

使用Apache HttpClient 4.x进行进行一次Http请求

依赖

<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.7</version>
</dependency>

请求式样

import org.apache.commons.lang3.StringUtils;
import org.apache.http.Consts;
import org.apache.http.HttpStatus;
import org.apache.http.ParseException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;import java.io.IOException;public class HttpPostUtils {/*** @param uri  the request address* @param json the request data that must be a JSON string* @throws IOException    if HTTP connection can not opened or closed successfully* @throws ParseException if response data can not be parsed successfully*/public String retryPostJson(String uri, String json) throws IOException, ParseException {if (StringUtils.isAnyBlank(uri, json)) {return null;}// 构建HttpClientCloseableHttpClient client = HttpClients.custom().setRetryHandler(new RetryHandler()).build();// 构建请求HttpPost httpPost = new HttpPost(uri);// 设置请求体StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);httpPost.setEntity(entity);// 设置超时时间RequestConfig config = RequestConfig.custom().setConnectionRequestTimeout(5000) // 超5秒还没返回新的可用链接,抛ConnectionPoolTimeoutException。默认值为0,表示无限等待。.setConnectTimeout(5000) // 超5秒还没建立链接,抛ConnectTimeoutException。默认值为0,表示无限等待。.setSocketTimeout(5000) // 超5秒还没返回数据,抛SocketTimeoutException。默认值为0,表示无限等待。.build();httpPost.setConfig(config);// ResponseCloseableHttpResponse response = null;String responseContent = null;try {response = client.execute(httpPost, HttpClientContext.create());if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {responseContent = EntityUtils.toString(response.getEntity(), Consts.UTF_8.name());}} finally {if (response != null) {response.close();}if (client != null) {client.close();}}return responseContent;}
}

失败重试机制

import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpRequest;
import org.apache.http.NoHttpResponseException;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.protocol.HttpContext;import javax.net.ssl.SSLException;
import javax.net.ssl.SSLHandshakeException;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.ConnectException;
import java.net.UnknownHostException;public class RetryHandler implements HttpRequestRetryHandler {@Overridepublic boolean retryRequest(IOException exception, int executionCount, HttpContext context) {if (executionCount >= 2) {// 如果已经重试了2次,就放弃// Do not retry if over max retry countreturn false;}if (exception instanceof NoHttpResponseException) {// 如果服务器丢掉了连接,那么就重试return true;}if (exception instanceof SSLHandshakeException) {// 不要重试SSL握手异常return false;}if (exception instanceof SSLException) {// SSL握手异常// SSL handshake exceptionreturn false;}if (exception instanceof ConnectTimeoutException) {// 连接被拒绝return false;}if (exception instanceof InterruptedIOException) {// An input or output transfer has been terminated// 超时return false;}if (exception instanceof UnknownHostException) {// Unknown host 修改代码让不识别主机时重试,实际业务当不识别的时候不应该重试,再次为了演示重试过程,执行会显示retryCount次下面的输出// 目标服务器不可达System.out.println("不识别主机重试");return true;}if (exception instanceof ConnectException) {// Connection refusedreturn false;}HttpClientContext clientContext = HttpClientContext.adapt(context);HttpRequest request = clientContext.getRequest();if (!(request instanceof HttpEntityEnclosingRequest)) {// Retry if the request is considered idempotentreturn true;}return false;}
}

模拟解析返回的Json串

public static List<User> parserJsonToUser() {// 模拟返回的Json串Map<String, List<String>> map = new HashMap<>();map.put("name", Arrays.asList("name1", "name2", "name3"));map.put("age", Arrays.asList("11", "23", "23"));map.put("num", Arrays.asList("123", "234", "345"));String json1 = JsonUtils.toJson(map);System.out.println(json1);// fastjsonJSONObject jsonObject = JSON.parseObject(json1, Feature.OrderedField);// 返回的属性List<String> fields = new ArrayList<>(map.keySet());// 几份值int size = jsonObject.getJSONArray(fields.get(0)).size();List<User> users = new ArrayList<>();for (int i = 0; i < size; i++) {User user1 = new User();for (String field : fields) {Object value = jsonObject.getJSONArray(field).get(i);ReflectUtils.setFieldValue(user1, field, value);}users.add(user1);}System.out.println(users);return users;
}结果
{"num":["123","234","345"],"name":["name1","name2","name3"],"age":["11","23","23"]}
[HttpPostUtils.User(name=name1, age=11, num=123), HttpPostUtils.User(name=name2, age=23, num=234), HttpPostUtils.User(name=name3, age=23, num=345)]

参考

使用Apache HttpClient 4.x进行异常重试

Need 学习消化

二十六、CloseableHttpClient的使用和优化

http://www.lryc.cn/news/254284.html

相关文章:

  • 易宝OA 两处任意文件上传漏洞复现
  • echart饼图高亮颜色设置,数据为0时候,labelLine不显示
  • Kafka 的消息格式:了解消息结构与序列化
  • 装箱 Box 数据类型
  • 多传感器融合SLAM在自动驾驶方向的初步探索的记录
  • ffmpeg与opencv-python处理视频
  • java 操作git
  • Linux 导入、导出 MySQL 数据库命令
  • 华为数通---BFD多跳检测示例
  • AWS 日志分析工具
  • gitLab 和Idea分支合并
  • 关于 mapboxgl 的常用方法及效果
  • C语言——二级指针
  • 股市复苏中的明懿金汇:抓住新机遇
  • Spacemesh、Kaspa和Chia的全面对比!
  • 【HTML语法】
  • ROS报错:RLException:Invalid roslaunch XML Syntax: mismatched tag:
  • C语言实现快速排序
  • ChatGPT对于当今的社会或科技发展有何重要性?
  • 宝塔是可以切换mongodb版本的
  • 16、XSS——会话管理
  • 稀疏矩阵的操作(数据结构实训)
  • sqlite - sqlite3_exec - c++回调函数的处理
  • docker搭建logstash和使用方法
  • Memory-augmented Deep Autoencoder for Unsupervised Anomaly Detection 论文阅读
  • Mac端 DevEco Preview 窗口无法展示,提示文件中的node.dir错误
  • TIMO后台管理系统 Shiro 反序列化漏洞复现
  • 3.4_1 java自制小工具 - pdf批量转图片
  • vue中实现数字+英文字母组合键盘
  • Centos服务器上根据端口号查询jar包,根据jar包查端口号