title: HTTP POST request on Android
body: <p>For the last two hours i've been trying to make a POST request to this page <a href="http://www.halebop.se/butik/byt_behall_nummer/" rel="nofollow">http://www.halebop.se/butik/byt_behall_nummer/</a> and tried to send <strong>numberToPort</strong>. However i get a bunch of cookies and a 302 moved temporarily back.</p>
  
  <p>All i want to do is send the POST request with the number and get the final page back. On iOS, i do this using ASIHTTPRequest which handles the redirect and cookies.</p>
  
  <p>iOS code:</p>
  
  <pre><code>NSString *halebopURLString = @"http://www.halebop.se/kontantkort/byt_behall_nummer/#";
  ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:halebopURLString]];
  [request setPostValue:halebopNumber forKey:@"numberToPort"];
  [request setPostValue:@"continue" forKey:@"action"];
  [request setPostValue:@"submit" forKey:@"submit"];
  [request startSynchronous];
  </code></pre>
  
  <p>How do i do this on Android?</p>
  
  <p>As an alternative, a PHP solution is acceptable.</p>
  
  <p>Edit: Tried this, it gives no output and no exceptions. I have the internet permission. Expected result: Send POST, get 302 and cookies back, send cookies to URL from 302 and get HTML back (Checked with FireBug) however i get nothing. </p>
  
  <pre><code>try {
          InputStream myInputStream =null;
          URL url;
          url = new URL("http://www.halebop.se/kontantkort/byt_behall_nummer/#");
          HttpURLConnection conn = (HttpURLConnection) url.openConnection();
          conn.setDoOutput(true);
          conn.setInstanceFollowRedirects(true);
          conn.setRequestMethod("POST");
          OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
          wr.write("numberToPort="+n+"&amp;action=continue&amp;submit=submit");
          wr.flush();
          myInputStream = conn.getInputStream();
          wr.close();
  
          BufferedReader rd = new BufferedReader(new InputStreamReader(myInputStream), 4096);
          String line;
          StringBuilder sbResult =  new StringBuilder();
          while ((line = rd.readLine()) != null) {
              sbResult.append(line);
              Log.d(TAG, "Line "+line);
          }
          rd.close();
          String contentOfMyInputStream = sbResult.toString();
          Log.d(TAG, "Output "+contentOfMyInputStream);
      } catch (Exception e) {
          Log.d(TAG,e.getMessage());
      }
  </code></pre>