title: HttpURLConnection Post: Output stream has no effect?
body: <p>I am developing an Android app and have already found out that different Android versions have a different ways on how they handle Http(s)URLConnections (http://stackoverflow.com/q/9556316/151682).</p>
  
  <p>I ran into the issue that Android 4 performs a POST request over HTTPS nicely, adding headers like Content-Type automatically when running the code below.</p>
  
  <p>However, on Android 2.3.5 (device &amp; emulator), any write to the output stream seems to be ignored - I debugged it with the web proxy Charles and while all headers are sent, the data written to the output stream is not sent along...</p>
  
  <p>Anyone knows how to solve this?</p>
  
  <p>Note: As the API I am developing against has only a self-signed certificate, I need to disable certification validation at this time.</p>
  
  <p>TIA, Patrick</p>
  
  <p><strong>Update</strong>
  In the meantime, I also have tried to following, to no avail:</p>
  
  <ul>
  <li>Calling <code>close()</code> after the <code>flush()</code> call on the BufferedOutputStream</li>
  <li>Calling <code>close()</code> on both the OutputStream and the BufferedOutputStream</li>
  <li>Using a OutputStreamWriter instead</li>
  <li><p>Not calling <code>close()</code> before calling <code>getInputStream()</code></p>
  
  <pre><code>HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  connection.setConnectTimeout(CONNECT_TIMEOUT);
  
  connection.setDoOutput(true); // Triggers POST.
  connection.setRequestMethod("POST");
  int contentLength = 0;
  if(body != null) {
      contentLength = body.getBytes().length;
  }
  
  // Workarounds for older Android versions who do not do that automatically (2.3.5 for example)
  connection.setRequestProperty(HTTP.TARGET_HOST, url.getHost());
  connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  
  // Set SSL Context -- Development only
  if(context != null &amp;&amp; connection instanceof HttpsURLConnection){
      HttpsURLConnection conn = (HttpsURLConnection)connection;
      conn.setSSLSocketFactory(context.getSocketFactory());
      conn.setHostnameVerifier(new HostnameVerifier() {
          @Override
          public boolean verify(String hostname, SSLSession session) {
              return true;
          }
      });
  }
  
  try{
      // Add headers
      if(headers != null){
          for (NameValuePair nvp : headers) {
              if(nvp != null){
                  connection.setRequestProperty(nvp.getName(), nvp.getValue());
              }
          }
      }
  
      connection.setFixedLengthStreamingMode(contentLength);
      OutputStream outputStream = null;
      try {
          if(body != null){
              outputStream = connection.getOutputStream();
              BufferedOutputStream stream = new BufferedOutputStream(outputStream);
              stream.write(body.getBytes()); // &lt;&lt;&lt;&lt; No effect ?!
              stream.flush();
  
          }
      } finally {
          if (outputStream != null) 
              try { 
                  outputStream.close(); 
              }
          catch (IOException logOrIgnore) {
              // ...
          }
      }
  
      InputStream inputStream = connection.getInputStream();
  
      // .... Normal case ....
  
  }
  catch(IOException e){
      // ... Exception! Check Error stream and the response code ...
  
  
  }
  finally{
      connection.disconnect();
  }
  </code></pre>
  
  <p>}</p></li>
  </ul>