In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
1 coding description
When a form in a web page is submitted using the POST method, the data content type is application/x-www-form-urlencoded, which will:
1. The characters "a"-"z", "A"-"Z", "0"-"9", ".", "-", "*" and "_" will not be encoded.
two。 Convert spaces to plus signs (+)
3. Convert non-text content to "% xy", where xy is a two-digit hexadecimal value
4. Place the & symbol between each name=value pair.
, /
Characters such as / &? @ #; $+ = and% can also be used, but each has its own special purpose if a file name includes these characters (/ &? @ #; $+ =%)
These characters and all other characters should be encoded.
The coding process is very simple. Any characters that are not ASCII numbers, letters, or the punctuation marks mentioned earlier will be converted to byte form, and each byte will be written in this form:
A "%" is followed by a two-digit hexadecimal value. Spaces are a special case because they are so common. In addition to being encoded as "% 20", it can also be encoded as a "+".
The plus sign (+) itself is encoded as% 2B. When / = & and? They should all be encoded when used as part of a name, rather than as a separator between URL parts.
Referenc
Http://www.cnblogs.com/jingzhishen/p/3506339.html
2 coding implementation
C++ 's libcurl library does not provide a concrete implementation similar to the URLEncoder class in C# or httpclient similar to Java, so it provides the following case code
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 ='a'& & x ='0' & & x > 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 生产环境编码 上面的代码是对=,&也进行了编码,但是在实际的生产环境中,如果请求数据中携带的是Json格式,并且数据中使用了参数=的方式,这种情况下=&不应该被编码,所以应该单独拆分进行编码,json内容进行编码,参数键值不应该被编,代码修改如下: 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指定了发送的POST数据,要进行URL编码,但是前面的&,=用在POST报文前面,作为参数的时候,是不需要进行编码的,可以直接跳过。例如: loginusername=admin&loginpassword=admin¶m={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::stringstrTemp = ""; size_tlength = 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]; elseif (str[i] == ' ') strTemp+= "+"; else { strTemp+= '%'; strTemp+= ToHex((unsigned char)str[i] >> 4)
StrTemp+= ToHex ((unsigned char) str [I]% 16)
}
}
ReturnstrTemp
}
50 is an approximate number, and the content in json format should be encoded separately, rather than the whole sending message.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.