为什么HttpClient在执行POST时会抛出SocketTimeOutException

发布于 2024-09-08 10:30:50 字数 708 浏览 4 评论 0原文

我的代码类似于以下内容:

try {
    HttpPost post = new HttpPost(httpsUrl);
    setHeaders(post);

    HttpEntity entity = new StringEntity(request, "UTF-8");

    post.setEntity(entity);

    HttpResponse response = httpclient.execute(post);
    String result = EntityReader.readContent(response.getEntity());
    checkAnswer(result);
    return result;

} catch (Exception e) {
    throw new ZapException("Error executing the http post request: "+e.getMessage(), e);
}

它使用之前可能已使用过的 httpclient 实例通过 POST 将 request 的内容发送到服务器(它已打开持久连接,因为我们正在发送对同一服务器的相当多的请求...)。

有时会失败,并出现SocketTimeoutException,消息为“读取超时”。 我们不清楚为什么它只在某些时候失败,而大多数时候却不会。什么给?

I have code similar to the following:

try {
    HttpPost post = new HttpPost(httpsUrl);
    setHeaders(post);

    HttpEntity entity = new StringEntity(request, "UTF-8");

    post.setEntity(entity);

    HttpResponse response = httpclient.execute(post);
    String result = EntityReader.readContent(response.getEntity());
    checkAnswer(result);
    return result;

} catch (Exception e) {
    throw new ZapException("Error executing the http post request: "+e.getMessage(), e);
}

It sends the content of request to a server via POST using a httpclient instance that might have already been used before (it has persistent connections turned on, since we're sending quite some requests to the same server...).

This sometimes fails with a SocketTimeoutException with "Read timed out" as the message.
It's not clear to us, why it only fails at some times, when most times it doesn't. What gives?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬心 2024-09-15 10:30:50

在下文中,我假设您使用的是 Apache Commons HttpClient (org.apache.commons.httpclient.HttpClient)。

也许您被抛出SocketTimeoutException只是因为,偶尔,您的 HttpClient 实例与之通信的主机需要很长时间才能响应,从而触发 HttpClient 的取消例程。
您可以使用以下方法增加连接超时和套接字超时

HttpConnectionParams params = httpclient.getHttpConnectionManager().getParams();
params.setConnectionTimeout(20000);
params.setSoTimeout(15000);

此外,如果尽管增加了超时限制,但您仍然面临超时,那么优雅地处理 SocketTimeoutException 是一个很好的做法 - 例如通过重试连接第二次和第三次。

In the following, I assume you are using Apache Commons HttpClient (org.apache.commons.httpclient.HttpClient).

Maybe you get thrown a SocketTimeoutException simply because, occasionally, the host your HttpClient instance is communicating with takes too long to respond, triggering HttpClient's cancellation routine.
You can increase the connection timeout and the socket timeout with the following

HttpConnectionParams params = httpclient.getHttpConnectionManager().getParams();
params.setConnectionTimeout(20000);
params.setSoTimeout(15000);

Aditionally, if you still face timeouts despite increasing the timeout limits, it is a good practice to handle the SocketTimeoutException gracefully - for example by retrying the connection a second and third time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文