Java apache http 客户端 org.apache.http.NoHttpResponseException: 目标服务器未能响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18237486/
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 23:38:25  来源:igfitidea点击:

apache http client org.apache.http.NoHttpResponseException: The target server failed to respond

javaweb-servicesapachejerseyapache-httpclient-4.x

提问by Waqas Ali

I am using apache http client to test my WS. I have write a get WS in jersey. URL for this WS is

我正在使用 apache http 客户端来测试我的 WS。我在球衣上写了一个 get WS。此 WS 的 URL 是

http://localhost:8080/mobilestore/rest/sysgestockmobilews/getinventory?xml=dataString

to call this WS using url i have write a method which is as follow

要使用 url 调用此 WS,我编写了一个方法,如下所示

public static void getInventory(String input)
        throws ClientProtocolException, IOException {

    System.out.println(input);
    String url = URL + "getinventory";
    HttpClient client = new DefaultHttpClient();

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
    nameValuePairs.add(new BasicNameValuePair("xml", input));
    String paramString = URLEncodedUtils.format(nameValuePairs, "utf-8");
    url += "?" + paramString;
    System.out.println(url);
    HttpGet request = new HttpGet(url);
    HttpResponse response = client.execute(request);
    BufferedReader rd = new BufferedReader(new InputStreamReader(response
            .getEntity().getContent()));

    String line = "";
    while ((line = rd.readLine()) != null) {
        System.out.println(line);
    }

}

Now when i run the program and pass the url to this function i get exception at the line

现在,当我运行程序并将 url 传递给这个函数时,我在该行得到异常

HttpResponse response = client.execute(request);

Exception is as follow

异常如下

Aug 14, 2013 9:31:50 PM org.apache.http.impl.client.DefaultRequestDirector tryExecute
INFO: I/O exception (org.apache.http.NoHttpResponseException) caught when processing         request: The target server failed to respond
Aug 14, 2013 9:31:50 PM org.apache.http.impl.client.DefaultRequestDirector tryExecute
INFO: Retrying request
Aug 14, 2013 9:31:50 PM org.apache.http.impl.client.DefaultRequestDirector tryExecute
INFO: I/O exception (org.apache.http.NoHttpResponseException) caught when processing   request: The target server failed to respond
Aug 14, 2013 9:31:50 PM org.apache.http.impl.client.DefaultRequestDirector tryExecute
INFO: Retrying request
Aug 14, 2013 9:31:50 PM org.apache.http.impl.client.DefaultRequestDirector tryExecute
INFO: I/O exception (org.apache.http.NoHttpResponseException) caught when processing      request: The target server failed to respond
Aug 14, 2013 9:31:50 PM org.apache.http.impl.client.DefaultRequestDirector tryExecute
INFO: Retrying request
Exception in thread "main" 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.tryExecute(DefaultRequestDirector.java:715)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:520)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
at    org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784)
at com.tainosystems.http.client.TestWs.getInventory(TestWs.java:66)
at com.tainosystems.http.client.TestWs.main(TestWs.java:47)

Now if i use the WS url and hit it using any browser i get the expected result but i want to know what is wrong with my apache http client code..

现在,如果我使用 WS url 并使用任何浏览器点击它,我会得到预期的结果,但我想知道我的 apache http 客户端代码有什么问题..

采纳答案by Ondrej Burkert

I went through the links from here and got to this answer: get NoHttpResponseException for load testing

我浏览了这里的链接并得到了这个答案: 获取 NoHttpResponseException 进行负载测试

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:

这让我走上了正确的轨道。在这里更新一下答案是使用当前 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.

我也在那里使用 spring-web,所以我使用客户端作为 RestTemplate 工厂的参数,因为我希望它在 RestTemplate 中使用。