title: Interesting issue with POST requests (HttpURLConnection) - But why does it happen?
body: <p>So I just spent 6 hours worth of work, figuring out this 'little' fact:
  Without calling client.getResponseCode() THE POST REQUEST DOES NOT GO THROUGH.</p>
  
  <p>I hope that someone can explain why!
  Specifically, for this minimalistic android client standalone code literally nothing happens without the line  <code>int status = client.getResponseCode();</code>
  but with it,everything works like magic.
  I didn't find any official documentation about this, so I'm wondering what's up, or what I don't understand (The people implementing Java usually do outstanding job, so it's probably me not getting something :) ). </p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          final String theUrl = "http://xx.yyy.zz.aa:bb/securitiesFollowServer/users";
          final String TAG = getClass().getSimpleName();
  
          AsyncTask&lt;Void, Void, Void&gt; task = new AsyncTask&lt;Void, Void, Void&gt;() {
              @Override
              protected Void doInBackground(Void... params) {
                  String body = "BODY OF MESSAGE";
  
                  HttpURLConnection client = null;
                  BufferedWriter outputPost = null;
  
                  try {
                      URL url = new URL(theUrl);
  
                      //  open the connection
                      client = (HttpURLConnection) url.openConnection();
                      client.setRequestProperty("content-type", "text/plain");
                      //client.setRequestMethod("POST"); Someone claimed that setDoOutput(true) works so I will try that instead (I tried both,mutually and exclusively so all 3 options).
                      client.setDoOutput(true);
  
                      outputPost = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
                      outputPost.write(body);
                      outputPost.flush();
                      outputPost.close();
                      int status = client.getResponseCode();
                      StackTraceElement[] r = Thread.currentThread().getStackTrace();
                      String toNotepad = "";
                      for (int i = 0; i &lt; r.length; ++i) {
                          toNotepad += '\n' + String.valueOf(i) + '.' + ':' + r[i].toString();
                      }
                      // This is where I set a breakpoint and got the stacktrace value from toNotepad,and copy pasted it.
                  } catch (IOException e) {
                      Log.e(TAG, "Error ", e);
                  } finally {
                      if (client != null) {
                          client.disconnect();
                      }
                      if (outputPost != null) {
                          try {
                              outputPost.close();
                          } catch (IOException e) {
                              Log.e(TAG, "IO ERROR");
                          }
                      }
  
                      return null;
                  }
              }
          };
          task.execute();
      }
  }
  </code></pre>
  
  <p>For completeness of the question, here is the "server side" minimalistic code</p>
  
  <pre><code>@POST
  @Consumes(MediaType.TEXT_PLAIN)
  @Produces(MediaType.TEXT_PLAIN)
  public String setDebugResource(String a){
      return "I got this string:"+a;}
  </code></pre>
  
  <p>And here is the stacktrace (I made it obvious in the code above where exactly I copy-pasted its value):</p>
  
  <p>When not working(or working, it is exactly the same stacktrace).:</p>
  
  <pre><code>0.:dalvik.system.VMStack.getThreadStackTrace(Native Method)
  1.:java.lang.Thread.getStackTrace(Thread.java:580)
  2.:dor.only.dorking.android.apppostrequest.MainActivity$1.doInBackground(MainActivity.java:52)
  3.:dor.only.dorking.android.apppostrequest.MainActivity$1.doInBackground(MainActivity.java:28)
  4.:android.os.AsyncTask$2.call(AsyncTask.java:292)
  5.:java.util.concurrent.FutureTask.run(FutureTask.java:237)
  6.:android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
  7.:java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
  8.:java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
  9.:java.lang.Thread.run(Thread.java:818)
  </code></pre>