Only 48 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with HTTP I/O  RSS feed

 
Michael Ramirez
Greenhorn
Posts: 2
Eclipse IDE Firefox Browser Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I'm new to the ranch but I'm hoping you guys can help me out with a small I/O problem.
I'm trying to learn a bit more about HTTP protocol, so I wrote some Java code to send & receive text HTTP messages over Sockets.

For example, I might send: "GET /somepath/somefile.html HTTP/1.0\r\n\r\n", and listen for a response from the web server.
However, I run into problems when I request a non-text file (like an Image), I can't figure out how to process it. A website I'm reading states:

An HTTP message may have a body of data sent after the header lines. In a response, this is where the requested resource is returned to the client (the most common use of the message body)

  • If its not text, how do I process the body? Do I just read raw bytes from the InputStream?
  • How can I tell where the 'Header Line' ends & the 'Message Body' starts? Do I simply check for "\r\n\r\n"?
  • How do I determine the byte length of the content? Can I trust the "Content-Length" field? or Do I loop until EOF?

  • I can't get my program to work with non text-files. I tried reading the HTTP RFC, but it didn't help much.
    -Mike

    http://en.wikipedia.org/wiki/HTTP_body_data
    http://www.jmarshall.com/easy/http/#messagebody
     
    Paul Clapham
    Sheriff
    Posts: 22185
    38
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Michael Ramirez wrote:
  • If its not text, how do I process the body? Do I just read raw bytes from the InputStream?

  • Yes, that's right. Don't use a Reader, stick to the InputStream.
  • How can I tell where the 'Header Line' ends & the 'Message Body' starts? Do I simply check for "\r\n\r\n"?

  • I think that's right; the RFC should tell you that.
  • How do I determine the byte length of the content? Can I trust the "Content-Length" field? or Do I loop until EOF?

  • If I'm not mistaken, you might find there is no Content-Length header. In that case you have to loop until EOF. Otherwise I think you're supposed to trust the Content-Length header; doesn't the RFC have a SHOULD or a MUST for that?
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!