How can I get an HTTP response body as a string?

JavaApache Httpclient-4.xApache Commons

Java Problem Overview


I know there used to be a way to get it with Apache Commons as documented here:

http://hc.apache.org/httpclient-legacy/apidocs/org/apache/commons/httpclient/HttpMethod.html

...and an example here:

http://www.kodejava.org/examples/416.html

...but I believe this is deprecated.

Is there any other way to make an http get request in Java and get the response body as a string and not a stream?

Java Solutions


Solution 1 - Java

Here are two examples from my working project.

  1. Using EntityUtils and HttpEntity

     HttpResponse response = httpClient.execute(new HttpGet(URL));
     HttpEntity entity = response.getEntity();
     String responseString = EntityUtils.toString(entity, "UTF-8");
     System.out.println(responseString);
    
  2. Using BasicResponseHandler

     HttpResponse response = httpClient.execute(new HttpGet(URL));
     String responseString = new BasicResponseHandler().handleResponse(response);
     System.out.println(responseString);
    

Solution 2 - Java

Every library I can think of returns a stream. You could use IOUtils.toString() from Apache Commons IO to read an InputStream into a String in one method call. E.g.:

URL url = new URL("http://www.example.com/");
URLConnection con = url.openConnection();
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String body = IOUtils.toString(in, encoding);
System.out.println(body);

Update: I changed the example above to use the content encoding from the response if available. Otherwise it'll default to UTF-8 as a best guess, instead of using the local system default.

Solution 3 - Java

Here's an example from another simple project I was working on using the httpclient library from Apache:

String response = new String();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("j", request));
HttpEntity requestEntity = new UrlEncodedFormEntity(nameValuePairs);

HttpPost httpPost = new HttpPost(mURI);
httpPost.setEntity(requestEntity);
HttpResponse httpResponse = mHttpClient.execute(httpPost);
HttpEntity responseEntity = httpResponse.getEntity();
if(responseEntity!=null) {
	response = EntityUtils.toString(responseEntity);
}

just use EntityUtils to grab the response body as a String. very simple.

Solution 4 - Java

This is relatively simple in the specific case, but quite tricky in the general case.

HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://stackoverflow.com/");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println(EntityUtils.getContentMimeType(entity));
System.out.println(EntityUtils.getContentCharSet(entity));

The answer depends on the Content-Type HTTP response header.

This header contains information about the payload and might define the encoding of textual data. Even if you assume text types, you may need to inspect the content itself in order to determine the correct character encoding. E.g. see the HTML 4 spec for details on how to do that for that particular format.

Once the encoding is known, an InputStreamReader can be used to decode the data.

This answer depends on the server doing the right thing - if you want to handle cases where the response headers don't match the document, or the document declarations don't match the encoding used, that's another kettle of fish.

Solution 5 - Java

Below is a simple way of accessing the response as a String using Apache HTTP Client library.

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;

//... 

HttpGet get;
HttpClient httpClient;

// initialize variables above

ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpClient.execute(get, responseHandler);

Solution 6 - Java

The Answer by McDowell is correct one. However if you try other suggestion in few of the posts above.

HttpEntity responseEntity = httpResponse.getEntity();
if(responseEntity!=null) {
   response = EntityUtils.toString(responseEntity);
   S.O.P (response);
}

Then it will give you illegalStateException stating that content is already consumed.

Solution 7 - Java

How about just this?

org.apache.commons.io.IOUtils.toString(new URL("http://www.someurl.com/"));

Solution 8 - Java

We can use the below code also to get the HTML Response in java

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.HttpResponse;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.log4j.Logger;

public static void main(String[] args) throws Exception {
    HttpClient client = new DefaultHttpClient();
    //  args[0] :-  http://hostname:8080/abc/xyz/CheckResponse
    HttpGet request1 = new HttpGet(args[0]);
    HttpResponse response1 = client.execute(request1);
    int code = response1.getStatusLine().getStatusCode();

    try (BufferedReader br = new BufferedReader(new InputStreamReader((response1.getEntity().getContent())));) {
        // Read in all of the post results into a String.
        String output = "";
        Boolean keepGoing = true;
        while (keepGoing) {
            String currentLine = br.readLine();

            if (currentLine == null) {
                keepGoing = false;
            } else {
                output += currentLine;
            }
        }

        System.out.println("Response-->" + output);
    } catch (Exception e) {
        System.out.println("Exception" + e);

    }
}

