You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Aegir Sun <Ae...@Sun.COM> on 2005/08/19 05:57:15 UTC

Odd behavior for httpclient?

Hi,

I am using httpclient v3 rc2. I want to use it to implement a downloader.
But when it downloads a html file, odd behavior occurs. I write a test java
class to demonstrate this.

/------------------------------------------------------------------------------------------
package test;

import java.io.FileOutputStream;
import java.io.InputStream;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;

public class Test {
    public static void main(String[] args) {
        HttpClient httpClient = new HttpClient();
       
        GetMethod method = new GetMethod("http://www.sf.net/index.php");
        method.addRequestHeader("Pragma", "no-cache");
        method.addRequestHeader("Cache-Control", "no-cache");
       
        try {
            httpClient.executeMethod(method);
           
            InputStream input = method.getResponseBodyAsStream();
            FileOutputStream out = new FileOutputStream("c:\\index.php");
            byte buffer[] = new byte[1024];
            while ( input.read(buffer) != -1) {
                out.write(buffer);
            }
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
---------------------------------------------------------------------------------/

You know, the download file for http://www.sf.net/index.php is much 
larger than the real target file.
Can anybody tell me what happened? And how to solve this problem? Thanks.

Regards,
Aegir

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org


Re: Odd behavior for httpclient?

Posted by Aegir Sun <Ae...@Sun.COM>.
My mistake. Thanks for pointing it out. It works fine now.

Regards,
Aegir

Roland Weber wrote:

>Hello Aegir,
>
>  
>
>>            byte buffer[] = new byte[1024];
>>            while ( input.read(buffer) != -1) {
>>                out.write(buffer);
>>            }
>>
>>You know, the download file for http://www.sf.net/index.php is much 
>>larger than the real target file.
>>Can anybody tell me what happened? And how to solve this problem? 
>>    
>>
>Thanks.
>
>In each iteration, you save the whole buffer even if only a few bytes
>have been read into it. You might have noticed this yourself by having
>a look at the contents of the file you created.
>
>cheers,
>  Roland
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: httpclient-user-help@jakarta.apache.org
>
>  
>


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org


Re: Odd behavior for httpclient?

Posted by Roland Weber <RO...@de.ibm.com>.
Hello Aegir,

>             byte buffer[] = new byte[1024];
>             while ( input.read(buffer) != -1) {
>                 out.write(buffer);
>             }
>
> You know, the download file for http://www.sf.net/index.php is much 
> larger than the real target file.
> Can anybody tell me what happened? And how to solve this problem? 
Thanks.

In each iteration, you save the whole buffer even if only a few bytes
have been read into it. You might have noticed this yourself by having
a look at the contents of the file you created.

cheers,
  Roland


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org