Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:HttpPost到API-ConnectTimeoutException-ApacheHttpClient 4_Java_Apache Httpclient 4.x - Fatal编程技术网

Java:HttpPost到API-ConnectTimeoutException-ApacheHttpClient 4

Java:HttpPost到API-ConnectTimeoutException-ApacheHttpClient 4,java,apache-httpclient-4.x,Java,Apache Httpclient 4.x,目前我正在进行一个项目,我的模块需要使用HttpClient调用一个外部API 我使用POSTMAN手动访问了API,我可以成功连接并获得结果,但是当我运行代码时,我遇到了org.apache.http.conn.ConnectTimeoutException 外部API: http://10.9.11.222:8500/api/getDocs PostToApi class: HttpPost httpPost = new HttpPost("http://10.9.11.222:

目前我正在进行一个项目,我的模块需要使用HttpClient调用一个外部API

我使用POSTMAN手动访问了API,我可以成功连接并获得结果,但是当我运行代码时,我遇到了org.apache.http.conn.ConnectTimeoutException

外部API:

 http://10.9.11.222:8500/api/getDocs

 PostToApi class:

  HttpPost httpPost = new HttpPost("http://10.9.11.222:8500/api/getDocs");
    StringEntity stringEntity = new StringEntity(jsonToBeSent);     
    httpPost.addHeader("Content-type", "application/json");
    httpPost.setEntity(stringEntity);

    response = httpClient.execute(httpPost);

    HttpEntity entity = response.getEntity();
    String responseString = EntityUtils.toString(entity, "UTF-8");

       serverResponse[0]=String.valueOf(response.getStatusLine().getStatusCode());
    serverResponse[1]=responseString;
遇到异常:

org.apache.http.conn.ConnectTimeoutException: Connect to 10.9.11.222:8500 [/ 10.9.11.222] failed: connect timed out
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:143)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:117)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
at element.bst.elementexploration.rest.util.PostToServer.post(PostToServer.java:58)

我找到了问题的解决方案: 我忘记的第一件事是,我正在使用代理,但我没有配置我的HttpClient在获取API时使用它

我的解决方案: 在PostToApi类中添加代理

    HttpHost proxy = new HttpHost("xx.xx.xx.xx",proxyport,"http");
    DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
    httpClient = HttpClients.custom().setRoutePlanner(routePlanner).build();
使用代理完成PostToApi类

    HttpHost proxy = new HttpHost("xxx.xxx.xxx.xxx",port,"http");
    DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
    httpClient = HttpClients.custom().setRoutePlanner(routePlanner).build();

    HttpPost httpPost = new HttpPost("http://10.9.11.222:8500/api/getDocs");
    StringEntity stringEntity = new StringEntity(jsonToBeSent);     
    httpPost.addHeader("Content-type", "application/json");
    httpPost.setEntity(stringEntity);

    response = httpClient.execute(httpPost);

    HttpEntity entity = response.getEntity();
    String responseString = EntityUtils.toString(entity, "UTF-8");

    serverResponse[0]=String.valueOf(response.getStatusLine().getStatusCode());
    serverResponse[1]=responseString;

如果您的客户端不应该做任何复杂的事情,我建议您使用HttpClient的fluentapi。这有助于避免样板代码,编写清晰简单的代码。上述所有代码可替换为:

HttpResponse response = Request.Post("http://10.9.11.222:8500/api/getDocs")
            .bodyString(json, ContentType.APPLICATION_JSON)     
            .viaProxy(new HttpHost("xx.xx.xx.xx", proxyport))
            .execute().returnResponse();
ContentResponseHandler contentHandler = new ContentResponseHandler();
serverResponse[0] = response.getStatusLine().getStatusCode();
serverResponse[1] = contentHandler.handleEntity(response.getEntity()).asString();

您可以阅读有关fluent API的更多信息。

谢谢,我将检查该API:)