Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
635 views
in Technique[技术] by (71.8m points)

java - HttpClientError: The target server failed to respond

I am trying to hit a server using HTTP client using PoolingClientConnectionManager setting max connections for individual hosts

//Code that initializes my connection manager and HTTP client 
HttpParams httpParam = httpclient.getParams();
HttpConnectionParams.setSoTimeout(httpParam, SOCKET_TIMEOUT);

HttpConnectionParams.setConnectionTimeout(httpParam, CONN_TIMEOUT);

httpclient.setParams(httpParam);

//Run a thread which closes Expired connections
new ConnectionManager(connManager).start(); 

//Code that executes my request 
HttpPost httpPost = new HttpPost(url);
        HttpEntity httpEntity = new StringEntity(request, "UTF-8");
httpPost.setEntity(httpEntity);

Header acceptEncoding = new BasicHeader("Accept-Encoding", "gzip,deflate");
httpPost.setHeader(acceptEncoding);     

if(contenttype != null && !contenttype.equals("")){
    Header contentType = new BasicHeader("Content-Type", contenttype);
    httpPost.setHeader(contentType);
}
InputStream inputStream = null;
LOG.info(dataSource + URL + url + REQUEST + request);

HttpResponse response = httpclient.execute(httpPost);

That is we are using Connection pooling for http persistence .

We are getting this error sporadically :

The target server failed to respond
org.apache.http.NoHttpResponseException: The target server failed to respond
        at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:95)
        at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:62)
        at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:254)
        at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:289)
        at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:252)
        at org.apache.http.impl.conn.ManagedClientConnectionImpl.receiveResponseHeader(ManagedClientConnectionImpl.java:191)
        at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:300)
        at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:127)
        at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:517)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)

Does any one know how to resolve this?

We are shutting down idle connections as well.

Can some Please help.

http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Probably, it is a bug in the HttpClient.

If you are using the HttpClient 4.4, please try to upgrade to 4.4.1.

If you want for more information, please look at this link.

If you can't upgrade, the following links might be helpful.

http://www.nuxeo.com/blog/using-httpclient-properly-avoid-closewait-tcp-connections/

Good luck!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...