com.aliyun.sdk.gateway.sls.internal.HttpUtil Maven / Gradle / Ivy
package com.aliyun.sdk.gateway.sls.internal;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;
public class HttpUtil {
/**
* Encode a URL segment with special chars replaced.
*/
public static String urlEncode(String value, String encoding) {
if (value == null) {
return "";
}
try {
String encoded = URLEncoder.encode(value, encoding);
return encoded.replace("+", "%20").replace("*", "%2A").replace("~", "%7E").replace("/", "%2F");
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException("FailedToEncodeUri", e);
}
}
/**
* Encode request parameters to URL segment.
*/
public static String paramToQueryString(Map params, String charset) {
if (params == null || params.isEmpty()) {
return "";
}
StringBuilder paramString = new StringBuilder();
boolean first = true;
for (Map.Entry p : params.entrySet()) {
String key = p.getKey();
String value = p.getValue();
if (!first) {
paramString.append("&");
}
// Urlencode each request parameter
paramString.append(urlEncode(key, charset));
if (value != null) {
paramString.append("=").append(urlEncode(value, charset));
}
first = false;
}
return paramString.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy