httpclient4.5.1 怎么实现请求超时重发
发送post请求代码如下经常会请求发不出去卡住,一次卡住后面的请求都发不出去了,怎么解决这个问题,怎么设置超时然后实现重发机制!很急啊!
// 接口地址
private static CloseableHttpClient client =HttpClientBuilder.create().build();
public static String postBody(String url, String parameters) {
String responseString = null;
HttpPost post = new HttpPost(url);
CloseableHttpResponse response=null;
try {
// 建立一个NameValuePair数组,用于存储欲传送的参数
post.addHeader("Content-type","text/xml");
post.setHeader("Accept", "text/xml");
StringEntity entity = new StringEntity(parameters, Charset.forName("UTF-8"));
post.setEntity(entity);
log.info("post start, url: " + url + ", entity: " + entity);
response = client.execute(post);
if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
responseString = EntityUtils.toString(response.getEntity());
}
log.info("post end, url: " + url + ", entity: " + entity + ", response: " + responseString);
//response.close();
} catch (IOException e) {
log.error(e);
e.printStackTrace();
} finally {
}
return responseString;
}