Problem downloading rar file using HttpURLConnection
I'm trying to read a rar(archiving) file from the Internet using a HttpUrlConnection.
The program seems to work fine - however the download is not the same size as the file if downloaded through a browser.
The archive is 52,332kb when downloaded through a browser
and 4kb smaller when I try to download it.
When I try to extract the downloaded file (with winRAR) the header is corrupt and there is an unexpected end of archive.
Here is the pertinent code.
try
{
// configure and open connection
HttpURLConnection conn = (HttpURLConnection)currURL.openConnection();// currURL is a URL object
conn.setRequestProperty("Connection","Keep-Alive");
conn.setRequestProperty("Accept-Encoding","gzip,deflate" );
conn.setRequestProperty("Content-type","application/x-rar-compressed" );
conn.setRequestProperty("Cookie", cookie);
conn.connect();
// get an input stream from the HttpURLConnection object
InputStreamReader isr =new InputStreamReader( conn.getInputStream() );
BufferedReader in =new BufferedReader( isr );
/* the string file_name has been parsed from the url
* and is in the format file_name.rar */
File dest =new File("C:\\my_folder\\"+file_name);
// open output stream
OutputStream os =new FileOutputStream( dest );
OutputStream buffer =new BufferedOutputStream( os );
int data;
while ( (data = in.read() ) != -1 )
{
buffer.write(data);
}
// close streams
in.close();
os.close();
}
catch( IOException iox)
{
iox.printStackTrace();
}
Thanks in advance.

