title: Read HTTP Parameters on TCP port using Java ServerSocket
body: <p>How would I read the HTTP Request parameters if I send an HTTP Request from a Java Servlet and receive it on a TCP port using ServerSocket. Can anyone please help me on this?</p>
  
  <p>Following is my design</p>
  
  <p>Servlet</p>
  
  <p>GET/POST using HttpURLConnection</p>
  
  <pre><code>    URL url = new URL("http://localhost:2309/");
      HttpURLConnection connection = (HttpURLConnection)url.openConnection();
      connection.setRequestProperty("Content-Type", "application/text");
      connection.setRequestProperty("Accept", "application/text");
      connection.setRequestMethod("POST");        
      connection.setRequestProperty("header1", "value1");
  </code></pre>
  
  <p>at localhost : 2309 I have a ServerSocket up and listening for a request from above servlet. I am trying to read the request but I only read the HTTP Headers, but I do not see the request parameters (I know in above example I am not sending any parameters, I had tried this by getting Output stream of the connection and writing to it).</p>
  
  <p>this is how I tried sending request parameters to my ServerSocket program.</p>
  
  <pre><code>byte[] parameters = someString.getBytes();
          OutputStream outStream = connection.getOutputStream();
          outStream.write(parameters);
  </code></pre>
  
  <p>Following is my ServerSocket program. </p>
  
  <pre><code>public static void main(String... args) {
      int port = 2309;
      ServerSocket sSocket = new sSocket(port);
      System.out.println("### SERVER IS UP AND RUNNING, WAITING FOR A CLIENT TO CONNECT ON " + port + " ###");
      Socket cSocket = sSocket.accept();
      System.out.println("### CONNECTION WITH THE CLIENT CREATED ###");
      BufferedReader readRequest = new BufferedReader(new InputStreamReader(cSocket.getInputStream()));
      PrintWriter writeResponse = new PrintWriter(cSocket.getOutputStream());
      String line = "";
      while (readRequest != null  &amp;&amp; (line = readRequest.readLine()) != null) {
          if (line.length() == 0)
              break;
          System.out.println(line);
      }
      writeResponse.write("HTTP/1.0 200 OK\r\n");
      writeResponse.write("Date: Fri, 31 Dec 1999 23:59:59 GMT\r\n");
      writeResponse.write("Server: Apache/0.8.4\r\n");
      writeResponse.write("Content-Type: text/html\r\n");
      writeResponse.write("Content-Length: 59\r\n");
      writeResponse.write("Expires: Sat, 01 Jan 2000 00:59:59 GMT\r\n");
      writeResponse.write("Last-modified: Fri, 09 Aug 1996 14:21:40 GMT\r\n");
      writeResponse.write("\r\n");
      writeResponse.write("&lt;TITLE&gt;Example&lt;/TITLE&gt;");
      writeResponse.write("&lt;P&gt;This is an example&lt;/P&gt;");
  }
  </code></pre>
  
  <p>Following is what I see on my ServerSocket program OUTPUT. </p>
  
  <pre><code>### SERVER IS UP AND RUNNING, WAITING FOR A CLIENT TO CONNECT ON 2309 ###
  ### CONNECTION WITH THE CLIENT CREATED ###
  POST / HTTP/1.1
  Content-Type: application/text
  Accept: application/text
  header1: value1
  Cache-Control: no-cache
  Pragma: no-cache
  User-Agent: Java/1.7.0_75
  Host: localhost:2309
  Connection: keep-alive
  ### CONNECTION WITH THE CLIENT TERMINATED ###
  </code></pre>
  
  <p>Can anyone suggest me </p>
  
  <ol>
  <li><p>how do I read the Request Parameters</p></li>
  <li><p>Writing to output stream on connection object, will it get me the
  request parameters right at the place?</p></li>
  <li><p>Is this a good approach, when I just want to keep an standalone
  server up, which will be just listening to the requests coming on to
  single port and serving it [OR] there is any better way through which
  I can perform this?</p></li>
  </ol>