title: Java URL class openStream throwing java.net.ConnectException: Connection refused: connect
body: <p>I'm trying to write a simple program that displays the content of various URLS.  My code is this.</p>
  
  <pre><code>    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();
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>The first 3 resources are fine</p>
  
  <ul>
  <li>String site ="ftp://ftp.suse.com/";   </li>
  <li>String site = "http://www.google.ca"; </li>
  <li>String site = "ftp://ftp.gnu.org/README";</li>
  </ul>
  
  <p>but the last one </p>
  
  <ul>
  <li>String site = "ftp://metalab.unc.edu/";</li>
  </ul>
  
  <p>produces the following error</p>
  
  <pre><code>    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)
  </code></pre>
  
  <p>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.</p>