Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to read with HttpURLConnection when transfer-encoding: chunked ?

 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My HttpURLConnection code was reading content from URLs fine until I came across pages with transfer-encoding: chunked.

Can anyone help me out with the best way to read such (textual) content?

Thanks,
James
 
Paul Clapham
Sheriff
Pie
Posts: 20755
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best way is the simplest way (i.e. you just read bytes from the InputStream you get from the connection).
 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul. I have tried that, but when I get display the content, it is just a load of garbage (i.e., rather than html).

I've noticed with my HttpTracer that the response I'm trying to read includes lines that have "1000" content - presumably that describe the length of the following block and are causing a problem when trying to read the actual content. Do you know how I can handle this?

Cheers,
James

PS - As chunked transfer encoding came around in HTTP 1.1, do you know if it's possible for me to change my request GET header to HTTP/1.0 as hopefully that would mean the response wouldn't then be chunked?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic