The HttpClient lilbrary can handle this afaik
http://hc.apache.org/httpc
Main Topics
Browse All TopicsI am writing a personal small web browser by using Java. How can I read a file that contains ASCII text, compressed (by gzip) and chunked data file?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
The HttpClient lilbrary can handle this afaik
http://hc.apache.org/httpc
It could be that it's not handled transparently though. Here is an example using the aforementioned API that wraps a POST method. Something similar could easily be done for a GET method
http://tinyurl.com/5b46ec
I need to write my own code to my small application. Now, if I can solve the problem below, then everything would be fine!
For example:
byte data [ ] = new byte [10];
int size = 0;
size = fin.read(data); //fin is the instance of FileInputStream
...
now the content of data is containing GZIP data, e.g [31, -117, 8, 0, 0, 0, 0, 0, 4, 0]
How can I use GZIP to decompress this array?
Thanks!!!
When you start reading the http response message, the first several lines are ASCII text then follows with compressed and chunked data.
For example:
HTTP/1.1 200 OK
Cache-Control: no-cache
Connection: close
...
a
9
578
ÄWmS"9þ~U÷²s
...
0
fin reads up to the end of ASCII text, then continues to read the first chunked data. the first letter "a" means the first chunked data has 10 bytes -
byte data [ ] = new byte [10];
int size = 0;
size = fin.read(data); //i.e [31], [-117], [8], [0], [0], [0], [0], [0], [4], [0]
My question is how to decompress this array? How can I decode this chunked data to ASCII text?
If I use GZIPInputStream right at the beginning to read this response message, it won't work at all :(
Thanks CEHJ...
However, I tried to run your program and didn't work. I am not sure your problem can open a file offline.
I need a method can open and read the file (the one I attached before) offline. Do you have some idea about FilterInputStream? I am still learning how to use this class. I think this maybe the right direction should go for it.
>>I am not sure your problem can open a file offline.
No - i've already said that HttpURLConnection is not designed to read files. If your objective is to be a real web client, i suggest, as i've also already said, that you make a test server that simply writes your files on client (the one i posted) connect
Hello CEHJ,
I have been thinking a couple of days about your method. If you can custom make the http request, then it will be perfect!
--------------------------
For example, I use socket to make connection, I can program the send message whatever I want.
--------------------------
sock = new Socket(url, port);
request = sock.getOutputStream();
response = sock.getInputStream();
request.write(sendMessage.
length = response.read(recv);
if(length > 0){
message = new String (recva);
System.out.println(message
}
--------------------------
The sendMessage is a long string -
sendMessage = GET / HTTP/1.1\r\nHost: www.somesite.com\r\nUser-A
Accept-Language: en-us,en;q=0.5\r\nAccept-E
Can you make the same http request (as above) by using your method? If yes, I would like to see how! Please let me know! Thanks!
The problem you have is not in making the request. The code i posted works fine - i tested it with my web server. Your problem is, as i've said more times than i care to remember now, that you're trying to read from files instead of connecting to a web server. If you want to keep testing against files instead of a real web server, then you need to follow my last suggestion
Yes, your code is working perfectly. I have completely changed my program because of your suggestion.
I just want to know a bit more about your method.
My method -
sendMessage = "GET /path/example.html HTTP/1.1\r\n" +
"Host: www.somesite.com\r\n" +
"User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.11) Gecko/20071220 BonEcho/2.0.0.11\r\n" +
"Accept: text/xml,application/xml,a
"Accept-Language: en-us,en;q=0.5\r\n" +
"Accept-Encoding: gzip,deflate\r\n" +
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q
"Keep-Alive: 300\r\n" +
"Referer: http://www.originalsite.co
"Connection: keep-alive\r\n\r\n"
url = "www.somesite.com";
port = 80;
sock = new Socket(url, port);
request = sock.getOutputStream();
request.write(sendMessage.
Your method -
URL url = new URL("http://www.somesite.c
HttpURLConnection conn = (HttpURLConnection)url.ope
conn.setRequestProperty("A
Can I use method two to do the same thing like method one?
Method one - I can make the sendMessage headers whatever I want.
Method two - what are the methods for "GET /path/example.html HTTP/1.1", "Referer: http://www.originalsite.co
By the way, can I repeat using setRequestProperty to set the other headers as below?
conn.setRequestProperty("U
conn.setRequestProperty("A
conn.setRequestProperty("A
conn.setRequestProperty("A
conn.setRequestProperty("A
conn.setRequestProperty("K
conn.setRequestProperty("C
Thanks!!!
Business Accounts
Answer for Membership
by: MicheleMarconPosted on 2008-06-10 at 23:14:08ID: 21757267
for GZIP
.4.2/docs/ api/java/u til/zip/ pa ckage-summ ary.html
http://java.sun.com/j2se/1
for chunked data use byte buffers
for text... well use String or StringBuffer ;)