sPara += sName + "=" + encodeURI(encodeURI(sValue)) + "&";
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open( "POST" , sURL , true );
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(sPara);
[color=red][b]
注意,这里我使用了两次encodeURI,encodeURI(encodeURI(sValue)) [/b][/color]
String providerName = request.getParameter("providerName");
//post 传递的时候,一定是用utf8编码的
providerName = URLDecoder.decode(providerName , "utf-8");
分析:当调用request.getParameter()函数时,会自动进行一次URI的解码过程,调用时内置的解码过程会导致乱码出现。而URI 编码两次后,request.getParameter()函数得到的是原信息URI编码一次的内容。再用可控的解码函数 java.net.URLDecoder.decode()就可解出原始的正确的信息。
原文出自:http://blog.youkuaiyun.com/leejah163/article/details/5887714
本文探讨了在HTTP请求中如何正确处理URI编码的问题。详细解释了为何有时需要对参数进行两次URI编码,并且展示了如何使用Java的URLDecoder.decode()方法来确保接收到的数据能够被正确解码。
363

被折叠的 条评论
为什么被折叠?



