溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2020-10-03 21:09:06 來(lái)源:網(wǎng)絡(luò) 閱讀:3453 作者:fengyuzaitu 欄目:系統(tǒng)運(yùn)維

1編碼說(shuō)明

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

 

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é)都寫成這種形式:

一個(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類,或者類似Javahttpclient的具體實(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&param={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)行編碼


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI