日期:2014-05-17  浏览次数:20752 次

eclipse 使用URL连接出错,求解
今天调用一个webservices,一直报错,以为是地址不正确,我写了个例子,如下
Java code

    public static void main(String[] args) throws Exception {
        URL url = new URL(
        "http://www.baidu.com");
        URLConnection conn = url.openConnection();
        conn.connect();
        System.out.println("連接成功");
    }


竟然连baidu都连不上,我的电脑是可以连网的,用的是eclipse(version=3.6.1),这是怎么回事,错误如下
Java code

Exception in thread "main" java.net.UnknownHostException: www.baidu.com
    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 com.arj.comm.util.WeatherReport.main(WeatherReport.java:131)


WeatherReport是main方法所在类,131行是下边这句
Java code

conn.connect();


机器问题还是这个例子有错误啊

------解决方案--------------------
代码没问题

难倒是楼主网络问题?

你是在电脑测试的?还是手机
------解决方案--------------------
这个说明DNS解析失败。。。

直接在命令行下:ping www.baidu.com 能看到IP地址么?

另外,你不会是用代理来上网的吧?