apache http client org.apache.http.NoHttpResponseException:目标服务器无法响应 [英] apache http client org.apache.http.NoHttpResponseException: The target server failed to respond

查看:3153
本文介绍了apache http client org.apache.http.NoHttpResponseException:目标服务器无法响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

}

现在我运行程序时将url传递给此函数我在行处获得异常

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

HttpResponse response = client.execute(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
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)

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

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..

推荐答案

我从这里查看了链接并得到了这个答案:
获取负载测试的NoHttpResponseException

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

这让我走上正轨。要在这里更新答案是使用当前http-client 4.5 API的解决方案:

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();
}

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

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.

这篇关于apache http client org.apache.http.NoHttpResponseException:目标服务器无法响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