public static String downloadStringResource(HttpURLConnection connection) {

        StringBuffer content;
        InputStream inputstream = getSafeInputStream(connection);
        if (inputstream == null) {
            return null;
        }

        // load the Stream in a StringBuffer
        // Check encoding
        String contentType = connection.getContentType();
        String charset = "";
        for (String value : contentType.split(";")) {
            value = value.trim();
	    if (value.toLowerCase().startsWith("charset=")) 
	        charset = value.substring("charset=".length());
        }
        if ("".equals(charset))
	    charset = "UTF-8";

	String result = "";
	try {
            InputStreamReader isr = new InputStreamReader(inputstream, charset);
            content = new StringBuffer();

	    // Write bytes into buffer
            char buf[] = new char[STREAM_BUFFER_SIZE];
            int cnt = 0;
            while ((cnt = isr.read(buf, 0, STREAM_BUFFER_SIZE)) != -1) {
                content.append(buf, 0, cnt);
            }
            isr.close();
            inputstream.close();

            result = content.toString();
        } catch (Exception ioexception) {

            System.out.println(ioexception);
        }

        return result;
    }