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 Sheetal D <sh...@yahoo.com> on 2006/04/03 11:18:25 UTC

Request

Hi all,
         I have a doubt on the way Http Client response bieng read,  HttpMethod returns Stream or String as response content. I see no method returns byte as output. Is there any reason for that 
   
  Thanks,
  Sheetal
   

		
---------------------------------
Talk is cheap. Use Yahoo! Messenger to make PC-to-Phone calls.  Great rates starting at 1&cent;/min.

RE: Request

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Mon, 2006-04-03 at 15:48 +0530, Ramit bhardwaj wrote:
>  Where does it say to use Stream after a certain limit?? I am pasting the API
> here:
> 
> 
> getResponseBody
> byte[] getResponseBody()
>                        throws IOException
> Returns the response body of the HTTP method, if any, as an array of bytes.
> If the method has not yet been executed or the response has no body, null is
> returned. Note that this method does not propagate I/O exceptions. If an
> error occurs while reading the body, null will be returned. 
> 
> Returns:
> The response body, or null if the body is not available. 
> Throws: 
> IOException - if an I/O (transport) problem occurs
> 
> 
> 
> I am using it for a highly avaliable mission critical enterprise application
> and it seems to work fine. Hence  I am curious to know...
> 
> 

Ramit,

This is actually correct. See
http://jakarta.apache.org/commons/httpclient/xref/org/apache/commons/httpclient/HttpMethodBase.html#652

The use of HttpMethod#getResponseBody and
HttpMethod#getResponseBodyAsString is *strongly* discouraged. Both
methods attempt to buffer the entire response content in memory. A badly
written or malicious server-side script can bring your app down with an
OutOfMemoryError, if the server generates more data that your JRE can
allocate. That is why it is generally recommended to use
HttpMethod#getResponseBodyAsStream method if your application must be
prepared to deal with any arbitrary responses including those that
generate infinite content stream, and introduce additional application
specific safe-guards in your code (such as method abort after reaching a
certain content length threshold) 

Oleg

> 
> -----Original Message-----
> From: Sheetal D [mailto:sheetal_d_2006@yahoo.com] 
> Sent: Monday, April 03, 2006 3:31 PM
> To: HttpClient User Discussion
> Subject: RE: Request
> 
> Thanks, But it tells to use Stream after a certain limit.
>    
>   Thanks,
>   Sheetal
>   
> 
> Ramit bhardwaj <ra...@cybage.com> wrote:
>   
> 
> What about getResponseBody() method. It returns a byte array.
> 
> 
> -----Original Message-----
> From: Sheetal D [mailto:sheetal_d_2006@yahoo.com]
> Sent: Monday, April 03, 2006 2:48 PM
> To: httpclient-user@jakarta.apache.org
> Subject: Request
> 
> Hi all,
> I have a doubt on the way Http Client response bieng read, HttpMethod
> returns Stream or String as response content. I see no method returns byte
> as output. Is there any reason for that 
> 
> Thanks,
> Sheetal
> 
> 
> 
> ---------------------------------
> Talk is cheap. Use Yahoo! Messenger to make PC-to-Phone calls. Great rates
> starting at 1¢/min.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-user-help@jakarta.apache.org
> 
> 
> 
> 		
> ---------------------------------
> New Yahoo! Messenger with Voice. Call regular phones from your PC and save
> big.
> 
> 
> ---------------------------------------------------------------------
> 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: Request

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

>                                  I want to pass the InputStream to 
> the other client. As I see,Once the connection is closed, The stream
> will be invalidated. I want to have the reference even after closing
> the connection. Can I do that. 

No. Sorry. You'll have to get the data as a byte array (which is
strongly discouraged) or copy it yourself from the InputStream
(with appropriate length checking). Then you can create a new
ByteArrayInputStream on top of the copied data.

cheers,
  Roland


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


Re: Request

