国泰 發表於 2019-12-3 11:10:00

Delphi UTF/URL编码/解码 UTF8Encode、UTF8Decode、URLEncode、URLDecode

<p><strong><span style="font-size: 16px">Delphi UTF/URL编码/解码 UTF8Encode、UTF8Decode、URLEncode、URLDecode</span></strong></p>
<p><strong><span style="font-size: 16px">一、URL简介</span></strong></p>
<ul>
<li><span style="font-size: 16px">URL是网页的地址,比如&nbsp;http://www.cnblogs.com。Web 浏览器通过 URL 从 web 服务器请求页面。</span></li>
<li><span style="font-size: 16px">由于URL字符串常常会包含非ASCII字符,URL在传输过程中,往往出现错误。因此,可以将非字符串字符,让一些特殊ASCII字符组合,代替非ASCII字符。这就是编码转换,当字符串传输后,可以返回原RUL字符串(解码)。</span></li>
<li><span style="font-size: 16px">URL只能使用 ASCII 字符集来通过因特网进行发送。URL编码,就是会将RUL字符转换为可通过因特网传输的格式。</span></li>
<li><span style="font-size: 16px">URL编码使用“%”其后跟随两位的十六进制数来替换非 ASCII 字符。比如“®”用“%A9”代替。</span></li>
<li><span style="font-size: 16px">URL不能包含空格。URL编码通常使用“+”来替换空格。</span></li>
</ul>
<p><strong><span style="font-size: 16px">二、URL编码与解码</span></strong><br><span style="font-size: 16px">&nbsp;&nbsp;&nbsp; 1、uses HttpApp;&nbsp; &nbsp;//引用单元</span></p>
<p><span style="font-size: 16px">&nbsp;&nbsp;&nbsp; 2、编码,先UTF8编码,然后再URL编码,不然和标准的url_encode()编码结果不一致,查询结果自然不是预期的</span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;">S2 := HttpEncode(UTF8Encode(S1));</pre>
</div>
<p><span style="font-size: 16px">&nbsp;&nbsp;&nbsp; 3、解码,先URL解码,然后再UTF8解码,否则结果是乱码。</span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;">S1 := UTF8Decode(HttpDecode(S2));</pre>
</div>
<p><span style="font-size: 16px"> 以上是内置函数调用</span></p>
<p><strong><span style="font-size: 16px">三、URLEncode、URLDecode&nbsp;</span></strong></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;"><span style="font-size: 16px">//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 &lt;= Length(S) do
begin
    case S of
      '%':
      begin
      // % should be followed by two hex digits - exception otherwise
      if Idx &lt;= Length(S) - 2 then
      begin
          // there are sufficient digits - try to decode hex digits
          Hex := S + S;
          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;
    end;
    Inc(Idx);
end;
end;

//URLDecode&nbsp;
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 of
      'A'..'Z', 'a'..'z', '0'..'9', '-', '_', '.':
      Result := Result + S;
      ' ':
      if InQueryString then
          Result := Result + '+'
      else
          Result := Result + '%20';
      else
      Result := Result + '%' + SysUtils.IntToHex(Ord(S), 2);
    end;
end;
end;
</span></pre>
</div>
<p><span style="font-size: 16px">  </span></p>
<p><strong><span style="font-size: 16px">四、示例</span></strong></p>
<p><span style="font-size: 16px">示例1:</span></p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;">uses
Httpapp;

begin
sStr:=HttpEncode(UTF8EnCode('滔Roy'));
//或:
//sStr:=HttpEncode(AnsiToUtf8('滔Roy'));
end;</pre>
</div>
<p>示例2:</p>
<div class="cnblogs_Highlighter">
<pre class="brush:delphi;gutter:true;">uses
IdURI;

begin
sStr := TIdURI.URLEncode(str);
//
sStr := TIdURI.URLDecode(str);
end;
</pre>
</div>
<p>  </p>
<p>  </p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><span style="font-size: 14px; color: rgba(136, 136, 136, 1)">&nbsp;创建时间:2019.12.03  更新时间:2021.04.29</span></p>

</div>
<div id="MySignature" role="contentinfo">
    博客园 滔Roy https://www.cnblogs.com/guorongtao 希望内容对你有所帮助,谢谢!<br><br>
来源:https://www.cnblogs.com/guorongtao/p/11975639.html
頁: [1]
查看完整版本: Delphi UTF/URL编码/解码 UTF8Encode、UTF8Decode、URLEncode、URLDecode