- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
目前我正在做一个项目,我的模块需要使用 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/
我的应用程序中有一个非常大的错误,我似乎真的无法解决。每当我通过以下代码进行休息时: HttpGet request = new HttpGet(url + getParams()); Htt
我正在使用 Spring WebFlux webclient 进行 REST 调用。我已经在 3000 上配置了连接超时毫秒,相应地: WebClient webClient = WebClient.
目前我正在做一个项目,我的模块需要使用 HttpClient 调用我们的一个外部 API。 我使用 POSTMAN 手动访问 API,我可以成功连接并获得结果,但是当我运行代码时遇到了 org.apa
我正在尝试以 react 方式使用 Reactor Netty TcpClient 与可能无法访问的主机进行交互。这是 channel 初始化逻辑的示例: ConnectionProvider con
我正在尝试在 Eclipse 中运行 MapReduce 作业。 我正在尝试连接到 Hortonworks VM 并读取 HDFS 中的文件之一。这是 HDFS 中文件的显示: 我正在使用以下代码访问
我正在使用 HttpClient 来处理 http 请求,当我尝试连接我的目标服务器时,出现错误 org.apache.http.conn.ConnectTimeoutException: Conne
自从我在 ICS 上测试我的应用程序后,我遇到了一个奇怪的问题。 在 Android 2.X 上使用以下代码效果很好(有时会发生超时但很少发生): HttpParams httpParamet
我是一名优秀的程序员,十分优秀!