http请求参数中含有&,则必须URL编码

http请求参数中含有&,则必须URL编码

/***
     * 如果参数中含有特殊字符&,则强制URL编码<br>
     * 为什么?因为http参数就是通过& 分隔的
     *
     * @param parameterIncludeBean
     */
    public static void urlencodeParameter(ParameterIncludeBean parameterIncludeBean) {
        String val = parameterIncludeBean.getValue();
        if (!ValueWidget.isNullOrEmpty(val) && val.contains("&")) {
            try {
                val = URLEncoder.encode(val, SystemHWUtil.CHARSET_UTF);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            parameterIncludeBean.setValue(val);
        }
    }

protected TreeMap getParameterMap(boolean isUrlEncoding,String requestCharset) {
        List<ParameterIncludeBean> parameters = getTableParameters();
        //如果请求参数中含有&特殊字符,则需要进行URL编码
        if (!ValueWidget.isNullOrEmpty(parameters)) {
            int size = parameters.size();
            for (int i = 0; i < size; i++) {
                ParameterIncludeBean parameterIncludeBean = parameters.get(i);
                urlencodeParameter(parameterIncludeBean);
            }
        }

       return TableUtil.getParameterMap(parameters, isUrlEncoding, requestCharset);
    }

com.common.bean.ParameterIncludeBean

public String getQueryString(boolean isUrlEncoding,String requestCharset) {
    	if(isUrlEncoding 
    			&&!ValueWidget.isNullOrEmpty(this.value) 
    			&&!ValueWidget.isNullOrEmpty(requestCharset)){
    		try {
				this.value=URLEncoder.encode(this.value, requestCharset);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
    	}else{//如果参数中含有特殊字符&,则强制URL编码,为什么?因为http参数就是通过& 分隔的
    		if(!ValueWidget.isNullOrEmpty(this.value)&&this.value.contains("&")){
                try {
                	this.value= URLEncoder.encode(this.value, SystemHWUtil.CHARSET_UTF);//必须使用UTF-8编码
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
    	}
        return this.key + "=" + this.value;
    }

猜你喜欢

转载自hw1287789687.iteye.com/blog/2305836