Java Utililty Methods HTTP Response

List of utility methods to do HTTP Response

Description

The list of methods to do HTTP Response are organized into topic(s).

Method

StringgetResponseErrorContent(HttpURLConnection httpConn)
Gets the error response as a string
if (httpConn.getErrorStream() == null) {
    return null;
} else {
    StringBuilder sb = new StringBuilder();
    BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getErrorStream()));
    String str;
    while ((str = in.readLine()) != null) {
        sb.append(str + "\n");
...
StringgetResponseHeader(HttpURLConnection conn)
get Response Header
String headerField = conn.getHeaderField(null);
if (headerField == null) {
    return null;
StringBuffer sb = new StringBuffer();
sb.append(headerField).append("\r\n");
String key;
for (int i = 1; (key = conn.getHeaderFieldKey(i)) != null; i++) {
...
StringgetResponseHeader(String headerName, HttpURLConnection urlConnection)
get Response Header
return ((HttpURLConnection) urlConnection).getHeaderField(headerName);
StringgetResponseHeaders(URLConnection conn, HashMap headers)
Gets the headers
headers.clear();
String responseCode = "";
for (int i = 0;; i++) {
    String name = conn.getHeaderFieldKey(i);
    String value = conn.getHeaderField(i);
    if (name == null && value == null) {
        break;
    if (name == null) {
        responseCode = value;
    } else {
        headers.put(name, value);
return responseCode;
StringgetResponseMessage(InputStream inputStream, HttpURLConnection connection)
get Response Message
String responseMessage = null;
StringBuffer sb = new StringBuffer();
InputStream dis = connection.getInputStream();
int chr;
while ((chr = dis.read()) != -1) {
    sb.append((char) chr);
if (sb != null) {
...
InputStreamgetResponseStream(final HttpURLConnection connection)
get Response Stream
try {
    return connection.getInputStream();
} catch (IOException ioe) {
    InputStream errorStream = connection.getErrorStream();
    if (errorStream != null) {
        return errorStream;
    throw ioe;
...
InputStreamgetResponseStream(HttpURLConnection conn)
Returns the body of the response.
return httpOk(conn) ? conn.getInputStream() : conn.getErrorStream();
StringgetResponseStringFromConn(HttpURLConnection conn, boolean isSuccess)
get Response String From Conn
BufferedReader reader;
if (isSuccess) {
    reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
} else {
    reader = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
StringBuffer stringBuffer = new StringBuffer();
String line = "";
...
StringgetResponseText(URL constructedUrl, String encoding)
get Response Text
HttpURLConnection conn = null;
try {
    conn = (HttpURLConnection) constructedUrl.openConnection();
    BufferedReader in = null;
    if (null == encoding) {
        in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    } else {
        in = new BufferedReader(new InputStreamReader(conn.getInputStream(), encoding));
...
StringgetResposeText(HttpURLConnection connection)
get Respose Text
InputStream is = connection.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) > 0) {
    baos.write(buffer, 0, len);
String responseText = baos.toString("UTF8");
...