title: Android: HttpURLConnection not working properly
body: <p>I'm trying to get the cookies from a website after sending user credentials through a POST Request an it seems that it doesn't work in android this way. ¿Am I doing something bad?. Please help. I've searched here in different posts but there's no useful answer.</p>
  
  <p>It's curious that this run in a desktop Java implementation it works perfect but it crashes in Android platform. And it is exactly the same code, specifically when calling HttpURLConnection.getHeaderFields(), it also happens with other member methods. It's a simple code and I don't know why the hell isn't working.</p>
  
  <p><strong>DESKTOP CODE: This goes just in the main()</strong></p>
  
  <pre><code>HttpURLConnection connection = null;
          OutputStream out = null;
          try {                   
              URL url = new URL("http://www.XXXXXXXX.php");           
              String charset = "UTF-8";       
  
              String postback = "1";
              String user = "XXXXXXXXX";
              String password = "XXXXXXXX";
              String rememberme = "on";
              String query = String.format("postback=%s&amp;user=%s&amp;password=%s&amp;rememberme=%s"
                      , URLEncoder.encode(postback, charset)
                      , URLEncoder.encode(user,charset)
                      , URLEncoder.encode(password, charset)
                      , URLEncoder.encode(rememberme, charset));
  
              connection = (HttpURLConnection)url.openConnection();
              connection.setRequestMethod("POST");
              connection.setRequestProperty("Accept-Charset", charset);
              connection.setDoOutput(true);           
              connection.setFixedLengthStreamingMode(query.length());
  
              out = connection.getOutputStream ();
              out.write(query.getBytes(charset));
  
              if (connection.getHeaderFields() == null){
                  System.out.println("Header null");
              }else{
                  for (String cookie: connection.getHeaderFields().get("Set-Cookie")){
                      System.out.println(cookie.split(";", 2)[0]);
                  }
              }
          } catch (IOException e){
              e.printStackTrace();
          } finally {
              try { out.close();} catch (IOException e) { e.printStackTrace();}
              connection.disconnect();            
          }
  </code></pre>
  
  <p>So the output is:</p>
  
  <pre><code>    login_key=20ad8177db4eca3f057c14a64bafc2c9
      FASID=cabf20cc471fcacacdc7dc7e83768880
      track=30c8183e4ebbe8b3a57b583166326c77
      client-data=%7B%22ism%22%3Afalse%2C%22showm%22%3Afalse%2C%22ts%22%3A1349189669%7D
  </code></pre>
  
  <p><strong>ANDROID CODE: This goes inside doInBackground AsyncTask body</strong></p>
  
  <pre><code>HttpURLConnection connection = null;
              OutputStream out = null;
              try {                   
                  URL url = new URL("http://www.XXXXXXXXXXXXXX.php");         
                  String charset = "UTF-8";       
  
                  String postback = "1";
                  String user = "XXXXXXXXX";
                  String password = "XXXXXXXX";
                  String rememberme = "on";
                  String query = String.format("postback=%s&amp;user=%s&amp;password=%s&amp;rememberme=%s"
                          , URLEncoder.encode(postback, charset)
                          , URLEncoder.encode(user,charset)
                          , URLEncoder.encode(password, charset)
                          , URLEncoder.encode(rememberme, charset));
  
                  connection = (HttpURLConnection)url.openConnection();
                  connection.setRequestMethod("POST");
                  connection.setRequestProperty("Accept-Charset", charset);
                  connection.setDoOutput(true);           
                  connection.setFixedLengthStreamingMode(query.length());
  
                  out = connection.getOutputStream ();
                  out.write(query.getBytes(charset));
  
                  if (connection.getHeaderFields() == null){
                      Log.v(TAG, "Header null");
                  }else{
                      for (String cookie: connection.getHeaderFields().get("Set-Cookie")){
                          Log.v(TAG, cookie.split(";", 2)[0]);
                      }
                  }
              } catch (IOException e){
                  e.printStackTrace();
              } finally {
                  try { out.close();} catch (IOException e) { e.printStackTrace();}
                  connection.disconnect();            
              }
  </code></pre>
  
  <p>And here there is no output, it seems that connection.getHeaderFields() doesn't return result. It takes al least 30 seconds to show the Log:</p>
  
  <pre><code>10-02 16:56:25.918: V/class com.giorgi.myproject.activities.HomeActivity(2596): Header null
  </code></pre>
  
  <p>TESTED ON A GALAXY NEXUS</p>