title: Using HttpURLConnection on Android
body: <p>I am trying to connect a web server I have running in my android App however for some reason it's failing.</p>
  
  <p>I can connect to the webpage normally and using my phone's browser, I can also use the below code to connect to google but for some reason my code will not connect to my web server. If I host the server on port 80 I get error code 503 every time, if I host it on 8080 (which is preferable) I get a timeout exception which is below.</p>
  
  <pre><code>03-05 20:12:22.432: WARN/System.err(29254): java.net.SocketException: The operation timed out
  03-05 20:12:22.602: WARN/System.err(29254):     at org.apache.harmony.luni.platform.OSNetworkSystem.connectSocketImpl(Native Method)
  03-05 20:12:22.602: WARN/System.err(29254):     at org.apache.harmony.luni.platform.OSNetworkSystem.connect(OSNetworkSystem.java:125)
  03-05 20:12:22.602: WARN/System.err(29254):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:227)
  03-05 20:12:22.612: WARN/System.err(29254):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:521)
  03-05 20:12:22.612: WARN/System.err(29254):     at java.net.Socket.connect(Socket.java:1019)
  03-05 20:12:22.612: WARN/System.err(29254):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.&lt;init&gt;(HttpConnection.java:67)
  03-05 20:12:22.612: WARN/System.err(29254):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionManager$ConnectionPool.getHttpConnection(HttpConnectionManager.java:151)
  03-05 20:12:22.612: WARN/System.err(29254):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionManager.getConnection(HttpConnectionManager.java:73)
  03-05 20:12:22.612: WARN/System.err(29254):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getHTTPConnection(HttpURLConnection.java:826)
  03-05 20:12:22.612: WARN/System.err(29254):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:812)
  03-05 20:12:22.612: WARN/System.err(29254):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getResponseCode(HttpURLConnection.java:1275)
  03-05 20:12:22.612: WARN/System.err(29254):     at com.project.library.remailer.ServerSelect.downloadDirectory(ServerSelect.java:84)
  03-05 20:12:22.622: WARN/System.err(29254):     at com.project.main.NewMessage$3$1$1.run(NewMessage.java:156)
  </code></pre>
  
  <p>The code is:</p>
  
  <pre><code>try {
      URL url = new URL(directoryLocation);
      HttpURLConnection httpURLConnection = (HttpURLConnection) url
              .openConnection();
  
      // The line below is where the exception is called
      int response = httpURLConnection.getResponseCode();                 
      if (response == HttpURLConnection.HTTP_OK) {
  
  
          // Response successful
      InputStream inputStream = httpURLConnection.getInputStream();
  
      // Parse it line by line
      BufferedReader bufferedReader = new BufferedReader(
                  new InputStreamReader(inputStream));
      String temp;
      while ((temp = bufferedReader.readLine()) != null) {
          // Parsing of data here
      }
         } else {
              Log.e("SAMES", "INCORRECT RETURN CODE: " + response);
         }
  } catch (MalformedURLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
  } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
  }
  </code></pre>
  
  <p>When I try the code in the link suggested by CommonsWare for port 8080 I get the same error as before, the timeout exception. For port 80 I get the following exception, both of these errors occur on the url.openStream().</p>
  
  <p>Port 80 exception:</p>
  
  <pre><code>03-06 11:53:53.296: WARN/System.err(639): java.io.FileNotFoundException: http://64.197.194.165:80/path
  03-06 11:53:53.376: WARN/System.err(639):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1064)
  03-06 11:53:53.376: WARN/System.err(639):     at java.net.URL.openStream(URL.java:674)
  03-06 11:53:53.376: WARN/System.err(639):     at com.project.library.remailer.ServerSelect.downloadDirectory(ServerSelect.java:118)
  03-06 11:53:53.376: WARN/System.err(639):     at com.project.main.NewMessage$3$1$1.run(NewMessage.java:156)
  </code></pre>
  
  <p>I also tried using the following code:</p>
  
  <pre><code>HttpClient client = new DefaultHttpClient();
          HttpGet request = new HttpGet(directoryLocation);
          try
          {
              HttpResponse response = client.execute(request);
              BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
              String line;
              while((line = reader.readLine()) != null) {
                  Log.e("SAMES", line);
          }
  
      }
      catch(Exception e)
      {
          e.printStackTrace();
      }
  </code></pre>
  
  <p>However that has resulted in the same timeout error for port 8080, which occurs when client.execute(response) is called.</p>
  
  <p>For port 80 I get a webpage which says that it couldn't connect, which alot of HTML editing, however the main thing it says on the page is:</p>
  
  <blockquote>
    <p>03-06 13:10:23.576: ERROR/SAMES(1267): The Web Server may be down, too busy, or experiencing other problems preventing it from responding to requests. You may wish to try again at a later time.</p>
  </blockquote>