• Post Reply Bookmark Topic Watch Topic
  • New Topic

Downloading Files Using Commons HttpClient

 
Ilya PA
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear reader,

I've been trying to figure this out since monday, searching through bunches of google groups and what not.. without finding answers. See if you guys have any idea of what's causing this to fail so miserably.

I am trying to programmatically download a file from a website. The way it works manually is as follows:

1. User clicks on a 'Download' button
2. HTTP GET is sent to the server:
http://yyy.com/send-file.cgi?action=Download&filename=myfile.000
3. In response a dialog box pops up, user is prompted to choose path and save the file
4. User clicks save, and file is downloaded

The dialog box pops up because the server's response header has Content-Disposition set to attachment. This way the file is not stored in the message body, but in the attachment. Here are the server's response headers:
Server: HTTPD/1.0.0
Content-Disposition: attachment; filename=myfile.000
Content-Length: 0
Vary: Accept-Encoding,User-Agent
Content-Type: application/x-force-download

Commons' HttpClient, provides methods to read the message body, headers and footers, however I could not figure out whether the attachment is stored in either of the three, or whether I need to look for another API (suggestions welcomed!) to get that file in from an application.

Thanks ahead for any ideas/direction,

Kind regards,
Ilya.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should be in the body. What do you get when you call getResponseBody(asStream)? It should be rather big because the file should be in there.
 
Ilya PA
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, here is my code that does the read with body returned as a byte array:



And here is the output it produces:


http://yyy.com/send-file.cgi?action=Download&filename=test.000
The answer is []


If the code is right, then it appears like the body is empty.
Here is the code of the same thing with getResponseBodyAsStream():


And the output for this one is:


http://yyy.com/send-file.cgi?action=Download&filename=test.000
The body is []


Am I doing something wrong?

Thank you,
Ilya.

[ November 23, 2005: Message edited by: Ilya PA ]

[ November 23, 2005: Message edited by: Ilya PA ]
[ November 23, 2005: Message edited by: Ilya PA ]
 
Ilya PA
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I figured it out. It was my own problem, the site that I had to connect to and download files from had a bunch of hidden fields, which were necessary for successful connection. I got to know about only after wasting couple of days trying to figure out whats wrong with GET, when all I needed was a POST with a bunch of parameters hidden on the form.

Cheers,
Ilya.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!