Java URL类openStream抛出java.net.ConnectException:连接被拒绝:连接

时间:2011-10-11 19:50:52

标签: java network-programming

我正在尝试编写一个显示各种URL的内容的简单程序。我的代码就是这个。

    import java.io.IOException;
    import java.io.InputStream;
    import java.net.*;

    public class test {
public static void main(String[] args) {
    URL url;
    //String site ="ftp://ftp.suse.com/";
    //String site ="http://www.google.ca";
    //String site = "ftp://ftp.gnu.org/README";
    String site = "ftp://metalab.unc.edu/";
    try {
        url = new URL(site);
        InputStream stream = url.openStream();
        for(int i = 0;i!= -1;i= stream.read()){
            System.out.print((char)i);
        }
        stream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

前3个资源都很好

  • String site =“ftp://ftp.suse.com/”;
  • String site =“http://www.google.ca”;
  • String site =“ftp://ftp.gnu.org/README”;

但最后一个

  • String site =“ftp://metalab.unc.edu/”;

产生以下错误

    java.net.ConnectException: Connection refused: 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.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.ftp.impl.FtpClient.openPassiveDataConnection(Unknown Source)
at sun.net.ftp.impl.FtpClient.openDataConnection(Unknown Source)
at sun.net.ftp.impl.FtpClient.list(Unknown Source)
at sun.net.www.protocol.ftp.FtpURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at test.main(test.java:13)

这也发生在我尝试过的各种其他ftp站点上。 HTTP站点没有任何问题。任何想法我可以做些什么来解决这个问题。我可以通过浏览器访问的所有指定资源。

3 个答案:

答案 0 :(得分:5)

该网站可能是refusing non-browser user agents。您可以尝试设置User-Agent:

URL url;
String site = "ftp://metalab.unc.edu/";
try {
    url = new URL(site);
    URLConnection urlc = url.openConnection();
    urlc.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0a2) Gecko/20110613 Firefox/6.0a2");
    InputStream stream = urlc.getInputStream();
    for(int i = 0;i!= -1;i= stream.read()){
        System.out.print((char)i);
    }
    stream.close();
} catch (IOException e) {
    e.printStackTrace();
}

答案 1 :(得分:3)

private static InputStream getStreamFromUrl(URL urlTemp) {
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("http-pnproxy", 8080));
        URLConnection uc;
        InputStream inputStream = null;
        URL url = null;
        try {
            url = urlTemp;
            uc = (URLConnection)url.openConnection(proxy);
            uc.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0a2) Gecko/20110613 Firefox/6.0a2");
            uc.connect();
            inputStream = uc.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return inputStream;
    }

答案 2 :(得分:1)

远景:您的网络防火墙是否可以选择性地过滤您可以访问的地方,除非您使用他们的代理服务器?

快速测试:更改您的浏览器配置,使其使用直接连接(无自动代理配置或给定代理),只需输入有问题的ftp服务器的URL,看看它是否有效。

如果没有,那可能是您的问题,并且您需要使用允许您指定代理的库,可能是HttpCore

如果一切都失败了,请安装wireshark,看看网络层面究竟发生了什么......