为什么在 URL up 时会出现“java.net.ConnectException: Connection timed out”异常?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/86824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why would a "java.net.ConnectException: Connection timed out" exception occur when URL is up?
提问by Sarah Haskins
I'm getting a ConnectException: Connection timed out
with some frequency from my code. The URL I am trying to hit is up. The same code works for some users, but not others. It seems like once one user starts to get this exception they continue to get the exception.
我ConnectException: Connection timed out
从我的代码中得到了一些频率。我试图点击的网址已上线。相同的代码适用于某些用户,但不适用于其他用户。似乎一旦一个用户开始得到这个异常,他们就会继续得到这个异常。
Here is the stack trace:
这是堆栈跟踪:
java.net.ConnectException: Connection timed out
Caused by: java.net.ConnectException: Connection timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.Socket.connect(Socket.java:516)
at java.net.Socket.connect(Socket.java:466)
at sun.net.NetworkClient.doConnect(NetworkClient.java:157)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:365)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:477)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:214)
at sun.net.www.http.HttpClient.New(HttpClient.java:287)
at sun.net.www.http.HttpClient.New(HttpClient.java:299)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:796)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:748)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:673)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:840)
Here is a snippet from my code:
这是我的代码片段:
URLConnection urlConnection = null;
OutputStream outputStream = null;
OutputStreamWriter outputStreamWriter = null;
InputStream inputStream = null;
try {
URL url = new URL(urlBase);
urlConnection = url.openConnection();
urlConnection.setDoOutput(true);
outputStream = urlConnection.getOutputStream(); // exception occurs on this line
outputStreamWriter = new OutputStreamWriter(outputStream);
outputStreamWriter.write(urlString);
outputStreamWriter.flush();
inputStream = urlConnection.getInputStream();
String response = IOUtils.toString(inputStream);
return processResponse(urlString, urlBase, response);
} catch (IOException e) {
throw new Exception("Error querying url: " + urlString, e);
} finally {
IoUtil.close(inputStream);
IoUtil.close(outputStreamWriter);
IoUtil.close(outputStream);
}
回答by larsivi
There is a possibility that your IP/host are blocked by the remote host, especially if it thinks you are hitting it too hard.
您的 IP/主机有可能被远程主机阻止,尤其是当它认为您的攻击力太大时。
回答by Jumpy
Connection timeouts (assuming a local network and several client machines) typically result from
连接超时(假设有本地网络和多台客户端机器)通常是由
a) some kind of firewall on the way that simply eats the packets without telling the sender things like "No Route to host"
a) 途中的某种防火墙,它只是吃掉数据包而不告诉发件人诸如“无路由到主机”之类的信息
b) packet loss due to wrong network configuration or line overload
b) 网络配置错误或线路过载导致丢包
c) too many requests overloading the server
c) 太多请求使服务器过载
d) a small number of simultaneously available threads/processes on the server which leads to all of them being taken. This happens especially with requests that take a long time to run and may combine with c).
d) 服务器上有少量同时可用的线程/进程,这导致所有线程/进程都被占用。这尤其发生在需要很长时间运行并且可能与 c) 结合的请求中。
Hope this helps.
希望这可以帮助。
回答by Alexander
If the URL works fine in the web browser on the same machine, it might be that the Java code isn't using the HTTP proxy the browser is using for connecting to the URL.
如果 URL 在同一台计算机上的 Web 浏览器中正常工作,则可能是 Java 代码未使用浏览器用于连接到 URL 的 HTTP 代理。
回答by Powerlord
I'd recommend raising the connection timeout time before getting the output stream, like so:
我建议在获取输出流之前提高连接超时时间,如下所示:
urlConnection.setConnectTimeout(1000);
Where 1000 is in milliseconds (1000 milliseconds = 1 second).
其中 1000 以毫秒为单位(1000 毫秒 = 1 秒)。
回答by Powerlord
- try to do the Telnet to see any firewall issue
- perform
tracert
/traceroute
to find number of hops
- 尝试执行 Telnet 以查看任何防火墙问题
- 执行
tracert
/traceroute
以查找跃点数
回答by eckes
This can be a IPv6 problem (the host publishes an IPv6 AAAA-Address and the users host thinks it is configured for IPv6 but it is actually not correctly connected). This can also be a network MTU problem, a firewall block, or the target host might publish different IP addresses (randomly or based on originators country) which are not all reachable. Or similliar network problems.
这可能是 IPv6 问题(主机发布了 IPv6 AAAA 地址,用户主机认为它已配置为 IPv6,但实际上未正确连接)。这也可能是网络 MTU 问题、防火墙阻止,或者目标主机可能会发布不同的 IP 地址(随机或基于发起者国家/地区),而这些 IP 地址并非都是可访问的。或者类似的网络问题。
You cant do much besides setting a timeout and adding good error messages (especially printing out the hosts' resolved address). If you want to make it more robust add retry, parallel trying of all addresses and also look into name resolution caching (positive and negative) on the Java platform.
除了设置超时和添加好的错误消息(尤其是打印出主机的解析地址)之外,您无能为力。如果您想让它更健壮,请添加重试、所有地址的并行尝试,并查看 Java 平台上的名称解析缓存(正面和负面)。
回答by Damir Olejar
The reason why this happened to me was that a remote server was allowing only certain IP addressed but not its own, and I was trying to render the images from the server's URLs... so everything would simply halt, displaying the timeout error that you had...
这发生在我身上的原因是远程服务器只允许某些 IP 地址而不是它自己的地址,并且我试图从服务器的 URL 渲染图像......所以一切都会简单地停止,显示你的超时错误有...
Make sure that either the server is allowing its own IP, or that you are rendering things from some remote URL that actually exists.
确保服务器允许其自己的 IP,或者您正在从实际存在的某个远程 URL 呈现内容。
回答by M Hamza Javed
The error message says it all: your connection timed out. This means your request did not get a response within some (default) timeframe. The reasons that no response was received is likely to be one of:
错误消息说明了一切:您的连接超时。这意味着您的请求在某个(默认)时间范围内没有得到响应。未收到回复的原因可能是以下原因之一:
a) The IP/domain or port is incorrect
a) IP/域或端口不正确
b) The IP/domain or port (i.e service) is down
b) IP/域或端口(即服务)关闭
c) The IP/domain is taking longer than your default timeout to respond
c) IP/域的响应时间比您的默认超时要长
d) You have a firewall that is blocking requests or responses on whatever port you are using
d) 您有一个防火墙阻止您使用的任何端口上的请求或响应
e) You have a firewall that is blocking requests to that particular host
e) 您有一个防火墙阻止对该特定主机的请求
f) Your internet access is down
f) 您的互联网访问已关闭
g) Your live-server is down i.e in case of "rest-API call".
g) 您的实时服务器已关闭,即在“rest-API 调用”的情况下。
Note that firewalls and port or IP blocking may be in place by your ISP
请注意,您的 ISP 可能设置了防火墙和端口或 IP 阻止
回答by NikNik
I solved my problem with:
我用以下方法解决了我的问题:
System.setProperty("https.proxyHost", "myProxy");
System.setProperty("https.proxyPort", "80");
or http.proxyHost
...
或者http.proxyHost
……
回答by Lonzak
Why would a “java.net.ConnectException: Connection timed out” exception occur when URL is up?
为什么在 URL up 时会出现“java.net.ConnectException: Connection timed out”异常?
Because the URLConnection (HttpURLConnection/HttpsURLConnection) is erratic. You can read about this hereand here. Our solution were two things:
因为 URLConnection (HttpURLConnection/HttpsURLConnection) 是不稳定的。您可以在此处和此处阅读有关此内容的信息。我们的解决方案是两件事:
a) set the ContentLength via setFixedLengthStreamingMode
a) 通过设置 ContentLength setFixedLengthStreamingMode
b) catch any TimeoutException and retry if it failed.
b) 捕获任何 TimeoutException 并在失败时重试。