如题Hi,各位朋友:
    我遇到一个问题是关于HttpClient自动关闭了连接,部分代码如下:method.addRequestHeader("connection","keep-alive")
method.setRequestHeader("Cookie","JUCSSESSIONID="+JUCSSESSIONID);
LOGGER.info("JUCSSESSIONID="+JUCSSESSIONID);
int statusCode = client.executeMethod(method);
LOGGER.info(statusCode);
if(statusCode == HttpStatus.SC_OK){
    LOGGER.info(method.getResponseBodyAsString());
    return method.getResponseBodyAsString();
}else{
    return null;
}
打印的日志如下(变量statusCode没有在日志中打印出来,连接关闭了):[2013-10-28 08:58:00] INFO  [com.epay.utils.sso.UCSUtils] (UCSUtils.java:81) - JUCSSESSIONID=00005Z-xL_aZyM4Ah0p3P2MWgOA:17qh2jfr0
[2013-10-28 08:58:00] DEBUG [org.apache.commons.httpclient.HttpConnection] (HttpConnection.java:692) - Open connection to js.10086.cn:80
[2013-10-28 08:58:00] DEBUG [org.apache.commons.httpclient.HttpMethodDirector] (HttpMethodDirector.java:404) - Closing the connection.
[2013-10-28 08:58:00] DEBUG [org.apache.commons.httpclient.HttpMethodDirector] (HttpMethodDirector.java:434) - Method retry handler returned false. Automatic recovery will not be attempted
[2013-10-28 08:58:00] DEBUG [org.apache.commons.httpclient.HttpConnection] (HttpConnection.java:1178) - Releasing connection back to connection manager.
----------------------------------------
请问HttpClient为什么会关闭连接呢?谢谢了!!!HttpClientWeb

解决方案 »

  1.   

    有点奇怪啊。你那个method里设置哪些东西?
      

  2.   

    if (method instanceof HttpMethodBase) {
                             MethodRetryHandler handler = 
                               ((HttpMethodBase)method).getMethodRetryHandler();
                            if (handler != null) {
                                if (!handler.retryMethod(
                                        method,
                                        this.conn, 
                                        new HttpRecoverableException(e.getMessage()),
                                        execCount, 
                                        method.isRequestSent())) {
                                    LOG.debug("Method retry handler returned false. "
                                             + "Automatic recovery will not be attempted");
                                    throw e;
                                 }
                            }
                        }
                        // ========================================
                        HttpMethodRetryHandler handler = 
                             (HttpMethodRetryHandler)method.getParams().getParameter(
                                    HttpMethodParams.RETRY_HANDLER);
                        if (handler == null) {
                             handler = new DefaultHttpMethodRetryHandler();
                        }
                        if (!handler.retryMethod(method, e, execCount)) {
                             LOG.debug("Method retry handler returned false. "
                                    + "Automatic recovery will not be attempted");
                            throw e;
                        }看异常是handler.retryMethod方法返回false了。估计连接超时了吧。看看你那网站有连接时间限制没。
      

  3.   

    楼上正解。补充下 IOException 才会retry,应该是超时了,或者网络不稳定。
     } catch (HttpException e) {
                        // filter out protocol exceptions which cannot be recovered from
                        throw e;
      } catch (IOException e) {
                        LOG.debug("Closing the connection.");
                        this.conn.close();
      

  4.   

    没有什么设置,就“HttpClient client=new HttpClient();”
      

  5.   

    终于找到一个与此相关的帖子:
    http://seanhe.iteye.com/blog/234759