Java HttpClientError: 目标服务器未能响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29104643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:22:00  来源:igfitidea点击:

HttpClientError: The target server failed to respond

javaapachehttpclientconnection-pooling

提问by Balaji V

I am trying to hit a server using http client using PoolingClientConnectionManagersetting max connections for individual hosts

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

 //Code that inilizes my connection mananger and http client 

HttpParams httpParam = httpclient.getParams(); HttpConnectionParams.setSoTimeout(httpParam, SOCKET_TIMEOUT);

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 .

那就是我们使用连接池来实现 http 持久化。

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

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

采纳答案by daimarom

Probably, it is a bug in the HttpClient.

可能是 HttpClient 中的一个错误。

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

如果您使用的是 HttpClient 4.4,请尝试升级到 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/

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

Good luck!

祝你好运!

回答by RSingh

Recently faced similar while using HttpClient 5.

最近在使用HttpClient 5.

On enabling the HttpClientlogs and found that the issue was due to stale connections.

在启用HttpClient日志并发现问题是由于陈旧的连接。

Adding the below helped to solve the issue, it detects and validates the connections that have become stale while kept inactive in the pool before reuse.

添加以下内容有助于解决该问题,它会检测并验证在重用之前在池中保持不活动状态时已失效的连接。

PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();

connectionManager.setValidateAfterInactivity(timeinmilliseconds);