HttpClient 如何设置请求接口等待时间
程序员文章站
2024-03-23 10:40:34
...
HttpClient 如何设置请求接口等待时间
结合接口日志的打印,我判断是第二种现象,就是接口还没有返回信息,我程序这边就出现了超时的报错。那么如何解决,我们只有增加接口的请求等待时间
问题解决:
private static Logger logger = LoggerFactory.getLogger(HttpUtil.class);
public static String postGeneralUrl(String url, String contentType, String jsonParam, String encoding, int reSend) {
logger.info("当前请求接口地址:{}",jsonParam.toString());
// 声明返回结果
String result = "";
// 开始请求API接口时间
long startTime = System.currentTimeMillis();
// 请求API接口的响应时间
long endTime = 0L;
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
HttpClient httpClient = null;
try {
contentType = contentType == null ? "application/x-www-form-urlencoded" : contentType;
// 创建连接
httpClient = HttpClientFactory.getInstance().getHttpClient();
// 设置请求头和报文
HttpPost httpPost = HttpClientFactory.getInstance().httpPost(url);
Header header = new BasicHeader("Accept-Encoding", null);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build();
httpPost.setConfig(requestConfig);
httpPost.addHeader(header);
httpPost.addHeader("Content-Type", contentType);
List<NameValuePair> list = new LinkedList<>();
list.add(new BasicNameValuePair("params", jsonParam));// pageHelper放在body中传过去
// 使用URL实体转换工具
UrlEncodedFormEntity entityParam = new UrlEncodedFormEntity(list, "UTF-8"); // 使用 UrlEncodedFormEntity 来设置
// // body,消息体内容
httpPost.setEntity(entityParam);
logger.info("请求{}接口的参数为{}", url, jsonParam);
httpResponse = httpClient.execute(httpPost);
httpEntity = httpResponse.getEntity();
result = EntityUtils.toString(httpEntity);
} catch (SocketTimeoutException es) {
endTime = System.currentTimeMillis();
result = "网络连接异常,请检查网络或者开票服务是否正常启动";
logger.info("请求{}接口的响应报文内容为{},本次请求API接口的响应时间为:{}毫秒", url, result, (endTime - startTime));
logger.error("请求{}接口出现异常", url, es);
return result;
} catch (Exception e) {
logger.error("请求{}接口出现异常", url, e);
} finally {
try {
EntityUtils.consume(httpEntity);
} catch (IOException e) {
e.printStackTrace();
}
}
// 请求接口的响应时间
endTime = System.currentTimeMillis();
logger.info("请求{}接口的响应报文内容为{},本次请求API接口的响应时间为:{}毫秒", url, result, (endTime - startTime));
return result;
}
其中主要是这部分代码:
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(20000).setConnectTimeout(20000).build();
httpPost.setConfig(requestConfig);
当然这个设置和使用的 http.client 版本也有一定的关系
当前我使用的版本是:
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
<exclusions>
<exclusion>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</exclusion>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
推荐阅读
-
HttpClient 如何设置请求接口等待时间
-
使用Apache提供的HttpClient对象调用HTTP接口(基于POST请求)
-
thinkphp页面跳转(successerror)如何设置跳转等待时间
-
如何设置Fiddler来拦截Java代码发送的HTTP请求,进行各种问题排查 fiddlejavahttp抓包
-
如何设置双操作系统的默认启动及等待时间
-
如何设置Fiddler来拦截Java代码发送的HTTP请求,进行各种问题排查
-
thinkphp页面跳转(successerror)如何设置跳转等待时间
-
使用HttpClient发送post和get请求三方接口
-
java如何设置http请求头、请求头作用、idea测试请求等
-
java用 httpclient请求一般的mvc接口