Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Help with HTTP I/O

 
Greenhorn
Posts: 2
Google Web Toolkit Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
     
    Sheriff
    Posts: 26958
    84
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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?
     
    WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic