title: Is it possible to have multiple URL connections in JAVA?
body: <p>I'm having some trouble getting URL connection to work. I find that if I create one URL object it works fine. But everything after that doesn't work at all. At first I thought it could have been due to me not closing a stream but that was not the case.</p>
  
  <p>-1 = Failure<p>
   1 = success</p>
  
  <pre><code>Testing:
          URLFail test0 = new URLFail();
          URLFail test1 = new URLFail(1);
          URLFail test2 = new URLFail(1,1);
  
  OutPut:
      0Param  1
      1Param -1
      2Param -1
  </code></pre>
  
  <p>Now if i switch which object gets create first the results change.</p>
  
  <pre><code>Testing:
          URLFail test2 = new URLFail(1,1);
          URLFail test0 = new URLFail();
          URLFail test1 = new URLFail(1);
  
  OutPut:
        0Param -1
        1Param -1
        2Param  1
  </code></pre>
  
  <p>In this instance only test2 succeeded. This leads me to believe that only one URL stream can be used per class?<p>This is what i've been using to access and parse the URL.</p>
  
  <pre><code>        URL url = null;
          URLConnection urlConnection = null;
          try {
              url = new URL("xxxxx"); // In my program this is a proper URL
  
          } catch (MalformedURLException ex) {
              return -1;
          }
  
          ArrayList&lt;String&gt; index = new ArrayList&lt;String&gt;();
  
          try {
              urlConnection = url.openConnection();
              BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
  
              while (in.ready()) {
                  index.add(in.readLine());
              }
  
              in.close();
          } catch (IOException ex) {
              return -1;
  
          }
  </code></pre>
  
  <p>So far I don't really know what's wrong. I think it could be due to the fact that I'm trying to access the same website too fast?
  <p>
  I've also tried without a URLConnection object by using the openStream() method found in URL. Any help will be appreciated, also any general comments about the code itself are welcomed as well. Thanks! </p>
  
  <p><p>This is the Entire Code:</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.net.MalformedURLException;
  import java.net.URL;
  import java.net.URLConnection;
  import java.util.ArrayList;
  
  public class URLFail {
      private int name;
  
      public static void main(String[] args) {
  
          URLFail test0 = null;
          URLFail test1 = null;
          URLFail test2 = null;
  
          try {
              test0 = new URLFail();
              System.out.println("First Complete...");
              Thread.sleep(1000);
              test2 = new URLFail();
              System.out.println("Second Complete...");
              Thread.sleep(1000);
              test1 = new URLFail();
              System.out.println("Third Complete...");
          } catch (InterruptedException e) {
              // TODO: handle exception
          }
  
          System.out.println();
          System.out.println("0Param " + test0.name);
          System.out.println("1Param " + test1.name);
          System.out.println("2Param " + test2.name);
  
      }
  
      public URLFail() {
          name = getAsList();
      }
  
      public int getAsList() {
  
          URL url = null;
          URLConnection urlConnection = null;
          try {
              url = new URL("xxxxx");
          } catch (MalformedURLException ex) {
              ex.printStackTrace();
              return -1;
          }
  
          ArrayList&lt;String&gt; index = new ArrayList&lt;String&gt;();
  
          try {
              urlConnection = url.openConnection();
              BufferedReader in = new BufferedReader(new InputStreamReader(
                      urlConnection.getInputStream()));
  
              while (in.ready()) {
                  index.add(in.readLine());
              }
  
              in.close();
          } catch (IOException ex) {
              ex.printStackTrace();
              return -1;
          }
  
          if (index.isEmpty()) {
              return -1;
          } else {
              return 1;
          }
  
      }
  
  }
  </code></pre>
  
  <p>I tried this with google.com and it worked fine, so it must be the host that I'm trying to connect to. Is there any way to close a URL connection/disconnect?</p>