Posted by Sheetal D <sh...@yahoo.com>.
HI all,
   
  Thanks for all the replies.
                                 I want to pass the InputStream to the other client. As I see,Once the connection is closed, The stream will be invalidated. I want to have the reference even after closing the connection. Can I do that. 
  The InputStream object returned by the  getResponseAsStream mehod  is AutoCloseInputStream.
  Could anybody tell me how can I keep this alive even after closing the connection. 
  One option is to read from getInputStream and create a new byteinputstream and 
  pass it on. Again they will read it from the inputStream, I just want to cut one read and can pass InputStream object directly having the connection closed.
   
  Thanks,
  Sheetal
   
   
   
   
   
   
  

Chaitanya Ravi <r....@gmail.com> wrote:
  Hi Sheetal,

You can use the below code to read ur response.


httpclientR.executeMethod(postMethod);
InputStream is = postMethod.getResponseBodyAsStream();
BufferedInputStream bis = new BufferedInputStream( is );

String datastr = null;
StringBuffer sb = new StringBuffer();
byte[] bytes = new byte[ 8192 ]; // reading as chunk of 8192
bytes
int count = bis.read( bytes );
while( count != -1 && count <= 8192 )
{
datastr = getString(bytes, count);
sb.append(datastr);
count = bis.read( bytes );
}

bis.close();
reply = sb.toString();

Regards,
On 4/3/06, Sheetal D wrote:
>
> I got a warning saying
> "Going to buffer of large or unknown, getResponseAsStream is recommonded".
>
>
> Thanks,
> Sheetal
>
> Ramit bhardwaj wrote:
>
> Where does it say to use Stream after a certain limit?? I am pasting the
> API
> here:
>
>
> getResponseBody
> byte[] getResponseBody()
> throws IOException
> Returns the response body of the HTTP method, if any, as an array of
> bytes.
> If the method has not yet been executed or the response has no body, null
> is
> returned. Note that this method does not propagate I/O exceptions. If an
> error occurs while reading the body, null will be returned.
>
> Returns:
> The response body, or null if the body is not available.
> Throws:
> IOException - if an I/O (transport) problem occurs
>
>
>
> I am using it for a highly avaliable mission critical enterprise
> application
> and it seems to work fine. Hence I am curious to know...
>
>
>
> -----Original Message-----
> From: Sheetal D [mailto:sheetal_d_2006@yahoo.com]
> Sent: Monday, April 03, 2006 3:31 PM
> To: HttpClient User Discussion
> Subject: RE: Request
>
> Thanks, But it tells to use Stream after a certain limit.
>
> Thanks,
> Sheetal
>
>
> Ramit bhardwaj wrote:
>
>
> What about getResponseBody() method. It returns a byte array.
>
>
> -----Original Message-----
> From: Sheetal D [mailto:sheetal_d_2006@yahoo.com]
> Sent: Monday, April 03, 2006 2:48 PM
> To: httpclient-user@jakarta.apache.org
> Subject: Request
>
> Hi all,
> I have a doubt on the way Http Client response bieng read, HttpMethod
> returns Stream or String as response content. I see no method returns byte
> as output. Is there any reason for that
>
> Thanks,
> Sheetal
>
>
>
> ---------------------------------
> Talk is cheap. Use Yahoo! Messenger to make PC-to-Phone calls. Great rates
> starting at 1¢/min.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-user-help@jakarta.apache.org
>
>
>
>
> ---------------------------------
> New Yahoo! Messenger with Voice. Call regular phones from your PC and save
> big.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-user-help@jakarta.apache.org
>
>
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam? Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
>



--
Chaitanya Ravi,
e-Trust, CA,ITC
E-Mail: chaitanya.ravi@ca.com




		
---------------------------------
How low will we go? Check out Yahoo! Messenger’s low  PC-to-Phone call rates.

Re: Request

Posted by Chaitanya Ravi <r....@gmail.com>.
Hi Sheetal,

You can use the below code to read ur response.


           httpclientR.executeMethod(postMethod);
            InputStream is = postMethod.getResponseBodyAsStream();
            BufferedInputStream bis = new BufferedInputStream( is );

            String datastr = null;
            StringBuffer sb = new StringBuffer();
            byte[] bytes = new byte[ 8192 ]; // reading as chunk of 8192
