连接到网站时出错

时间:2012-09-13 12:04:04

标签: java

我尝试运行以下代码:

public class URLConnectionReader {
    public static void main(String[] args) throws Exception {
        URL oracle = new URL("http://www.google.com/");
        URLConnection yc = oracle.openConnection();
        yc.connect();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                                yc.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) 
            System.out.println(inputLine);
        in.close();
    }
}

但控制台没有显示任何内容,当我尝试添加yc.connect时,控制台显示以下错误:

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 internet01.URLConnectionReader.main(URLConnectionReader.java:18)

更新: 我通过代理连接到互联网。

1 个答案:

答案 0 :(得分:1)

您通过代理连接,JVM可能不知道这一点。

使用这些参数运行程序

-Dhttp.proxyHost=proxy.ip -Dhttp.proxyPort=proxy.port

或者在程序中设置它......

通过 Chris Carruthers gr5

查看the answer