连接超时时发送Java中HTTP请求(Connection Timeout When Sending

2019-10-19 04:36发布

我用下面这个简单的代码来发送HTTP请求:

import java.net.*;
import java.io.*;

public class URLConnectionReader {
 public static void main(String[] args) throws Exception {
    URL yahoo = new URL("http://www.yahoo.com/");
    URLConnection yc = yahoo.openConnection();
    BufferedReader in = new BufferedReader(
                            new InputStreamReader(
                            yc.getInputStream()));
    String inputLine;

    while ((inputLine = in.readLine()) != null) 
        System.out.println(inputLine);
    in.close();
 }
}

但是,这是行不通的。 我有总是相同的错误:

 Exception in thread "main" java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)

我的操作系统:Windows 7。
我找不到哪里出了问题。 我没有互联网连接的问题。 我已经通过下车我的防火墙尝试,但它并没有解决问题。 我想,因为我类似的C#代码工作它不是一个网络问题。

Answer 1:

请与您的防火墙设置。



Answer 2:

如果你的问题是喜欢我的 ,这个问题可能会在另一端(无论你想谈)的网络安全设置,而不是你自己的防火墙。



Answer 3:

如果你已经AVAST防病毒,这种联系应该帮助你http://forum.avast.com/index.php?topic=120600.0



文章来源: Connection Timeout When Sending HTTP Request in Java