当前位置: 首页 > 面试题库 >

apache http客户端org.apache.http.NoHttpResponseException:目标服务器无法响应

拓拔阎宝
2023-03-14
问题内容

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

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

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

}

现在,当我运行程序并将URL传递给此函数时,我在行中得到了异常

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客户端代码出了什么问题。


问题答案:

我从这里浏览了链接,并得到以下答案:
获得NoHttpResponseException以进行负载测试

那使我走上了正确的道路。要稍微更新一下答案,这里是使用当前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中使用它。



 类似资料:
  • 我正在web应用程序上使用JMeter5.2运行负载测试。当我运行线程组时,即使使用超过500个线程,我也会在用户中配置jmeter:in。我设置的属性:httpclient4.retrycount=1 hc.parameters.file=hc.parameters 在hc.parameters中,我设置了: http . connection . stale check $ Boolean =

  • 问题内容: 我正在尝试使用http客户端通过为单个主机设置最大连接来访问服务器 HttpParams httpParam = httpclient.getParams(); HttpConnectionParams.setSoTimeout(httpParam,SOCKET_TIMEOUT); 那就是我们使用连接池来实现http持久性。 我们偶尔会收到此错误: 有谁知道如何解决这个问题? 我们也将

  • 我有一个Android Studio应用程序,用于客户端读取加速计的数据,并将其发送到运行TCP C套接字服务器的计算机。 我想等到服务器确认接收到数据后再发送一个。当我不得不从Android系统读取服务器的确认信息时,问题就出现了,这只是简单的崩溃,我不知道为什么。这是我使用的代码。 服务器代码 和客户端代码(Android Studio) 我在交流中遗漏了什么还是我做错了什么?

  • 我有一个Tcp客户端,连接到一个旧的主机(52年),发送和接收来自它的请求和响应。 这是我的客户机的核心连接部分, 我试图用Netty重写下面的文章。通过使用以下教程作为参考。 http://tutorials.jenkov.com/netty/netty-tcp-client.html 我面临的问题是我能够连接到服务器,但不能从中读写。我正在使用一个来执行读写操作。 这是我的脾气暴躁的客户 处理

  • 问题内容: 我正在尝试编写一个简单的Java http客户端,该客户端仅打印出服务器响应的一行。我的问题是服务器没有响应。这是我所拥有的,正在编译并且没有明显错误地运行,只是在键入主机名(例如“ www.google.com”)后挂起: 有什么建议?请注意,这假设存在一个“ index.html”-即使为true,它仍会挂起。 问题答案: 我认为我可以通过对代码进行少量更改来重现该问题,因此现在它

  • 我想在一些计算机之间建立点对点连接,这样用户就可以在没有外部服务器的情况下聊天和交换文件。我最初的想法如下: 我在服务器上制作了一个中央服务器插座,所有应用程序都可以连接到该插座。此ServerSocket跟踪已连接的套接字(客户端),并将新连接的客户端的IP和端口提供给所有其他客户端。每个客户端都会创建一个新的ServerSocket,所有客户端都可以连接到它。 换句话说:每个客户端都有一个Se