httpclient4.5.1 怎么实现请求超时重发

huyawen123 2017-04-20 10:34:48
发送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;
}
...全文
617 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
大非鱼 2019-12-28
  • 打赏
  • 举报
回复
引用 3 楼 我爱娃哈哈 的回复:
你上面不是有判断httpstatus状态码么,状态码不为正常的时候再发一次就行了啊,就是加一个else就行了
如果有异常的话,response就是空的,根本不会到判断状态码那一步,只能是在外面定义一个计数器,while循环执行
110成成 2017-04-20
  • 打赏
  • 举报
回复
捕获超时异常,在里面进行重发。
huyawen123 2017-04-20
  • 打赏
  • 举报
回复
public static String postBody(String url, String param) { PrintWriter out = null; BufferedReader in = null; String result = ""; try { URL realUrl = new URL(url); URLConnection conn; conn = realUrl.openConnection(); conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestProperty("Content-Type", "text/xml"); out = new PrintWriter(conn.getOutputStream()); out.print(param); out.flush(); in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8")); String line; while ((line = in.readLine()) != null) { result += line; } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e){ e.printStackTrace(); } finally{ try{ if(out!=null){ out.close(); } if(in!=null){ in.close(); } } catch(IOException ex){ ex.printStackTrace(); } } return result; } 我把请求改成这样了!本地测试可以执行,想问下有什么区别!为什么要用上面那种!上面方法的连接好像一直处于没有关闭状态求分析下
huyawen123 2017-04-20
  • 打赏
  • 举报
回复
刚查看日志发现执行这段代码有时候会抛出这样的异常: org.apache.http.impl.execchain.RequestAbortedException: Request aborted at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:193) at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184) at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88) at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110) at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107) at com.glocalme.common.APIHttpClient.postBody(APIHttpClient.java:176) at com.glocalme.application.service.officeial.impl.ProductOrderServiceImpl.createOTAOrder(ProductOrderServiceImpl.java:492) at com.glocalme.quartz.OmsOrderCheck.lambda$sendCommentMailScheduled$0(OmsOrderCheck.java:83) at java.util.ArrayList.forEach(ArrayList.java:1249) at com.glocalme.quartz.OmsOrderCheck.sendCommentMailScheduled(OmsOrderCheck.java:54) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65) at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) Caused by: java.lang.InterruptedException at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.reportInterruptAfterWait(AbstractQueuedSynchronizer.java:2014) at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2048) at org.apache.http.pool.PoolEntryFuture.await(PoolEntryFuture.java:138) at org.apache.http.pool.AbstractConnPool.getPoolEntryBlocking(AbstractConnPool.java:306) at org.apache.http.pool.AbstractConnPool.access$000(AbstractConnPool.java:64) at org.apache.http.pool.AbstractConnPool$2.getPoolEntry(AbstractConnPool.java:192) at org.apache.http.pool.AbstractConnPool$2.getPoolEntry(AbstractConnPool.java:185) at org.apache.http.pool.PoolEntryFuture.get(PoolEntryFuture.java:107) at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.leaseConnection(PoolingHttpClientConnectionManager.java:276) at org.apache.http.impl.conn.PoolingHttpClientConnectionManager$1.get(PoolingHttpClientConnectionManager.java:263) at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:190) ... 25 more 要怎么解决
李德胜1995 2017-04-20
  • 打赏
  • 举报
回复
引用 2 楼 huyawen123 的回复:
能具体给出吗!设置超时时间然后从发代码!就在我的代码基础上加本人小菜!谢谢
写一个循环。。你要重试几次就循环几次,如果请求超时会报错ConnectTimeoutException异常,用try..cacth捕获,重新执行请求,如果没有报错,说明执行成功,直接break;
huyawen123 2017-04-20
  • 打赏
  • 举报
回复
我想让他超时后重发呢
licip 2017-04-20
  • 打赏
  • 举报
回复
while(true){ 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()); break; } log.info("post end, url: " + url + ", entity: " + entity + ", response: " + responseString); //response.close(); } catch (IOException e) { log.error(e); e.printStackTrace(); } finally { } } 在外面套一层while循环,如果请求ok了,就break退出来,如果response.getStatusLine().getStatusCode() != HttpStatus.SC_OK 让循环再继续
我爱娃哈哈 2017-04-20
  • 打赏
  • 举报
回复
你上面不是有判断httpstatus状态码么,状态码不为正常的时候再发一次就行了啊,就是加一个else就行了
huyawen123 2017-04-20
  • 打赏
  • 举报
回复
能具体给出吗!设置超时时间然后从发代码!就在我的代码基础上加本人小菜!谢谢

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