对于URL传参的请求来讲,特殊字符要进行URLencoding处理
private static String getUrl(String url) throws Exception {
if (!url.contains("?") || !url.contains("=")) {
return url;
}
String prefix = url.split("\\?")[0];
StringBuilder stringBuilder = new StringBuilder(prefix);
stringBuilder.append("?");
String value = url.split("\\?")[1];
String[] arr = value.split("&");
for (int i = 0; i < arr.length; i++) {
if (!StringUtils.hasLength(arr[i])) {
continue;
}
String[] ss = arr[i].split("=");
if (ss.length < 2) {
continue;
}
String key = ss[0];
String v = ss[1];
//先解码在编码
v = URLEncoder.encode(URLDecoder.decode(v, "UTF-8"), "UTF-8");
stringBuilder.append(key).append("=").append(v);
stringBuilder = i == arr.length - 1 ? stringBuilder : stringBuilder.append("&");
}
return stringBuilder.toString();
}