java-apache http client org.apache.http.NoHttpResponseException:目标服务器无法响应
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-apache http client org.apache.http.NoHttpResponseException:目标服务器无法响应,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4422字,纯文字阅读大概需要7分钟。
内容图文
![java-apache http client org.apache.http.NoHttpResponseException:目标服务器无法响应](/upload/InfoBanner/zyjiaocheng/694/fd7538c1bafd4d6191374d42aacd6494.jpg)
我正在使用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客户端代码出了什么问题.
解决方法:
我从这里浏览了链接,得到了这个答案:
get NoHttpResponseException for load testing
那使我走上了正确的道路.要稍微更新一下答案,这里是使用当前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中使用它.
内容总结
以上是互联网集市为您收集整理的java-apache http client org.apache.http.NoHttpResponseException:目标服务器无法响应全部内容,希望文章能够帮你解决java-apache http client org.apache.http.NoHttpResponseException:目标服务器无法响应所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。