真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

網(wǎng)絡(luò)通信第二課HTTP報(bào)文請(qǐng)求數(shù)據(jù)編碼application/x-www-form-urlencoded

1編碼說(shuō)明

網(wǎng)頁(yè)中的表單使用POST方法提交時(shí),數(shù)據(jù)內(nèi)容的類型是 application/x-www-form-urlencoded,這種類型會(huì):

定制網(wǎng)站制作可以根據(jù)自己的需求進(jìn)行定制,網(wǎng)站設(shè)計(jì)、網(wǎng)站制作構(gòu)思過(guò)程中功能建設(shè)理應(yīng)排到主要部位公司網(wǎng)站設(shè)計(jì)、網(wǎng)站制作的運(yùn)用實(shí)際效果公司網(wǎng)站制作網(wǎng)站建立與制做的實(shí)際意義

 

1.字符"a"-"z","A"-"Z","0"-"9",".","-","*",和"_"都不會(huì)被編碼;

 

2.將空格轉(zhuǎn)換為加號(hào) (+) ;

 

3.將非文本內(nèi)容轉(zhuǎn)換成"%xy"的形式,xy是兩位16進(jìn)制的數(shù)值;

 

4.在每個(gè) name=value 對(duì)之間放置 & 符號(hào)。

 

*/

 

 諸如字符: / & ? @ # ; $ + = 和 %也可以被使用,但是它們各有其特殊的用途,如果一個(gè)文件名包括了這些字符( / & ? @ # ; $ + = %),

這些字符和所有其他字符就應(yīng)該被編碼。

 

編碼過(guò)程非常簡(jiǎn)單,任何字符只要不是ASCII碼數(shù)字,字母,或者前面提到的標(biāo)點(diǎn)符,它們都將被轉(zhuǎn)換成字節(jié)形式,每個(gè)字節(jié)都寫(xiě)成這種形式:

一個(gè)“%”后面跟著兩位16進(jìn)制的數(shù)值??崭袷且粋€(gè)特殊情況,因?yàn)樗鼈兲匠A?。它除了被編碼成“%20”以外,還能編碼為一個(gè)“+”。

加號(hào)(+)本身被編碼為%2B。當(dāng)/ # = & 和?作為名字的一部分來(lái)使用時(shí),而不是作為URL部分之間的分隔符來(lái)使用時(shí),它們都應(yīng)該被編碼。

參考

http://www.cnblogs.com/jingzhishen/p/3506339.html

 

2 編碼實(shí)現(xiàn)

C++的libcurl庫(kù)并沒(méi)有提供一個(gè)類似C#中URLEncoder類,或者類似Java的httpclient的具體實(shí)現(xiàn),,因此提供如下的案例代碼

unsigned char ToHex(unsigned char x)

{

         return  x > 9 ? x + 55 : x + 48;

}

 

unsigned char FromHex(unsigned char x)

{

         unsignedchar y;

         if(x >= 'A' && x <= 'Z') y = x - 'A' + 10;

         elseif (x >= 'a' && x <= 'z') y = x - 'a' + 10;

         elseif (x >= '0' && x <= '9') y = x - '0';

         elseassert(0);

         returny;

}

 

std::string UrlEncode(conststd::string& str)

{

         std::stringstrTemp = "";

         size_tlength = str.length();

         for(size_t i = 0; i < length; i++)

         {

                   if(isalnum((unsigned char)str[i]) ||

                            (str[i]== '-') ||

                            (str[i]== '_') ||

                            (str[i]== '.') ||

                            (str[i]== '~'))

                            strTemp+= str[i];

                   elseif (str[i] == ' ')

                            strTemp+= "+";

                   else

                   {

                            strTemp+= '%';

                            strTemp+= ToHex((unsigned char)str[i] >> 4);

                            strTemp+= ToHex((unsigned char)str[i] % 16);

                   }

         }

         returnstrTemp;

}

 

std::string UrlDecode(conststd::string& str)

{

         std::stringstrTemp = "";

         size_tlength = str.length();

         for(size_t i = 0; i < length; i++)

         {

                   if(str[i] == '+') strTemp += ' ';

                   elseif (str[i] == '%')

                   {

                            assert(i+ 2 < length);

                            unsignedchar high = FromHex((unsigned char)str[++i]);

                            unsignedchar low = FromHex((unsigned char)str[++i]);

                            strTemp+= high * 16 + low;

                   }

                   elsestrTemp += str[i];

         }

         returnstrTemp;

}

