Here you can find the source of getResponseAsString(HttpURLConnection conn)
protected static String getResponseAsString(HttpURLConnection conn) throws IOException
//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; } }