2

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

    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

  • String site ="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/";

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.

train5potting
  • 21
  • 1
  • 1
  • 2

3 Answers3

8

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();
}
Raedwald
  • 46,613
  • 43
  • 151
  • 237
skyuzo
  • 1,140
  • 7
  • 13
  • Except you'll want to use a [real user agent string](http://www.useragentstring.com/pages/useragentstring.php), like maybe "Mozilla/5.0 (Windows NT 6.1; rv:6.0) Gecko/20110814 Firefox/6.0" – Ryan Stewart Oct 11 '11 at 20:31
  • 1
    This didn't work for me I still got the same error. I tested on a few different machines and got this error on one of them `sun.net.ftp.FtpProtocolException: PORT :500 Illegal PORT command, EPSV ALL in effect at sun.net.ftp.FtpClient.openDataConnection(FtpClient.java:435) at sun.net.ftp.FtpClient.list(FtpClient.java:624) at sun.net.www.protocol.ftp.FtpURLConnection.getInputStream(FtpURLConnection.java:373) at test.main(test.java:17)` – train5potting Oct 11 '11 at 22:09
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;
    }
Stephen
  • 1,737
  • 2
  • 26
  • 37
user714814
  • 31
  • 1
1

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.

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.

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

Miquel
  • 15,405
  • 8
  • 54
  • 87