3 生產(chǎn)環(huán)境編碼

上面的代碼是對(duì)=,&也進(jìn)行了編碼,但是在實(shí)際的生產(chǎn)環(huán)境中,如果請(qǐng)求數(shù)據(jù)中攜帶的是Json格式,并且數(shù)據(jù)中使用了參數(shù)=的方式,這種情況下=&不應(yīng)該被編碼,所以應(yīng)該單獨(dú)拆分進(jìn)行編碼,json內(nèi)容進(jìn)行編碼,參數(shù)鍵值不應(yīng)該被編,代碼修改如下:

std::string UrlEncode(conststd::string& str)

{

         std::stringstrTemp = "";

         size_tlength = str.length();

         for(size_t i = 0; i < length; i++)

         {

                   if(isalnum((unsigned char)str[i]) ||

                            (str[i]== '-') ||

                            (str[i]== '_') ||

                            (str[i]== '.') ||

                            (str[i]== '~') ||

                            (str[i]== '&') ||

                            (str[i]== '='))

                            strTemp+= str[i];

                   elseif (str[i] == ' ')

                            strTemp+= "+";

                   else

                   {

                            strTemp+= '%';

                            strTemp+= ToHex((unsigned char)str[i] >> 4);

                            strTemp+= ToHex((unsigned char)str[i] % 16);

                   }

         }

         returnstrTemp;

}

 

std::string UrlDecode(conststd::string& str)

{

         std::stringstrTemp = "";

         size_tlength = str.length();

         for(size_t i = 0; i < length; i++)

         {

                   if(str[i] == '+') strTemp += ' ';

                   elseif (str[i] == '%')

                   {

                            assert(i+ 2 < length);

                            unsignedchar high = FromHex((unsigned char)str[++i]);

                            unsignedchar low = FromHex((unsigned char)str[++i]);

                            strTemp+= high * 16 + low;

                   }

                   elsestrTemp += str[i];

         }

         returnstrTemp;

}

 

4 restful接口編碼

application/x-www-form-urlencoded指定了發(fā)送的POST數(shù)據(jù),要進(jìn)行URL編碼,但是前面的&,=用在POST報(bào)文前面,作為參數(shù)的時(shí)候,是不需要進(jìn)行編碼的,可以直接跳過(guò)。例如:

loginusername=admin&loginpassword=admin¶m={JSON報(bào)文}

對(duì)于前面的兩個(gè)&&都不能進(jìn)行編碼,否則Java后臺(tái)無(wú)法正常解析出POST數(shù)據(jù)。目前JSON報(bào)文里面存在一個(gè)uri:

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

里面存在&,如果沒(méi)有進(jìn)行URL編碼的話,Java后臺(tái)無(wú)法正常解析出報(bào)文

因此對(duì)以前的url編碼函數(shù)進(jìn)行了簡(jiǎn)單的處理

std::string UrlEncode(const std::string&str)

{

         std::stringstrTemp = "";

         size_tlength = str.length();

         for(size_t i = 0; i < length; i++)

         {

                   /*

                   前面的&用來(lái)對(duì)多個(gè)參數(shù)鍵值進(jìn)行區(qū)分,不能進(jìn)行編碼,后面的&必須進(jìn)行編碼

                   */

                   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];

                   elseif (str[i] == ' ')

                            strTemp+= "+";

                   else

                   {

                            strTemp+= '%';

                            strTemp+= ToHex((unsigned char)str[i] >> 4);

                            strTemp+= ToHex((unsigned char)str[i] % 16);

                   }

         }

         returnstrTemp;

}

 

50是一個(gè)大致的數(shù)字,應(yīng)該分別對(duì)json格式內(nèi)容進(jìn)行編碼,而不是對(duì)整一個(gè)發(fā)送報(bào)文進(jìn)行編碼


網(wǎng)頁(yè)標(biāo)題:網(wǎng)絡(luò)通信第二課HTTP報(bào)文請(qǐng)求數(shù)據(jù)編碼application/x-www-form-urlencoded
鏈接地址:http://weahome.cn/article/jgphcp.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部