What's the recommended way to get the HTTP response as a String when using Apache's HTTP Client? [duplicate]
Asked Answered
E

4

18

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;

}
Eleneeleni answered 23/8, 2012 at 19:57 Comment(0)
M
51

You can use EntityUtils#toString() for this.

// ...
HttpResponse response = client.execute(get);
String responseAsString = EntityUtils.toString(response.getEntity());
// ...
Mobility answered 23/8, 2012 at 20:4 Comment(3)
this answer is the way to go for HttpClient. Apache Jersey is, in my opinion, a lot easier to work with than HttpClient.Macdonell
@Paul: I'm not sure what you mean with "Apache Jersey". There's no such thing in Apache project. "Jersey" is the Sun/Oracle reference implementation of the JAX-RS API which is not a HTTP client at all.Mobility
sorry, I meant sun/oracle's jersey. it contains an http client with a more fluent API.Macdonell
A
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())

Aleta answered 23/8, 2012 at 20:8 Comment(0)
U
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.

Unguent answered 23/8, 2012 at 20:8 Comment(0)
M
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.

Mingrelian answered 25/7, 2017 at 20:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.