java - HttpClient 错误 : The target server failed to respond

标签 java apache httpclient connection-pooling

我正在尝试使用 PoolingClientConnectionManager 设置单个主机的最大连接数来使用 HTTP 客户端访问服务器

//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);

那是我们正在使用连接池来实现 http 持久性。

我们偶尔会收到此错误:

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)

有人知道怎么解决吗?

我们也将关闭空闲连接。

请帮忙。

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

最佳答案

可能是 HttpClient 中的错误。

如果您使用的是 HttpClient 4.4,请尝试升级到 4.4.1。

如果你想了解更多信息,请看this link .

如果您无法升级,以下链接可能会有帮助。

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

祝你好运!

关于java - HttpClient 错误 : The target server failed to respond,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29104643/

相关文章:

java - 如何重构使用遍历 map 的代码?

java - OpenGl ES on android微调光线拾取代码

java - 使用 Android TCP 客户端应用程序与传感器交互

java - 使用 HttpClient 发布 JSONObject 时出错

java - 限制接受的 HTTP 响应大小

java - 如何获取已在 Java Servlet 上下文中设置的全局对象

apache - 边缘NGram : Error instantiating class: 'org.apache.lucene.analysis.ngram.EdgeNGramFilterFactory'

apache - Apache 可写目录权限的最佳实践是什么?

Android:使用 Apache Commons Compress 7zip 文件夹

android - 使用 HttpClient 的 HTTP Post 请求需要 2 秒,为什么?