2

我正在使用 Apache HttpClient 4.2.5,需要将连接超时设置为 30 秒。我执行以下操作:

int timeout = 30 * 1000;
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, timeout);
HttpConnectionParams.setSoTimeout(params, timeout);
HttpClient client = new DefaultHttpClient(params);

HttpGet request = new HttpGet(url.toURI());
client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);
client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
HttpResponse response = client.execute(request);

但在 12 秒后,从 client.execute(request) 中抛出 NoRouteToHostException。据我了解, CONNECTION_TIMEOUT 和 SO_TIMEOUT 在这里没用。您知道如何为 NoRouteToHostException 设置超时吗?我希望服务器在这段时间内可用。感谢您的任何评论和建议!

4

0 回答 0