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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7731673/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:11:25  来源:igfitidea点击:

Java URL class openStream throwing java.net.ConnectException: Connection refused: connect

javanetwork-programming

提问by train5potting

I'm trying to write a simple program that displays the content of various URLS. My code is this.

我正在尝试编写一个简单的程序来显示各种 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();
    }
}

}

}

The first 3 resources are fine

前3个资源没问题

  • String site ="ftp://ftp.suse.com/";
  • String site = "http://www.google.ca";
  • String site = "ftp://ftp.gnu.org/README";
  • 字符串站点 ="ftp://ftp.suse.com/";
  • String site = "http://www.google.ca";
  • String site = "ftp://ftp.gnu.org/README";

but the last one

但最后一个

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

produces the following error

产生以下错误

    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)

This also happens with various other ftp sites that I have tried as well. Haven't had any problems with HTTP sites. Any ideas what I can do to fix this. All the specified resources I can reach from my browser.

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

回答by skyuzo

It's possible that the site is refusing non-browser user agents. You can try setting the 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();
}

回答by user714814

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;
    }

回答by Miquel

Long shot: could it be that your network firewall is selectively filtering where you can access, unless you use their proxy server?

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

A quick test: change your browser config so it uses direct connection (no auto-proxy configuration or a given proxy), and just type in the url of the offending ftp server, see if it works.

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

If it doesn't, that might be your problem, and you'll need to use a library that lets you specify a proxy, perhaps HttpCore.

如果没有,那可能是您的问题,您需要使用一个可以指定代理的库,可能是HttpCore

If all else fails, install wireshark and see what on earth is happenning at the network level...

如果所有其他方法都失败了,请安装wireshark并查看网络级别到底发生了什么......