含中文URL地址编码转换函数UrlEncode
在我们编写程序过程中,有时提交的http链接请求的URL中含有中文参数,直接提交很可能提示有错,所以我们在提交这种HTTP地址前先要将中文参数进行编码转换,然后再提交。经过网上查询,我写了一个转换函数。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// URL地址转换 function UrlEncode(Url: string): string; var i: Integer; raws: RawByteString; begin Result := ''; raws := Utf8Encode(Url); for i := 1 to Length(raws) do case raws[i] of '!', '#', '$', '&', '''', '(', ')', '*', '+', ',', '-', '.', '/', ':', ';', '=', '?', '@', '_', '~', '0' .. '9', 'a' .. 'z', 'A' .. 'Z': Result := Result + raws[i]; else Result := Result + '%' + IntToHex(Ord(raws[i]), 2); end; end; |