This error occurs when the connection closes abruptly (when the TCP connection is reset, while there is still data in the send buffer). The condition is very similar to the much more common "Connection reset by peer". This can happen sporadically when connected over the Internet, but also systematically if the timing is correct (for example, using keep-alive connections on the local host).
The HTTP client should simply re-open the connection and retry the request. It is important to understand that when the connection is in this state, it has no way out but to close it. Any attempt to send or receive will result in the same error.
Do not use URL.open() , use the Apache-Commons HttpClient , which has a retry mechanism, pooling, saving and many other features.
Sample Usage:
HttpClient httpClient = HttpClients.custom() .setConnectionTimeToLive(20, TimeUnit.SECONDS) .setMaxConnTotal(400).setMaxConnPerRoute(400) .setDefaultRequestConfig(RequestConfig.custom() .setSocketTimeout(30000).setConnectTimeout(5000).build()) .setRetryHandler(new DefaultHttpRequestRetryHandler(5, true)) .build(); // the httpClient should be re-used because it is pooled and thread-safe. HttpGet request = new HttpGet(uri); HttpResponse response = httpClient.execute(request); reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); // handle response ...
RustyX Mar 28 '15 at 21:30 2015-03-28 21:30
source share