bytes
            int count = bis.read( bytes );
            while( count != -1 && count <= 8192 )
            {
             datastr = getString(bytes, count);
             sb.append(datastr);
             count = bis.read( bytes );
            }

           bis.close();
           reply = sb.toString();

Regards,
On 4/3/06, Sheetal D <sh...@yahoo.com> wrote:
>
> I got a warning saying
> "Going to buffer of large or unknown, getResponseAsStream is recommonded".
>
>
> Thanks,
> Sheetal
>
> Ramit bhardwaj <ra...@cybage.com> wrote:
>
> Where does it say to use Stream after a certain limit?? I am pasting the
> API
> here:
>
>
> getResponseBody
> byte[] getResponseBody()
> throws IOException
> Returns the response body of the HTTP method, if any, as an array of
> bytes.
> If the method has not yet been executed or the response has no body, null
> is
> returned. Note that this method does not propagate I/O exceptions. If an
> error occurs while reading the body, null will be returned.
>
> Returns:
> The response body, or null if the body is not available.
> Throws:
> IOException - if an I/O (transport) problem occurs
>
>
>
> I am using it for a highly avaliable mission critical enterprise
> application
> and it seems to work fine. Hence I am curious to know...
>
>
>
> -----Original Message-----
> From: Sheetal D [mailto:sheetal_d_2006@yahoo.com]
> Sent: Monday, April 03, 2006 3:31 PM
> To: HttpClient User Discussion
> Subject: RE: Request
>
> Thanks, But it tells to use Stream after a certain limit.
>
> Thanks,
> Sheetal
>
>
> Ramit bhardwaj wrote:
>
>
> What about getResponseBody() method. It returns a byte array.
>
>
> -----Original Message-----
> From: Sheetal D [mailto:sheetal_d_2006@yahoo.com]
> Sent: Monday, April 03, 2006 2:48 PM
> To: httpclient-user@jakarta.apache.org
> Subject: Request
>
> Hi all,
> I have a doubt on the way Http Client response bieng read, HttpMethod
> returns Stream or String as response content. I see no method returns byte
> as output. Is there any reason for that
>
> Thanks,
> Sheetal
>
>
>
> ---------------------------------
> Talk is cheap. Use Yahoo! Messenger to make PC-to-Phone calls. Great rates
> starting at 1¢/min.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-user-help@jakarta.apache.org
>
>
>
>
> ---------------------------------
> New Yahoo! Messenger with Voice. Call regular phones from your PC and save
> big.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: httpclient-user-help@jakarta.apache.org
>
>
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
>



--
Chaitanya Ravi,
e-Trust, CA,ITC
E-Mail: chaitanya.ravi@ca.com

RE: Request

Posted by Sheetal D <sh...@yahoo.com>.
I got a warning saying 
  "Going to buffer of large or unknown, getResponseAsStream is recommonded".
   
   
  Thanks,
  Sheetal

Ramit bhardwaj <ra...@cybage.com> wrote:
  
Where does it say to use Stream after a certain limit?? I am pasting the API
here:


getResponseBody
byte[] getResponseBody()
throws IOException
Returns the response body of the HTTP method, if any, as an array of bytes.
If the method has not yet been executed or the response has no body, null is
returned. Note that this method does not propagate I/O exceptions. If an
error occurs while reading the body, null will be returned. 

Returns:
The response body, or null if the body is not available. 
Throws: 
IOException - if an I/O (transport) problem occurs



I am using it for a highly avaliable mission critical enterprise application
and it seems to work fine. Hence I am curious to know...



-----Original Message-----
From: Sheetal D [mailto:sheetal_d_2006@yahoo.com] 
Sent: Monday, April 03, 2006 3:31 PM
To: HttpClient User Discussion
Subject: RE: Request

Thanks, But it tells to use Stream after a certain limit.

Thanks,
Sheetal


Ramit bhardwaj wrote:


What about getResponseBody() method. It returns a byte array.


