码迷,mamicode.com
首页 > 移动开发 > 详细

restful接口中指定application/x-www-form-urlencoded的问题

时间:2017-09-15 18:49:08      阅读:669      评论:0      收藏:0      [点我收藏+]

标签:restful & application/x-www-form-urlencoded

    application/x-www-form-urlencoded指定了发送的POST数据,要进行URL编码,但是前面的&,=用在POST报文前面,作为参数的时候,是不需要进行编码的,可以直接跳过。例如:

loginusername=admin&loginpassword=admin&param={JSON报文}

对于前面的两个&&都不能进行编码,否则Java后台无法正常解析出POST数据。目前JSON报文里面存在一个uri:

http://192.168.0.225:8080/kms/services/rest/dataInfoService/downloadFileid=00000001/temp001/097_5848300_10488&token=7a57a5a7ffffffffc1a0316369671314

里面存在&,如果没有进行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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!