Chinese character encoding of URL in Delphi
Show. asp? Sort = All & sortlevel = 1 & gorq = for & n = 5 & sitename = All & IMG = Yes & imgfile =/images/dot_g.gif
In this form, Baidu queries are converted to gb2312 encoding. Each Chinese Character corresponds to 2% XX, but on Google, each Chinese Character corresponds to three % XX, which adopts unicode encoding.
In delphi2010, due to the introduction of Unicode, the default value is 3% XX, which leads to a problem in my program. After searching for half a day, each function can implement automatic detection of full URL encoding, so I wrote one and shared it with you:
Uses
Httpapp;
Function urlencode (aurl: string): string;
VaR
I: integer;
Stmp, tstmp: string;
Begin
Result: = aurl;
If length (aurl)> 0 then
Begin
For I: = 1 to length (aurl) Do
Begin
If INTEGER (ord (aurl [I])> 255 then
Begin
Stmp: = copy (aurl, I, 1 );
Tstmp: = httpencode (stmp );
Result: = stringreplace (result, stmp, tstmp, []);
End;
End;
End;
End;
Usage:
URL: = urlencode (URL );
Then, the Chinese characters in the URL are automatically converted to the % XX encoding of gb2312.
Refer:
Http://ifso.iteye.com/blog/1513438