-----Original Message-----
From: Sheetal D [mailto:sheetal_d_2006@yahoo.com]
Sent: Monday, April 03, 2006 2:48 PM
To: httpclient-user@jakarta.apache.org
Subject: Request

Hi all,
I have a doubt on the way Http Client response bieng read, HttpMethod
returns Stream or String as response content. I see no method returns byte
as output. Is there any reason for that 

Thanks,
Sheetal



---------------------------------
Talk is cheap. Use Yahoo! Messenger to make PC-to-Phone calls. Great rates
starting at 1¢/min.


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




---------------------------------
New Yahoo! Messenger with Voice. Call regular phones from your PC and save
big.


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



__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

RE: Request

Posted by Ramit bhardwaj <ra...@cybage.com>.
 
Where does it say to use Stream after a certain limit?? I am pasting the API
here:


getResponseBody
byte[] getResponseBody()
                       throws IOException
Returns the response body of the HTTP method, if any, as an array of bytes.
If the method has not yet been executed or the response has no body, null is
returned. Note that this method does not propagate I/O exceptions. If an
error occurs while reading the body, null will be returned. 

Returns:
The response body, or null if the body is not available. 
Throws: 
IOException - if an I/O (transport) problem occurs



I am using it for a highly avaliable mission critical enterprise application
and it seems to work fine. Hence  I am curious to know...



-----Original Message-----
From: Sheetal D [mailto:sheetal_d_2006@yahoo.com] 
Sent: Monday, April 03, 2006 3:31 PM
To: HttpClient User Discussion
Subject: RE: Request

Thanks, But it tells to use Stream after a certain limit.
   
  Thanks,
  Sheetal
  

Ramit bhardwaj <ra...@cybage.com> wrote:
  

What about getResponseBody() method. It returns a byte array.


-----Original Message-----
From: Sheetal D [mailto:sheetal_d_2006@yahoo.com]
Sent: Monday, April 03, 2006 2:48 PM
To: httpclient-user@jakarta.apache.org
Subject: Request

Hi all,
I have a doubt on the way Http Client response bieng read, HttpMethod
returns Stream or String as response content. I see no method returns byte
as output. Is there any reason for that 

Thanks,
Sheetal



---------------------------------
Talk is cheap. Use Yahoo! Messenger to make PC-to-Phone calls. Great rates
starting at 1¢/min.


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



		
---------------------------------
New Yahoo! Messenger with Voice. Call regular phones from your PC and save
big.


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


RE: Request

Posted by Sheetal D <sh...@yahoo.com>.
Thanks, But it tells to use Stream after a certain limit.
   
  Thanks,
  Sheetal
  

Ramit bhardwaj <ra...@cybage.com> wrote:
  

What about getResponseBody() method. It returns a byte array.


-----Original Message-----
From: Sheetal D [mailto:sheetal_d_2006@yahoo.com] 
Sent: Monday, April 03, 2006 2:48 PM
To: httpclient-user@jakarta.apache.org
Subject: Request

Hi all,
I have a doubt on the way Http Client response bieng read,
HttpMethod returns Stream or String as response content. I see no method
returns byte as output. Is there any reason for that 

Thanks,
Sheetal



---------------------------------
Talk is cheap. Use Yahoo! Messenger to make PC-to-Phone calls. Great rates
starting at 1¢/min.


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



		
---------------------------------
New Yahoo! Messenger with Voice. Call regular phones from your PC and save big.

RE: Request

Posted by Ramit bhardwaj <ra...@cybage.com>.
 

What about getResponseBody() method. It returns a byte array.


-----Original Message-----
From: Sheetal D [mailto:sheetal_d_2006@yahoo.com] 
Sent: Monday, April 03, 2006 2:48 PM
To: httpclient-user@jakarta.apache.org
Subject: Request

Hi all,
         I have a doubt on the way Http Client response bieng read,
HttpMethod returns Stream or String as response content. I see no method
returns byte as output. Is there any reason for that 
   
  Thanks,
  Sheetal
   

		
---------------------------------
Talk is cheap. Use Yahoo! Messenger to make PC-to-Phone calls.  Great rates
starting at 1&cent;/min.


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