17

I've just begun using Apache's HTTP Client library and noticed that there wasn't a built-in method of getting the HTTP response as a String. I'm just looking to get it as as String so that i can pass it to whatever parsing library I'm using.

What's the recommended way of getting the HTTP response as a String? Here's my code to make the request:

public String doGet(String strUrl, List<NameValuePair> lstParams) {

    String strResponse = null;

    try {

        HttpGet htpGet = new HttpGet(strUrl);
        htpGet.setEntity(new UrlEncodedFormEntity(lstParams));

        DefaultHttpClient dhcClient = new DefaultHttpClient();

        PersistentCookieStore pscStore = new PersistentCookieStore(this);
        dhcClient.setCookieStore(pscStore);

        HttpResponse resResponse = dhcClient.execute(htpGet);
        //strResponse = getResponse(resResponse);

    } catch (ClientProtocolException e) {
        throw e;
    } catch (IOException e) {
        throw e;
    }

    return strResponse;

}
Mridang Agarwalla
  • 43,201
  • 71
  • 221
  • 382

4 Answers4

50

You can use EntityUtils#toString() for this.

// ...
HttpResponse response = client.execute(get);
String responseAsString = EntityUtils.toString(response.getEntity());
// ...
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    this answer is the way to go for HttpClient. Apache Jersey is, in my opinion, a lot easier to work with than HttpClient. – Paul Sanwald Aug 23 '12 at 20:09
  • 1
    @Paul: I'm not sure what you mean with "Apache Jersey". There's no such thing in Apache project. ["Jersey"](http://jersey.java.net) is the Sun/Oracle reference implementation of the JAX-RS API which is not a HTTP client at all. – BalusC Aug 24 '12 at 10:52
  • 1
    sorry, I meant sun/oracle's jersey. it contains an http client with a more fluent API. – Paul Sanwald Aug 24 '12 at 13:02
5

You need to consume the response body and get the response:

BufferedReader br = new BufferedReader(new InputStreamReader(httpresponse.getEntity().getContent()));

And then read it:

String readLine;
String responseBody = "";
while (((readLine = br.readLine()) != null)) {
  responseBody += "\n" + readLine;
}

The responseBody now contains your response as string.

(Don't forget to close the BufferedReader in the end: br.close())

dumbPotato21
  • 5,669
  • 5
  • 21
  • 34
Pedro Nunes
  • 410
  • 2
  • 5
1

You can do something like:

Reader in = new BufferedReader(
        new InputStreamReader(response.getEntity().getContent(), "UTF-8"));

Using the reader you will be able to build your string. But if you are using SAX you can give the stream to the parser directly. This way you will not have to create the string and your memory footprint will be lower too.

dan
  • 13,132
  • 3
  • 38
  • 49
0

In terms of conciseness of code it might be using the Fluent API like this:

import org.apache.http.client.fluent.Request;
[...]
String result = Request.Get(uri).execute().returnContent().asString();

The documentation warns though that this approach is not ideal in terms of memory consumption.

anothernode
  • 5,100
  • 13
  • 43
  • 62