Gets a string with the proper encoding (including using the charset specified in the MIME type
of the request) from a URLConnection.
544145
private static String readFromConnection(URLConnection connection) throws IOException {
    InputStream stream;
    if (connection instanceof HttpURLConnection) {
      HttpURLConnection httpConnection = (HttpURLConnection) connection;
      try {
        stream = connection.getInputStream();
      } catch (Exception e) {
        stream = httpConnection.getErrorStream();
      }
    } else {
      stream = connection.getInputStream();
    }
    try {
      ByteArrayOutputStream output = new ByteArrayOutputStream();
      byte[] buffer = new byte[1024];
      int read = 0;
      while ((read = stream.read(buffer)) != -1) {
        output.write(buffer, 0, read);
      }
      String charset = connection.getContentEncoding();
      if (charset == null) {
        String mimeType = connection.getContentType();
        String[] parts = mimeType.split(";");
        for (String part : parts) {
          part = part.trim();
          if (part.startsWith("charset=")) {
            charset = part.substring("charset=".length());
            break;
          }
        }
        if (charset == null) {
          charset = "UTF-8";
        }
      }
      return new String(output.toByteArray(), charset);
    } finally {
      stream.close();
    }
  }
1045609
public static String readString(URL url, String charset) throws IORuntimeException {
		if (url == null) {
			throw new NullPointerException("Empty url provided!");
		}

		InputStream in = null;
		try {
			in = url.openStream();
			return IoUtil.read(in, charset);
		} catch (IOException e) {
			throw new IORuntimeException(e);
		} finally {
			IoUtil.close(in);
		}
	}
614826
public static String urlFormEncode(final String s)
        {
        if (StringUtils.isBlank(s))
            {
            LOG.warn("Could not encode blank string");
            throw new IllegalArgumentException("Blank string");
            }
        try
            {
            return URLEncoder.encode(s, "UTF-8");
            }
        catch (final UnsupportedEncodingException e)
            {
            LOG.error ("Could not encode: " + s, e);            
            return s;
            }
        }
1186839
public static String getResponseAsString(String uri, String charsetName) {
        return HttpRequester.getResponseAsString(uri, charsetName);
    }
1155034
public static String getURLString(String methodName,
			Map<String, Object> params) throws MusixMatchException {
		String paramString = new String();

		paramString += methodName + "?";

		for (Map.Entry<String, Object> entry : params.entrySet()) {
			try {
				paramString += entry.getKey()
						+ "="
						+ URLEncoder.encode(entry.getValue().toString(),
								"UTF-8");
			} catch (UnsupportedEncodingException e) {
				throw new MusixMatchException("Problem encoding "
						+ entry.getValue(), e);
			}

			paramString += "&";
		}

		paramString = paramString.substring(0, paramString.length() - 1);

		return paramString;
	}
179015
public static String urlEncode(String input) {
        try {
            return URLEncoder.encode(input, "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            throw new AssertionError("UTF-8 not supported", ex);
        }
    }
223396
private static String decodeFormFields(final String content, final Charset charset) {
        if (content == null) {
            return null;
        }

        return urlDecode(content, (charset != null) ? charset : Charsets.UTF_8, true);
    }
177369
public String data(boolean useUrlencode, String charset) {
        StringBuilder sb = new StringBuilder();
        if (useUrlencode) {
            datas.forEach((k, v) -> sb.append("&").append(k).append("=")
                .append(urlencode(String.valueOf(v), charset)));
        } else {
            datas.forEach((k, v) -> sb.append("&").append(k).append("=").append(String.valueOf(v)));
        }
        return sb.toString().substring(1);
    }
1075706
private static byte[] getBytes(String string, Charset charset) {
        if (string == null) {
            return null;
        }
        return string.getBytes(charset);
    }
872025
public static String encodeUrlSafe(byte[] source, String charset) {
		return StrUtil.str(encodeUrlSafe(source, false), charset);
	}
1343643
private String urlencode(String data, String charset) {
        try {
            return URLEncoder.encode(String.valueOf(data), charset);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
1114814
public static String getText(URL url) throws IOException {
        return getText(url, CharsetToolkit.getDefaultSystemCharset().name());
    }
357364
public static HttpServletRequest getEncodingRequest(
            HttpServletRequest request)
            throws UnsupportedEncodingException {
        String method = request.getMethod();
        if (method.equalsIgnoreCase("post")) {
            return new EncodingPostRequest(request);
        } else if (method.equalsIgnoreCase("get")) {
            return new EncodingGetRequest(request);
        }
        return request;
    }
678407
public static String urlNonFormEncode(final String s)
        {
        if (StringUtils.isBlank(s))
            {
            LOG.warn("Could not encode blank string");
            throw new IllegalArgumentException("Blank string");
            }
        try
            {
            // We use the HttpClient encoder because the java URLEncoder
            // class uses form encoding.
            return URIUtil.encodeQuery(s, "UTF-8");
            }
        catch (final URIException e)
            {
            LOG.error ("Could not encode: " + s, e);
            
            // This really should never happen.
            assert false;
            return s;
            }
        }
1279481
def get_charset(response):
    
    # Set default charset
    charset = DEFAULT_CHARSET

    m = re.findall(r';charset=(.*)', response.get('mimeType', ''))
    if m:
        charset = m[0]

    return charset
692353
function( input, charset ) {
    if( charset && !Buffer.isBuffer( input ) ) {
      return MIME.Iconv.encode( input, charset )
    } else {
      return Buffer.isBuffer( input )
        ? input.toString( 'base64' )
        : Buffer.from( input ).toString( 'base64' )
    }
  }
33170
public static String encodeUrlSafe(String source, String charset) {
		return Base64Encoder.encodeUrlSafe(source, charset);
	}