http请求参数中含有 & 或者空格,则必须URL编码
http请求参数中含有& 或者空格,则必须URL编码
发送请求时,会先把请求参数转化为形如:
a=a1&b=b1&c=c1
updateParameter会调用RequestPanel中的getRequestBodyFromList,
参考com/common/bean/ParameterIncludeBean.java中的getQueryString方法
WebServletUtil.isShouldURLEncode就是用来智能地判断是否需要URL转码,
目前的规则是:
http请求参数中含有& 或者空格,则必须URL编码
具体代码:
/***
* 是否包含指定字符串,不区分大小写
*
* @param input
* : 原字符串
* @param regex
* @return
*/
public static boolean contain2(String input, String regex) {
/***
* input:(1)bss登录(2) <br>
regex:bss登录(2)
*/
regex = regex.replace("(", "\\(");
regex = regex.replace(")", "\\)");
if (ValueWidget.isNullOrEmpty(input)) {
return false;
}
Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(input);
boolean result = m.find();
return result;
}
/***
* 是否需要URL编码<br>
* http请求参数中含有& 或者空格,则必须URL编码
*
* @param value
* @return
*/
public static boolean isShouldURLEncode(String value) {
return !ValueWidget.isNullOrEmpty(value) && (value.contains("&") || RegexUtil.contain2(value, "[\\s]"));
}
优化:
GET请求时,参数含有中文,也需要URL编码:
public static boolean isHasChinses(String str) {
String encodeName = "UTF-8";
for (int i = 0; i < str.length(); i++) {
try {
String singleStr = str.substring(i, i + 1);
int leng = getEncodeLength(singleStr, encodeName);
// System.out.println(singleStr + "\t" + leng);
if (leng == 9)// 表示是中文字符
{
// System.out.println("有中文");
return true;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
/***
* 是否需要URL编码<br>
* http请求参数中含有& 或者空格,则必须URL编码
*
* @param value
* @return
*/
public static boolean isShouldURLEncode(String value) {
return !ValueWidget.isNullOrEmpty(value) && (value.contains("&") || RegexUtil.contain2(value, "[\\s]")
|| SystemHWUtil.isHasChinses(value));
}