Delphi UTF/URL编码/解码 UTF8Encode、UTF8Decode、URLEncode、URLDecode
Delphi UTF/URL编码/解码 UTF8Encode、UTF8Decode、URLEncode、URLDecode
一、URL简介
- URL是网页的地址,比如 http://www.cnblogs.com。Web 浏览器通过 URL 从 web 服务器请求页面。
- 由于URL字符串常常会包含非ASCII字符,URL在传输过程中,往往出现错误。因此,可以将非字符串字符,让一些特殊ASCII字符组合,代替非ASCII字符。这就是编码转换,当字符串传输后,可以返回原RUL字符串(解码)。
- URL只能使用 ASCII 字符集来通过因特网进行发送。URL编码,就是会将RUL字符转换为可通过因特网传输的格式。
- URL编码使用“%”其后跟随两位的十六进制数来替换非 ASCII 字符。比如“®”用“%A9”代替。
- URL不能包含空格。URL编码通常使用“+”来替换空格。
二、URL编码与解码
1、uses HttpApp; //引用单元
2、编码,先UTF8编码,然后再URL编码,不然和标准的url_encode()编码结果不一致,查询结果自然不是预期的
1 | S2 := HttpEncode(UTF8Encode(S1)); |
3、解码,先URL解码,然后再UTF8解码,否则结果是乱码。
1 | S1 := UTF8Decode(HttpDecode(S2)); |
以上是内置函数调用
三、URLEncode、URLDecode
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | //URLEncode function URLDecode( const S: string ): string ; var Idx: Integer ; // loops thru chars in string Hex: string ; // string of hex characters Code: Integer ; // hex character code (-1 on error) begin // Intialise result and string index Result := '' ; Idx := 1 ; // Loop thru string decoding each character while Idx <= Length(S) do begin case S[Idx] of '%' : begin // % should be followed by two hex digits - exception otherwise if Idx <= Length(S) - 2 then begin // there are sufficient digits - try to decode hex digits Hex := S[Idx+ 1 ] + S[Idx+ 2 ]; Code := SysUtils . StrToIntDef( '$' + Hex, - 1 ); Inc(Idx, 2 ); end else // insufficient digits - error Code := - 1 ; // check for error and raise exception if found if Code = - 1 then raise SysUtils . EConvertError . Create( 'Invalid hex digit in URL' ); // decoded OK - add character to result Result := Result + Chr(Code); end ; '+' : // + is decoded as a space Result := Result + ' ' else // All other characters pass thru unchanged Result := Result + S[Idx]; end ; Inc(Idx); end ; end ; //URLDecode function URLEncode( const S: string ; const InQueryString: Boolean ): string ; var Idx: Integer ; // loops thru characters in string begin Result := '' ; for Idx := 1 to Length(S) do begin case S[Idx] of 'A' .. 'Z' , 'a' .. 'z' , '0' .. '9' , '-' , '_' , '.' : Result := Result + S[Idx]; ' ' : if InQueryString then Result := Result + '+' else Result := Result + '%20' ; else Result := Result + '%' + SysUtils . IntToHex(Ord(S[Idx]), 2 ); end ; end ; end ; |
四、示例
示例1:
1 2 3 4 5 6 7 8 | uses Httpapp; begin sStr:=HttpEncode(UTF8EnCode( '滔Roy' )); //或: //sStr:=HttpEncode(AnsiToUtf8('滔Roy')); end ; |
示例2:
1 2 3 4 5 6 7 8 | uses IdURI; begin sStr := TIdURI . URLEncode(str); // sStr := TIdURI . URLDecode(str); end ; |
创建时间:2019.12.03 更新时间:2021.04.29
博客园 滔Roy https://www.cnblogs.com/guorongtao 希望内容对你有所帮助,谢谢!
分类:
Delphi Web
, Delphi 编码/格式
标签:
Delphi
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)