gpt4 book ai didi

Java:HttpPost 到 API - ConnectTimeoutException - Apache HttpClient 4

转载 作者:行者123 更新时间:2023-11-30 10:33:03 24 4
gpt4 key购买 nike

目前我正在做一个项目,我的模块需要使用 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;

关于Java:HttpPost 到 API - ConnectTimeoutException - Apache HttpClient 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42402856/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com