标签:restful & application/x-www-form-urlencoded
application/x-www-form-urlencoded指定了发送的POST数据,要进行URL编码,但是前面的&,=用在POST报文前面,作为参数的时候,是不需要进行编码的,可以直接跳过。例如:
loginusername=admin&loginpassword=admin¶m={JSON报文}
对于前面的两个&&都不能进行编码,否则Java后台无法正常解析出POST数据。目前JSON报文里面存在一个uri:
里面存在&,如果没有进行URL编码的话,Java后台无法正常解析出报文
因此对以前的url编码函数进行了简单的处理
std::string UrlEncode(const std::string& str)
{
std::string strTemp = "";
size_t length = str.length();
for (size_t i = 0; i < length; i++)
{
/*
前面的&用来对多个参数键值进行区分,不能进行编码,后面的&必须进行编码
*/
if (i < 50 && str[i] == ‘&‘)
{
strTemp += str[i];
continue;
}
if (isalnum((unsigned char)str[i]) ||
(str[i] == ‘-‘) ||
(str[i] == ‘_‘) ||
(str[i] == ‘.‘) ||
(str[i] == ‘~‘) ||
(str[i] == ‘=‘))
strTemp += str[i];
else if (str[i] == ‘ ‘)
strTemp += "+";
else
{
strTemp += ‘%‘;
strTemp += ToHex((unsigned char)str[i] >> 4);
strTemp += ToHex((unsigned char)str[i] % 16);
}
}
return strTemp;
}
50是一个大致的数字,目前暂时没有考虑其他的通用做法
restful接口中指定application/x-www-form-urlencoded的问题
标签:restful & application/x-www-form-urlencoded
原文地址:http://fengyuzaitu.blog.51cto.com/5218690/1965564