Java 教程示例代码抛出:java.net.SocketException: Connection reset - 是什么原因导致的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16942539/
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
Java tutorial sample code throws: java.net.SocketException: Connection reset - what does cause it?
提问by achingfingers
So this is a beginner's question.
所以这是一个初学者的问题。
When executing the sample code from the working with urlschapter it throws:
在执行使用 urls章节中的示例代码时,它会抛出:
Exception in thread "main" java.net.SocketException: Connection resetat java.net.SocketInputStream.read(SocketInputStream.java:189) ...
线程“main”中的异常java.net.SocketException: Connection resetat java.net.SocketInputStream.read(SocketInputStream.java:189) ...
Origin is the openStream()method.
起源是openStream()方法。
Here is the code:
这是代码:
import java.net.*;
import java.io.*;
public class URLReader {
public static void main(String[] args) throws Exception {
URL oracle = new URL("http://www.oracle.com/");
BufferedReader in = new BufferedReader(
new InputStreamReader(oracle.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
in.close();
}
}
I know there are similar threads regarding that topic, but i could not find an answer that suits me.
我知道有关于该主题的类似主题,但我找不到适合我的答案。
What I've tried so far:
到目前为止我尝试过的:
- I have set the proxy host as suggested here. Command was: java -Dhttp.proxyHost=dslb-088-071-100-199.pools.arcor-ip.net, I also tried it with inserting System.setProperty("http.proxyHost", "dslb-088-071-100-199.pools.arcor-ip.net");in the first line of the URLReader class.
- I tried JSoup html parser and
- org.apache.commons.io.FileUtils.copyURLToFile(URL, File) method to have a similar result.
- 我已经按照此处的建议设置了代理主机 。命令是:java -Dhttp.proxyHost=dslb-088-071-100-199.pools.arcor-ip.net,我也尝试插入System.setProperty("http.proxyHost", "dslb-088-071- 100-199.pools.arcor-ip.net"); 在 URLReader 类的第一行。
- 我试过 JSoup html 解析器和
- org.apache.commons.io.FileUtils.copyURLToFile(URL, File) 方法有类似的结果。
Whatever I try, I always get the same error: There will happen nothing for 30 seconds or so and then it throws the mentioned SocketException.
无论我尝试什么,我总是得到同样的错误:30 秒左右什么都不会发生,然后它会抛出提到的 SocketException。
I simply dont know how to continue in solving this problem. Helpful would be to get information about what happens in background during the 30seconds before connection reset.
我只是不知道如何继续解决这个问题。获取有关在连接重置前 30 秒内后台发生的情况的信息会很有帮助。
So what could actually cause this Exception?
那么究竟是什么导致了这个异常呢?
The smallest hint could help! Thank you!
最小的提示可能会有所帮助!谢谢!
采纳答案by Menios
Your code is working fine for JVM's that can connect to the internet.
您的代码适用于可以连接到 Internet 的 JVM。
Based on the original question and discussion: http://chat.stackoverflow.com/rooms/31264/discussion-between-achingfingers-and-meewokit seems that either:
基于原始问题和讨论:http: //chat.stackoverflow.com/rooms/31264/discussion-between-achingfingers-and-meewok似乎:
- An intermediate firewallis blocking the JVM from making the connection (or another similar network issue).
- An operating system firewall, or antivirusthat is causing the problems as well.
- 中间防火墙阻止 JVM 建立连接(或其他类似的网络问题)。
- 导致问题的操作系统防火墙或防病毒软件。
My suggestion is to try:
我的建议是尝试:
- Same app on different computer within same network (to see if it is PC specific).
- Same app on different network.
- 同一网络内不同计算机上的相同应用程序(查看它是否特定于 PC)。
- 不同网络上的相同应用程序。
回答by Balint Bako
Try Apache HTTPClient. I hope all the imports are included as this code is not tested as it is... Also your 30s is the connection timeout of your client.
试试 Apache HTTPClient。我希望所有导入都包含在内,因为此代码未按原样进行测试...此外,您的 30 多岁是客户端的连接超时。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
public class URLReader {
public static void main(String[] args) throws Exception {
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, timeOut);
httpclient.getParams().setParameter(
CoreConnectionPNames.SO_TIMEOUT, 2 * timeOut);
httpclient.getParams().setParameter(
CoreConnectionPNames.STALE_CONNECTION_CHECK, false);
httpclient.getParams().setParameter(
CoreConnectionPNames.TCP_NODELAY, true);
HttpHost proxy = new HttpHost(%proxyhost%, %proxyport%);
HttpGet httpget = new HttpGet("http://www.oracle.com");
HttpResponse resp = httpclient.execute(httpget);
respCode = resp.getStatusLine().getStatusCode();
BufferedReader br = new BufferedReader(new InputStreamReader(resp
.getEntity().getContent()));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}
}