我正在使用apache http客户端v4.5并将其用作REST客户端.在某些情况下,我识别出一个错误“[读取] I / O错误:读取超时”,它来自httpclient框架,当它读取接收到的内容并将其显示为最后一条消息时.
它似乎没有影响,但我想知道是否有人知道它来自何处以及如何解决它.根据以下主题(link),似乎是“mutlithreaded”配置的问题.
但是我只使用http客户端的默认配置,当我使用版本v4时,我不知道如何将“multithreaded”设置为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);