简体   繁体   English

调用 API 时 I/O 超时异常 (java.net.ConnectException)

[英]I/O timeout exception (java.net.ConnectException) When calling API

I'm using HttpClient in order to call an API and get its response, and I have set a 60 secs timeout.我正在使用 HttpClient 来调用 API 并获得其响应,并且我设置了 60 秒的超时时间。 In this 60 seconds, Java try and retry to connect to the API by each 20 seconds and shows the exception.在这 60 秒内,Java 每隔 20 秒尝试并重试连接到 API 并显示异常。 In the final of 60 secs, it stops retrying.在 60 秒的最后,它停止重试。

My doubt is: this I/O Exception is caused by the API?我的疑问是:这个 I/O Exception 是由 API 引起的? Since I set a timeout higher than the exception returns (each 20 seconds).由于我设置的超时时间高于异常返回值(每 20 秒)。

Here's the code and the log:这是代码和日志:

HttpClient client = new HttpClient();
GetMethod getMethod = new GetMethod(GET_TICKETS_URL);
getMethod.setRequestHeader("Content-Type", "application/json");
getMethod.setRequestHeader("Accept", "application/json");
getMethod.getParams().setSoTimeout(60000);
logger.info("Calling service: " + getMethod.getPath());

client.getHttpConnectionManager().getParams().setConnectionTimeout(60000);
client.getHttpConnectionManager().getParams().setSoTimeout(60000);
client.getParams().setSoTimeout(60000);
client.getParams().setConnectionManagerTimeout(60000);

int getTicketsResponse = client.executeMethod(getMethod);
[2020-08-27 13:41:25,215] pool-3-thread-1 br.com.pfm.tasks.baml.task Task INFO  - Calling service: /
[2020-08-27 13:41:46,228] pool-3-thread-1 org.apache.commons.httpclient.HttpMethodDirector INFO  - I/O exception (java.net.ConnectException) caught when processing request: Connection timed out: connect
[2020-08-27 13:41:46,228] pool-3-thread-1 org.apache.commons.httpclient.HttpMethodDirector INFO  - Retrying request
[2020-08-27 13:42:07,242] pool-3-thread-1 org.apache.commons.httpclient.HttpMethodDirector INFO  - I/O exception (java.net.ConnectException) caught when processing request: Connection timed out: connect
[2020-08-27 13:42:07,242] pool-3-thread-1 org.apache.commons.httpclient.HttpMethodDirector INFO  - Retrying request
[2020-08-27 13:42:28,258] pool-3-thread-1 org.apache.commons.httpclient.HttpMethodDirector INFO  - I/O exception (java.net.ConnectException) caught when processing request: Connection timed out: connect
[2020-08-27 13:42:28,258] pool-3-thread-1 org.apache.commons.httpclient.HttpMethodDirector INFO  - Retrying request
[2020-08-27 13:42:49,271] pool-3-thread-1 br.com.pfm.tasks.baml.task ERROR - java.net.ConnectException: Connection timed out: connect

I/O exception is occur when there is an issue in reading from the url, can you able to get the stack stacktrace, will give an insight about the issue, it could be the server unavailable or not found anything from http 400+当从 url 读取时出现问题时会发生 I/O 异常,您能否获得堆栈堆栈跟踪,将提供有关该问题的见解,可能是服务器不可用或未从 http 400+ 中找到任何内容

so please add this to your catch block and will help you get an idea, what went wrong.所以请将它添加到您的 catch 块中,这将帮助您了解哪里出了问题。

catch(Exception e)
{
   e.printStacktrace();
}

Based on the somewhat incomplete information presented here, a stack trace will likely reveal the point of failure is where you try to kick off the HTTP transaction (actually tell it to go a head and connect).基于此处提供的有些不完整的信息,堆栈跟踪可能会揭示故障点是您尝试启动 HTTP 事务的地方(实际上是告诉它开始并连接)。

Assuming that you've set everything up correctly in GetMethod (and I cant tell), may i recomend that you check that the target you are trying to reach, is actually reachable from the machine?假设您已经在 GetMethod 中正确设置了所有内容(我无法判断),我可以建议您检查一下您试图到达的目标,实际上可以从机器到达吗? try a wget or if its on a desk top try to access it with your browser.尝试使用 wget 或者如果它在桌面上,请尝试使用浏览器访问它。 If that works, its likely you set things up incorrectly in your GetMethod code - posting that would be very helpful to troubleshoot further.如果可行,则很可能您在 GetMethod 代码中设置错误 - 发布这对进一步排除故障非常有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.net.ConnectException:连接被拒绝超时 - java.net.ConnectException: Connection refused timeout java.net.connectexception连接超时 - java.net.connectexception connection timeout java.net.ConnectException - java.net.ConnectException java.net.ConnectException - java.net.ConnectException 在指定超时之前抛出异常“java.net.ConnectException:连接超时:连接” - Exception “java.net.ConnectException: Connection timed out: connect” is thrown before specified timeout Hadoop Java获得异常:java.net.ConnectException - Hadoop java Got exception: java.net.ConnectException Java 中的 ssh 异常:java.net.ConnectException:连接被拒绝:连接 - ssh Exception in Java: java.net.ConnectException: Connection refused: connect 线程“ main”中的异常java.net.ConnectException:连接被拒绝:运行Oracle教程示例时进行连接 - Exception in thread “main” java.net.ConnectException: Connection refused: connect when running Oracle tutorial example java.net.ConnectException: /192.168.1.106:8002 - 连接被拒绝(Android 异常) - java.net.ConnectException: /192.168.1.106:8002 - Connection refused ( Android Exception ) 为什么在 URL up ​​时会出现“java.net.ConnectException: Connection timed out”异常? - Why would a “java.net.ConnectException: Connection timed out” exception occur when URL is up?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM