Apache HttpClient 3.1 套接字超时

Apache HttpClient 3.1 socket timeout

我正在通过以下方式使用 Apache HttpClient 3.1 执行网络服务请求:

private MultiThreadedHttpConnectionManager cm = null;
private HttpClient client = null;

// Setting up connection manager during init process
cm = new MultiThreadedHttpConnectionManager();
int connectionTimeout = 12000; // it actually comes from config file, but this is the current value
cm.getParams().setConnectionTimeout(connectionTimeout);
cm.getParams().setTcpNoDelay(true);
client = new HttpClient(cm);

// method is prepared with request data earlier
int socketTimeout = 30000; // it actually comes from config file, but this is the current value
method.getParams().setSoTimeout(socketTimeout);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false));

logger.info("Starting request");
int statusCode = client.executeMethod(method);
logger.info("StatusCode = " + statusCode);
String response = method.getResponseBodyAsString();
logger.info("Response = " + response);

日志文件中的时间戳如下:

2017-05-02 08:50:03,881 Starting request
2017-05-02 08:50:16,680 StatusCode = 200
2017-05-02 08:50:46,708 java.net.SocketTimeoutException

因此,即使连接超时设置为 12 秒,executeMethod 调用也花费了将近 13 秒。我对此感到困惑,因为此处的文档不清楚:说 executeMethod 和 getResponseBodyAsString 都分别使用套接字超时是否正确?因此,在这种情况下,套接字超时为 30 秒,那么理论上执行是否需要 60 秒才能完成?

连接超时无关紧要。您获得了状态代码 200,这意味着连接成功并且您至少读取了一些 headers。 read 超时设置为 30 秒,这意味着服务器在 30 秒内没有发送任何其他内容。