• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1863
  • Last Modified:

How to read GZIP and Chunked HTTP file by java

I 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?
0
guochu
Asked:
guochu
  • 13
  • 9
1 Solution
 
MicheleMarconCommented:
for GZIP

http://java.sun.com/j2se/1.4.2/docs/api/java/util/zip/package-summary.html

for chunked data use byte buffers

for text... well use String or StringBuffer ;)
0
 
CEHJCommented:
The HttpClient lilbrary can handle this afaik

http://hc.apache.org/httpclient-3.x/
0
 
CEHJCommented:
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
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
guochuAuthor Commented:
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!!!
0
 
CEHJCommented:
>>  //fin is the instance of FileInputStream

If you were going to use home-brewed code (not sure why you would since the problems have already been solved for you) then you wouldn't use FileInputStream, you'd use GZIPInputStream
0
 
guochuAuthor Commented:
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 :(

0
 
CEHJCommented:
>>If I use GZIPInputStream right at the beginning to read this response message, it won't work at all :(

It will if you use HttpURLConnection
0
 
guochuAuthor Commented:
The response message is already saved in file. We need to extract information out from this file. How can the HttpURLConnection open and read this file?
0
 
CEHJCommented:
You just need to open a stream on the file. Try using something like below. If you can post an example file, i'll try it


URL url = new URL("thefile.htm");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();

Open in new window

0
 
guochuAuthor Commented:
I have attached an example file with the file extension log. You can change any extension as long as we can extract the data out from the file.
httpMessage.log
0
 
CEHJCommented:
OK thanks.

>>The response message is already saved in file

First of all though - why is it that you have this contained in files at all? Since you said:

>> I am writing a personal small web browser by using Java

Web browsers operate on a socket connection to a server
0
 
guochuAuthor Commented:
This is for testing the correctness of my application!
0
 
CEHJCommented:
Well unfortunately, it will make it operate in a very different way. Unless you create a small web server and write those files literally as the response
0
 
CEHJCommented:
(An HttpURLConnection [what you need] expects to operate on a web server, not a file)
0
 
guochuAuthor Commented:
Do you mean there is no way we can open this mixed data format file by using java?
0
 
CEHJCommented:
Opening it is no problem - it's just that HttpURLConnection is not designed to read files
0
 
CEHJCommented:
The following works fine for me, with a proper web server
import java.net.*;
import java.io.*;
import java.util.zip.*;
 
public class GzipWebRead {
    public static void main(String[] args) throws Exception {
	URL url = new URL(args[0]);
	HttpURLConnection conn = (HttpURLConnection)url.openConnection();
	conn.setRequestProperty("Accept-Encoding", "gzip");
	InputStream in = new GZIPInputStream(conn.getInputStream());
	int buf = -1;
	while((buf = in.read()) > -1) {
	    System.out.print((char)buf);
	}
	in.close();
    }
}

Open in new window

0
 
guochuAuthor Commented:
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.
0
 
CEHJCommented:
>>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
0
 
guochuAuthor Commented:
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.getBytes());
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-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.11) Gecko/20071220 BonEcho/2.0.0.11\r\nAccept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n
Accept-Language: en-us,en;q=0.5\r\nAccept-Encoding: gzip,deflate\r\nAccept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\nKeep-Alive: 300\r\nConnection: keep-alive\r\n\r\n

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!
0
 
CEHJCommented:
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
0
 
guochuAuthor Commented:
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,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n" +
                           "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=0.7\r\n" +
                           "Keep-Alive: 300\r\n" +
                           "Referer: http://www.originalsite.com/\r\n" +
                           "Connection: keep-alive\r\n\r\n"
 
url = "www.somesite.com";
port = 80;
sock = new Socket(url, port);
request = sock.getOutputStream();
request.write(sendMessage.getBytes());
 
Your method -
URL url = new URL("http://www.somesite.com");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestProperty("Accept-Encoding", "gzip,deflate");

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.com/", etc.,

By the way, can I repeat using setRequestProperty to set the other headers as below?

conn.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.11) Gecko/20071220 BonEcho/2.0.0.11");
conn.setRequestProperty("Accept", "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
conn.setRequestProperty("Accept-Language", "en-us,en;q=0.5");
conn.setRequestProperty("Accept-Encoding", "gzip,deflate");
conn.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
conn.setRequestProperty("Keep-Alive", "300");
conn.setRequestProperty("Connection", "keep-alive");

Thanks!!!
0
 
CEHJCommented:
>>By the way, can I repeat using setRequestProperty to set the other headers as below?

Should be OK, yes
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 13
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now