You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Stephen Charles Huey <st...@fastmail.fm> on 2003/08/12 17:54:08 UTC

HELP with HttpClient in downloading a file

Is there no one out there who knows anything about HttpClient?  I can't
figure out how to download a file using this thing.  Before the following
code, I successfully manage to log into an HTTPS site and submit another
form to get a listing of data files ready for download, and after that
POST, I can get the HTML from the file listing using
getResponseBodyAsString(), so from there I parse out the URLs I need and
pass them on to my downloadFiles() method--which currently only tries to
do this with one URL and file:

    private void downloadFiles(String toDownload) {
        GetMethod oGetFile = new GetMethod(toDownload);
        try { oClient.executeMethod(oGetFile); }
        catch(HttpException e) {e.printStackTrace(); }
        catch(IOException e) {e.printStackTrace(); }
        System.out.println(oGetFile.getStatusCode() + " : " +
        oGetFile.getStatusText() + " \n\n");
        oGetFile.releaseConnection();
        try 
        {  
            InputStream is = oGetFile.getResponseBodyAsStream();
            FileOutputStream fos = new
            FileOutputStream("c:\\downloaded");
            int c;
            while((c=is.read()) != -1) {    
                 fos.write(c);  
            }  
            is.close();
            fos.flush();  
            fos.close();
        }
        catch (FileNotFoundException fnfe) { System.out.println("FNFE:
        "+fnfe.toString()); }
        catch (IOException ioe) { System.out.println("IOE:
        "+ioe.toString()); }
        catch (NullPointerException e) { System.out.println("NPE: " +
        e.toString()); }
    }

The above code returns 200 : OK from getStatusCode and getStatusText, but
then it throws a NullPointerException when I get to the while statement. 
There is definitely some data in this file, so why am I getting a
NullPointerException???  Or is there some other way to try to download a
file?  Thanks so much in advance...

Stephen

Re: HELP with HttpClient in downloading a file

Posted by Stephen Charles Huey <st...@fastmail.fm>.
Thank you very much!

Re: HELP with HttpClient in downloading a file

Posted by Andreas Probst <an...@gmx.net>.
Hi Stephen,

I think you must not releaseConnection(), before you read the 
actual file. In releaseConnection() close() is called on 
responseStream (see class HttpMethodBase). Hence the NPE. Try to 
put the release call into the finally branch.

Regards.
Andreas

On 12 Aug 2003 at 9:54, Stephen Charles Huey wrote:

> Is there no one out there who knows anything about HttpClient?  I
> can't figure out how to download a file using this thing.  Before
> the following code, I successfully manage to log into an HTTPS
> site and submit another form to get a listing of data files ready
> for download, and after that POST, I can get the HTML from the
> file listing using getResponseBodyAsString(), so from there I
> parse out the URLs I need and pass them on to my downloadFiles()
> method--which currently only tries to do this with one URL and
> file:
> 
>     private void downloadFiles(String toDownload) {
>         GetMethod oGetFile = new GetMethod(toDownload);
>         try { oClient.executeMethod(oGetFile); }
>         catch(HttpException e) {e.printStackTrace(); }
>         catch(IOException e) {e.printStackTrace(); }
>         System.out.println(oGetFile.getStatusCode() + " : " +
>         oGetFile.getStatusText() + " \n\n");
>         oGetFile.releaseConnection(); try {  
>             InputStream is = oGetFile.getResponseBodyAsStream();
>             FileOutputStream fos = new
>             FileOutputStream("c:\\downloaded"); int c;
>             while((c=is.read()) != -1) {    
>                  fos.write(c);  
>             }  
>             is.close();
>             fos.flush();  
>             fos.close();
>         }
>         catch (FileNotFoundException fnfe) {
>         System.out.println("FNFE: "+fnfe.toString()); } catch
>         (IOException ioe) { System.out.println("IOE:
>         "+ioe.toString()); } catch (NullPointerException e) {
>         System.out.println("NPE: " + e.toString()); }
>     }
> 
> The above code returns 200 : OK from getStatusCode and
> getStatusText, but then it throws a NullPointerException when I
> get to the while statement. There is definitely some data in this
> file, so why am I getting a NullPointerException???  Or is there
> some other way to try to download a file?  Thanks so much in
> advance...
> 
> Stephen
>