java - Apache Http 客户端打印 "[read] I/O error: Read timed out""

标签 java multithreading timeout httpclient apache-httpcomponents

我正在使用 apache http 客户端 v4.5 并将其用作 REST 客户端。在某些情况下,我发现一个错误“[读取] I/O 错误:读取超时”,它来自 httpclient 框架,当它读取接收到的内容并将其显示为最后一条消息时。

它似乎没有影响,但是我想知道是否有人知道它来自哪里以及如何解决它。根据以下线程(link),这似乎是“多线程”配置的问题。

但是我只使用 http 客户端的默认配置,当我使用版本 v4 时,我不知道如何将“多线程”设置为 false 以查看它是否有任何区别。

我也尝试设置超时,但没有帮助。

有什么提示吗?

日志:

15:48:05.984 [main] DEBUG org.apache.http.wire - http-outgoing-8 << "HTTP/1.1 200 OK[\r][\n]"
15:48:05.984 [main] DEBUG org.apache.http.wire - http-outgoing-8 << "Date: Tue, 29 Dec 2015 14:48:03 GMT[\r][\n]"
15:48:05.984 [main] DEBUG org.apache.http.wire - http-outgoing-8 << "Server: Apache/2.4.12 (Win32) OpenSSL/1.0.1l PHP/5.6.8[\r][\n]"
15:48:05.984 [main] DEBUG org.apache.http.wire - http-outgoing-8 << "X-Powered-By: PHP/5.6.8[\r][\n]"
15:48:05.985 [main] DEBUG org.apache.http.wire - http-outgoing-8 << "Cache-Control: nocache, private[\r][\n]"
15:48:05.985 [main] DEBUG org.apache.http.wire - http-outgoing-8 << "Content-Length: 99[\r][\n]"
15:48:05.985 [main] DEBUG org.apache.http.wire - http-outgoing-8 << "Keep-Alive: timeout=5, max=99[\r][\n]"
15:48:05.985 [main] DEBUG org.apache.http.wire - http-outgoing-8 << "Connection: Keep-Alive[\r][\n]"
15:48:05.985 [main] DEBUG org.apache.http.wire - http-outgoing-8 << "Content-Type: application/json[\r][\n]"
15:48:05.985 [main] DEBUG org.apache.http.wire - http-outgoing-8 << "[\r][\n]"
15:48:05.985 [main] DEBUG org.apache.http.wire - http-outgoing-8 << "{"success":true,"data":{"id":1946,"location":"http:\/\/localhost:9001\/shop\/api\/articles\/1946"}}"
15:48:06.016 [main] DEBUG org.apache.http.wire - http-outgoing-8 << "[read] I/O error: Read timed out"

我的Http客户端初始化

HttpClientBuilder httpBuilder = HttpClientBuilder.create();

//      set timeout did not helped
//      RequestConfig.Builder requestBuilder = RequestConfig.custom();
//      requestBuilder = requestBuilder.setConnectTimeout(timeout);
//      requestBuilder = requestBuilder.setConnectionRequestTimeout(timeout);
//      requestBuilder = requestBuilder.setSocketTimeout(timeout);
//      httpBuilder.setDefaultRequestConfig(requestBuilder.build());

HttpClient httpClient = httpBuilder.build();

最佳答案

我使用的是 httpclient 4.5.2,在我的例子中,设置请求超时有帮助:

HttpPost postRequest = new HttpPost("https://...");
postRequest.setHeader(..., ...);

RequestConfig requestConfig = RequestConfig.custom()
                        .setSocketTimeout(1000)
                        .setConnectTimeout(1000)
                        .build();

postRequest.setConfig(requestConfig);

关于java - Apache Http 客户端打印 "[read] I/O error: Read timed out"",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34513826/

相关文章:

java - 通过匹配给定字符串中的模式获取字符串数组

java - 对多线程概念有点困惑

java - 如何在 PowerShell 中获取 Java 版本

java - 使用 or 进行 Python 赋值

multithreading - Julia 1.5.2并行启动随机数选择

java - 如何避免交替同步线程上的竞争条件?

c - C 中用于 TFTP 的超时实现

bash - 如何使用 Bash 读取超时?

java - 当 JSF/Spring 中的 session 超时时重定向到登录或当 session 停用(或给定时间空闲)时自动注销

java - 解密错误: "no iv set when one expected"