title: Handling POST request via Socket in Java
body: <p>I'm trying to handle a simple POST Request in Java using a Socket. 
  I can receive the request header and answer the request without any problem, but I certainly can not get the body of the request.</p>
  
  <p>I read somewhere that I'd need to open a second InputStream to achive this, but this doesn't really makes sense to me. Do you have any tips on how to get the request body?</p>
  
  <p>This is what I basically use to get the header:</p>
  
  <pre><code>BufferedReader in = new BufferedReader(new InputStreamReader(
                  clientSocket.getInputStream()));
  
  char[] inputBuffer = new char[INPUT_BUFFER_LENGTH];
  
  int inputMessageLength = in.read(inputBuffer, 0,
                  INPUT_BUFFER_LENGTH);
  
  String inputMessage = new String(inputBuffer, 0, inputMessageLength);
  </code></pre>
  
  <p>So, the message I get is something like:</p>
  
  <pre><code>POST / HTTP/1.1
  User-Agent: Java/1.8.0_45
  Host: localhost:5555
  Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
  </code></pre>
  
  <p>But I can't get the parameters of the POST request.</p>
  
  <p><em>Edit:</em></p>
  
  <p>So it turned out I just had <strong>INPUT_BUFFER_LENGTH</strong> up high enough (I know, shame on me).
  So as it worked I changed my <em>ServerSocket</em> to <em>SSLServerSocket</em> and tried again to send a request with a <em>HttpsUrlConnection</em> from Java, now I have the same problem again (already checked the buffer), getting something like this:</p>
  
  <pre><code>POST / HTTP/1.1
  User-Agent: Java/1.8.0_45
  Host: localhost:5555
  Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
  Connection: keep-alive
  Content-type: application/x-www-form-urlencoded
  Content-Length: 128
  
  *Missing Body*
  </code></pre>
  
  <p>It turned out I only get this when sending requests with my Java-Client - Sending requests from Chrome, etc are working fine - so I assume I got something wrong in my code.
  This is what I use to send the request:</p>
  
  <pre><code>System.setProperty("javax.net.ssl.trustStore", ...);
  System.setProperty("javax.net.ssl.trustStorePassword", ...);
  
  SSLSocketFactory socketFactory = (SSLSocketFactory) SSLSocketFactory
              .getDefault();
  
  String url = "https://...";
  URL obj = new URL(url);
  HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
  
  HttpsURLConnection.setDefaultSSLSocketFactory(socketFactory);
  
  con.setRequestMethod("POST");
  con.setDoOutput(true);
  
  OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());
  
  writer.write(*Some String*);
  writer.flush();
  writer.close();
  </code></pre>
  
  <p>Any tips on what might be wrong with my code?</p>