Java HTTP Response getResponseAsString(HttpURLConnection conn)

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

Description

get Response As String

License

Apache License

Declaration

protected static String getResponseAsString(HttpURLConnection conn) throws IOException 

Method Source Code


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

import java.io.*;
import java.net.HttpURLConnection;

public class Main {
    public static final String DEFAULT_CHARSET = "UTF-8";

    protected static String getResponseAsString(HttpURLConnection conn) throws IOException {
        String charset = getResponseCharset(conn.getContentType());
        InputStream es = conn.getErrorStream();
        if (es == null) {
            return getStreamAsString(conn.getInputStream(), charset);
        } else {/*w w w  .  ja  v a 2s  . com*/
            String msg = getStreamAsString(es, charset);
            if (isEmpty(msg)) {
                throw new IOException(conn.getResponseCode() + ":" + conn.getResponseMessage());
            } else {
                throw new IOException(msg);
            }
        }
    }

    private static String getResponseCharset(String ctype) {
        String charset = DEFAULT_CHARSET;

        if (!isEmpty(ctype)) {
            String[] params = ctype.split(";");
            for (String param : params) {
                param = param.trim();
                if (param.startsWith("charset")) {
                    String[] pair = param.split("=", 2);
                    if (pair.length == 2) {
                        if (!isEmpty(pair[1])) {
                            charset = pair[1].trim();
                        }
                    }
                    break;
                }
            }
        }

        return charset;
    }

    private static String getStreamAsString(InputStream stream, String charset) throws IOException {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(stream, charset));
            StringWriter writer = new StringWriter();

            char[] chars = new char[256];
            int count = 0;
            while ((count = reader.read(chars)) > 0) {
                writer.write(chars, 0, count);
            }

            return writer.toString();
        } finally {
            if (stream != null) {
                stream.close();
            }
        }
    }

    private static boolean isEmpty(final String str) {
        return str == null || str.length() == 0;
    }
}

Related

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