Apache HttpClient Interim Error: NoHttpResponseException
Most likely persistent connections that are kept alive by the connection manager become stale. That is, the target server shuts down the connection on its end without HttpClient being able to react to that event, while the connection is being idle, thus rendering the connection half-closed or 'stale'. Usually this is not a problem. HttpClient employs several techniques to verify connection validity upon its lease from the pool. Even if the stale connection check is disabled and a stale connection is used to transmit a request message the request execution usually fails in the write operation with SocketException and gets automatically retried. However under some circumstances the write operation can terminate without an exception and the subsequent read operation returns -1 (end of stream). In this case HttpClient has no other choice but to assume the request succeeded but the server failed to respond most likely due to an unexpected error on the server side.
The simplest way to remedy the situation is to evict expired connections and connections that have been idle longer than, say, 1 minute from the pool after a period of inactivity. For details please see the 2.5. Connection eviction policy
of the HttpClient 4.5 tutorial.
How to solve org.apache.http.NoHttpResponseException
Finally figured it out on my own. If the previous response gives header, "connections=close", next request always gets this exception. So, when seeing this header, put 2 more lines
conManager.closeExpiredConnections();
conManager.closeIdleConnections(0, TimeUnit.SECONDS);
to let connection manager close the connection so that the connection won't be used by the next request.
apache http client org.apache.http.NoHttpResponseException: The target server failed to respond
I went through the links from here and got to this answer:
get NoHttpResponseException for load testing
That set me on the right track. To update the answer a bit here is the solution using the current http-client 4.5 API:
private final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(createHttpClient());
private CloseableHttpClient createHttpClient() {
return HttpClients.custom().setRetryHandler((exception, executionCount, context) -> {
if (executionCount > 3) {
LOGGER.warn("Maximum tries reached for client http pool ");
return false;
}
if (exception instanceof org.apache.http.NoHttpResponseException) {
LOGGER.warn("No response from server on " + executionCount + " call");
return true;
}
return false;
}).build();
}
I also use spring-web there so I used the client as a parameter for RestTemplate factory since I want it to be used in the RestTemplate.
Java - NoHttpResponseException when using apache fluent api to post
Instead of using fluent API I used the following below method and overcome my own problem:-
get NoHttpResponseException for load testing
Apache HttpClient Interim Error: NoHttpResponseException
get NoHttpResponseException for load testing
I've found the root cause of the problem.
For some reason the connection becomes invalid and pool is not aware of it.
In this case the NoHttpResponseException
is thrown and the request simply fails. I thought that such issues should be resolved in HTTP client pool layer and be transparent to my code, but this is not as it acts.
To solve this problem the HttpRequestRetryHandler
in HTTP client should be overridden:
ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(schemeRegistry);
...
DefaultHttpClient httpClient = new DefaultHttpClient(cm, params);
httpClient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
@Override
public boolean retryRequest(IOException exception, int executionCount,
HttpContext context) {
if (executionCount > 3) {
LOGGER.warn("Maximum tries reached for client http pool ");
return false;
}
if (exception instanceof org.apache.http.NoHttpResponseException) {
LOGGER.warn("No response from server on " + executionCount + " call");
return true;
}
return false;
}
});
Related Topics
Are There Best Practices for (Java) Package Organization
Package a Non-Modular Javafx Application
How to Express Dependency in Maven on Java Ee Features for Transition to Java 9
How to Read Xml Response from a Url in Java
What Is the Most Efficient Java Collections Library
What Are the Differences Between Abstract Classes and Interfaces in Java 8
How to Enumerate Ip Addresses of All Enabled Nic Cards from Java
How to Enable Logging for SQL Statements When Using Jdbc
Why Is This Java Stream Operated Upon Twice
Java Singleton and Synchronization
Java - How to Drag and Drop JPAnel with Its Components
Run Command Prompt as Administrator
Charsequence VS String in Java
Java Fileoutputstream Create File If Not Exists
Getting Database Connection in Pure JPA Setup