Solution 9 - Java

Here's a lightweight way to do so:

String responseString = "";
for (int i = 0; i < response.getEntity().getContentLength(); i++) { 
	responseString +=
	Character.toString((char)response.getEntity().getContent().read()); 
}

With of course responseString containing website's response and response being type of HttpResponse, returned by HttpClient.execute(request)

Solution 10 - Java

Following is the code snippet which shows better way to handle the response body as a String whether it's a valid response or error response for the HTTP POST request:

BufferedReader reader = null;
OutputStream os = null;
String payload = "";
try {
	URL url1 = new URL("YOUR_URL");
	HttpURLConnection postConnection = (HttpURLConnection) url1.openConnection();
	postConnection.setRequestMethod("POST");
	postConnection.setRequestProperty("Content-Type", "application/json");
	postConnection.setDoOutput(true);
	os = postConnection.getOutputStream();
	os.write(eventContext.getMessage().getPayloadAsString().getBytes());
	os.flush();
	
	String line;
	try{
		reader = new BufferedReader(new InputStreamReader(postConnection.getInputStream()));
	}
	catch(IOException e){
		if(reader == null)
			reader = new BufferedReader(new InputStreamReader(postConnection.getErrorStream()));
	}
	while ((line = reader.readLine()) != null)
		payload += line.toString();
}		
catch (Exception ex) {
			log.error("Post request Failed with message: " + ex.getMessage(), ex);
} finally {
	try {
		reader.close();
		os.close();
	} catch (IOException e) {
		log.error(e.getMessage(), e);
		return null;
	}
}

Solution 11 - Java

You can use a 3-d party library that sends Http request and handles the response. One of the well-known products would be Apache commons HTTPClient: HttpClient javadoc, HttpClient Maven artifact. There is by far less known but much simpler HTTPClient (part of an open source MgntUtils library written by me): MgntUtils HttpClient javadoc, MgntUtils maven artifact, MgntUtils Github. Using either of those libraries you can send your REST request and receive response independently from Spring as part of your business logic

Solution 12 - Java

If you are using Jackson to deserialize the response body, one very simple solution is to use request.getResponseBodyAsStream() instead of request.getResponseBodyAsString()

Solution 13 - Java

Here is a vanilla Java answer:

import java.net.http.HttpClient;
import java.net.http.HttpResponse;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublishers;

...
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
  .uri(targetUrl)
  .header("Content-Type", "application/json")
  .POST(BodyPublishers.ofString(requestBody))
  .build();
HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString());
String responseString = (String) response.body();

Solution 14 - Java

Using Apache commons Fluent API, it can be done as mentioned below,

String response = Request.Post("http://www.example.com/")
                .body(new StringEntity(strbody))
                .addHeader("Accept","application/json")
                .addHeader("Content-Type","application/json")
                .execute().returnContent().asString();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaniel ShaulovView Question on Stackoverflow
Solution 1 - JavaspideringwebView Answer on Stackoverflow
Solution 2 - JavaWhiteFang34View Answer on Stackoverflow
Solution 3 - JavamoonlightcheeseView Answer on Stackoverflow
Solution 4 - JavaMcDowellView Answer on Stackoverflow
Solution 5 - JavalkamalView Answer on Stackoverflow
Solution 6 - JavaAkshayView Answer on Stackoverflow
Solution 7 - JavaEric SchlenzView Answer on Stackoverflow
Solution 8 - JavaSubhasish SahuView Answer on Stackoverflow
Solution 9 - JavaMarko ZajcView Answer on Stackoverflow
Solution 10 - JavaKrutikView Answer on Stackoverflow
Solution 11 - JavaMichael GantmanView Answer on Stackoverflow
Solution 12 - JavaVic SeedoubleyewView Answer on Stackoverflow
Solution 13 - JavaWilliam EntrikenView Answer on Stackoverflow
Solution 14 - JavapratapView Answer on Stackoverflow