我通过以下方式使用Apache HttpClient 3.1执行Web服务请求:
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秒才能完成?
答案 0 :(得分:2)
连接超时无关紧要。您的状态代码为200,这表示连接成功,您至少读取了一些标题。 读取超时设置为30秒,这意味着服务器在30秒内没有发送任何其他内容。