【发布时间】:2014-06-11 12:26:11
【问题描述】:
我正在使用HttpClient 向网络服务发送SOAP 请求以查询一些数据。对于某些网络服务参数,网络服务的执行时间超过 5 分钟,5 分钟后我得到一个java.net.SocketException: Connection reset。
我认为发生错误是因为连接空闲超过 5 分钟,然后防火墙限制了连接。
有没有办法为 http post 请求发送 keep-alive 包或保持连接活动的东西?(如果可能,我需要客户端解决方案)
如果您在 Google 上搜索 HttpClient keep-alive,您会发现很多关于重用连接的主题。在我的情况下,我只想保持连接直到我得到响应。
执行 SOAP 请求的方法:
def executeSOAPRequest(String url, String content, String soapAction, Integer timeout) {
def retVal = new SoapResponse();
PostMethod post = new PostMethod(url);
RequestEntity entity = new StringRequestEntity(content,"ISO-8859-1","ISO-8859-1");
post.setRequestEntity(entity);
post.setRequestHeader("SOAPAction", soapAction);
HttpClient httpclient = new HttpClient()
httpclient.setTimeout(timeout)
try {
retVal.httpResponse = httpclient.executeMethod(post);
retVal.httpResponseBody = post.getResponseBodyAsString();
} catch(Exception e){
... exception handling ...
} finally {
... finally stuff ...
}
return retVal;
}
目前使用的是HttpClient v3.1。
【问题讨论】:
-
@tim_yates 看来keep alive策略是基于
HttpResponse(public long getKeepAliveDuration(HttpResponse response, HttpContext context) 但是如果我还没有得到回应呢?
标签: web-services soap groovy apache-commons-httpclient connection-reset