Java HTTP Response getResponseBody(HttpURLConnection conn)

Here you can find the source of getResponseBody(HttpURLConnection conn)

Description

get Response Body

License

Apache License

Declaration

public static String getResponseBody(HttpURLConnection conn) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.net.HttpURLConnection;

public class Main {
    public static String getResponseBody(HttpURLConnection conn) {
        BufferedReader br = null;
        StringBuilder body = null;
        String line = "";
        try {//  w  w w .j  a  v a 2s . c  o m
            br = new BufferedReader(new InputStreamReader(
                    conn.getInputStream()));
            body = new StringBuilder();
            while ((line = br.readLine()) != null)
                body.append(line);
            return body.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String getResponseBody(InputStream is) {
        BufferedReader br = null;
        StringBuilder body = null;
        String line = "";
        try {
            // we use xPath to get the baseUrl and accountId from the XML
            // response body
            br = new BufferedReader(new InputStreamReader(is));
            body = new StringBuilder();
            while ((line = br.readLine()) != null)
                body.append(line);
            return body.toString();
        } catch (Exception e) {
            throw new RuntimeException(e); // simple exception handling, please
            // review it
        }
    }
}

Related

  1. getResourceModifiedTime(URL url)
  2. getResponse(HttpURLConnection httpConn, StringBuilder responseContent, StringBuilder responseCode, HashMap headers)
  3. getResponse(URL request)
  4. getResponse(URL url)
  5. getResponseAsString(HttpURLConnection conn)
  6. getResponseContent(HttpURLConnection connection)
  7. getResponseContent(HttpURLConnection httpConn)
  8. getResponseContent(String url)
  9. getResponseErrorContent(HttpURLConnection httpConn)