This discussion is archived
10 Replies Latest reply: Apr 11, 2007 6:56 PM by EJP RSS

reading HTTP POST data from a socket

807596 Newbie
Currently Being Moderated
Hi,

I wanna read a stream of HTTP data from a socket. The most used method for the same seems to be

DataInputStream dis = new DataInputStream(client.getInputStream());
String strTemp = new String();

while( (strTemp = dis.readLine()) != null) {
     data += strTemp;
}

The problem is this loop never terminates even after reaching end of stream. The HTTP headers are read successfully.For a POST data with multiple '\r\n', when i want to retrieve the payload data from the HTTP request, how do i do it?

Anyone faced the same problem or knows the solution to this??

Any help would be appreciated,

Thnx in advance,

Regds,
Meesh
  • 1. Re: reading HTTP POST data from a socket
    807596 Newbie
    Currently Being Moderated
    On the point of efficiency, a new String() is the same as a "" for your purposes.
    A String is immutable so you += create a new appended string each time. Very inefficient for a large post.
    DataInputStream is used to reading binary data and readLine is deprecated.

    I suggest you use a class designed for using text.
    new BufferedReader(new InputStreamReader(client.getInputStream()))

    Then use a StringBuffer or StringBuilder to append each line.
    Note: readLine strips newline characters. If you want to keep them I suggest you use readChars or append the newlines extra.

    Better yet you could use the IOUtils class in the apache's commin IO library. With this you can do..
    String text = IOUtils.toString(client.getInputStream());
  • 2. Re: reading HTTP POST data from a socket
    807596 Newbie
    Currently Being Moderated
    Hi Peter,

    Thnx for your response. But using BufferedReader or StringBuffer or readChars/readLine will(does) not solve my problem. After reading the http headers, when the stream encounters multiple '\r\n' reading fails. The while loop never terminates. I want a means to read the data that comes in after the HTTP headers.

    Regds,
    Meesh.
  • 3. Re: reading HTTP POST data from a socket
    807596 Newbie
    Currently Being Moderated
    The connection you receive is probably one of the kind: connection: keep-alive. Which means the stream will be kept open and you will not receive an eof marker. Thus the readline method will wait until input will become ready, which will never happen. You could solve this by checking if data is available for reading.
  • 4. Re: reading HTTP POST data from a socket
    807596 Newbie
    Currently Being Moderated
    readLine() is deprecated, as a rule, always stay away from deprecated methods.

    As for your problem, read the HTTP RFC first so you know what an HTTP response looks like. What you do is create a while loop that will read all data available until you reach the end of the header (the double CRLF or /r/n as you mention). At that point, parse the header you just read (convert them to strings, read the key/value pairs). Look for Content-Length if it exists. If it does, read data until total request bytes = Content-Length header field value. If no Content-Length is defined read until EOF. Depending on the Content-Type header field there will be either binary or textual data in the message body of the HTTP response. Again it's easier to read all X bytes of the message body (where X is the Content-Length or the number of bytes you can read without getting an EOF). Then throw that byte array into the appropriate parser depending on the content-type.
  • 5. Re: reading HTTP POST data from a socket
    807597 Newbie
    Currently Being Moderated
    I still have the problem. After getting the content-length, i used the break command to quit the while loop. But the statement read(content-length); is still blocked.
  • 6. Re: reading HTTP POST data from a socket
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    I still have the problem. After getting the
    content-length, i used the break command to quit the
    while loop. But the statement read(content-length);
    is still blocked.
    You shouldn't be in a while loop once you attempt to read the content itself. Once you have the content length and you are at the content itself all you do is read the number of bytes specified by the content length and then terminate.

    You will find that you will also want to put a timeout on the read.
  • 7. Re: reading HTTP POST data from a socket
    807597 Newbie
    Currently Being Moderated
    The problem you are probably seeing is that after doing a readLine, the Reader is looking for the end of the next line which it may never see, because the contents is not ended by a newline.
  • 8. Re: reading HTTP POST data from a socket
    807597 Newbie
    Currently Being Moderated
    One solution:
    List<String> headers = new ArrayList<String>();
    String str;
    while ((str = reader.readLine()) != null)
    {
       headers.add(str);
       if (str.startsWith("Content-Length: "))
       {
          break; // and don't get the next line!
       }
    }
  • 9. Re: reading HTTP POST data from a socket
    807597 Newbie
    Currently Being Moderated
    And to get the actual content:
    int contentLength = Integer.parseInt(headers
                             .get(headers.size() - 1).substring(
                                       "Content-Length: ".length()));
    
    StringBuilder requestContent = new StringBuilder();
    int ch;
    for (int i = 0; i < contentLength; i++)
    {
       requestContent.append((char) reader.read());
    }
  • 10. Re: reading HTTP POST data from a socket
    EJP Guru
    Currently Being Moderated
    A_K_S, this thread is six months old. Save your breath.