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 Joan Balagueró <jo...@grupoventus.com> on 2018/09/15 21:30:27 UTC

Right way to use EntityUtils.toString()

Hello,

 

This is the first time I need to use EntityUtils.toString(). My code is:

 

  try 

  {

   ( . . . )

   response         = cluster.getHttpClient().performRequest(request);

   String body      = EntityUtils.toString(response.getEntity(),
GenericConstants.ENCODING_UTF8);

  

   ( . . . )

  }

  catch (Exception e) 

  {

   ( . . . )

  }

 

Is this the right way to use it? My questions:

 

1.       Do I need to check previously that response.getEntity is null
before calling EntityUtils.toString()?

2.       Do I need to close the connection in any way, or
EntityUtils.toString() is responsible to get the connection back to the pool
(even in case of exception)?

 

Thanks,

Joan.


Re: Right way to use EntityUtils.toString()

Posted by Alexey Panchenko <al...@gmail.com>.
Hi,

The right way would be executing queries like this:

String response = httpClient.execute(new HttpGet(url), new
BasicResponseHandler());

Regards,
Alex

On Sat, Sep 15, 2018 at 11:30 PM Joan Balagueró <
joan.balaguero@grupoventus.com> wrote:

> Hello,
>
>
>
> This is the first time I need to use EntityUtils.toString(). My code is:
>
>
>
>   try
>
>   {
>
>    ( . . . )
>
>    response         = cluster.getHttpClient().performRequest(request);
>
>    String body      = EntityUtils.toString(response.getEntity(),
> GenericConstants.ENCODING_UTF8);
>
>
>
>    ( . . . )
>
>   }
>
>   catch (Exception e)
>
>   {
>
>    ( . . . )
>
>   }
>
>
>
> Is this the right way to use it? My questions:
>
>
>
> 1.       Do I need to check previously that response.getEntity is null
> before calling EntityUtils.toString()?
>
> 2.       Do I need to close the connection in any way, or
> EntityUtils.toString() is responsible to get the connection back to the
> pool
> (even in case of exception)?
>
>
>
> Thanks,
>
> Joan.
>
>