delphi urlencode与urldecode


本文整理自网络,侵删。

 
function UrlDecode(const AStr: AnsiString): AnsiString;
var
  Sp, Rp, Cp: PAnsiChar;
  s: AnsiString;
begin
  SetLength(Result, Length(AStr));
  Sp := PAnsiChar(AStr);
  Rp := PAnsiChar(Result);
  Cp := Sp;
  while Sp^ <> #0 do
  begin
    case Sp^ of
      '+':
        Rp^ := ' ';
      '%':
        begin
          // Look for an escaped % (%%) or %<hex> encoded character
          Inc(Sp);
          if Sp^ = '%' then
            Rp^ := '%'
          else
          begin
            Cp := Sp;
            Inc(Sp);
            if (Cp^ <> #0) and (Sp^ <> #0) then
            begin
              s := AnsiChar('$') + Cp^ + Sp^;
              Rp^ := AnsiChar(StrToInt(string(s)));
            end;
          end;
        end;
    else
      Rp^ := Sp^;
    end;
    Inc(Rp);
    Inc(Sp)
  end;
  SetLength(Result, Rp - PAnsiChar(Result));
end;
function UrlEncode(const AStr: AnsiString): AnsiString;
const
  NoConversion = ['A' .. 'Z', 'a' .. 'z', '*', '@', '.', '_', '-',
    '0' .. '9', '$', '!', '''', '(', ')'];
var
  Sp, Rp: PAnsiChar;
begin
  SetLength(Result, Length(AStr) * 3);
  Sp := PAnsiChar(AStr);
  Rp := PAnsiChar(Result);
  while Sp^ <> #0 do
  begin
    if Sp^ in NoConversion then
      Rp^ := Sp^
    else
      if Sp^ = ' ' then
      Rp^ := '+'
    else
    begin
      FormatBuf(Rp^, 3, AnsiString('%%%.2x'), 6, [Ord(Sp^)]);
      Inc(Rp, 2);
    end;
    Inc(Rp);
    Inc(Sp);
  end;
  SetLength(Result, Rp - PAnsiChar(Result));
end;

相关阅读 >>

Delphi 常用控件属性

Delphi 获取其它软件的tlistbox句柄,怎么取得listbox中的数据

Delphi webbrowser ie 窗口选择中,选中的文字内容

Delphi 读取流信息

Delphi 向windows窗口发送alt组合键的问题

Delphi 给动态数组添加一个元素

Delphi listview1 中添加check选中事件

Delphi base64单元encddecd的修改

Delphi 10.4 freeandnil 问题

Delphi用sendmessage获取目录下所有文件

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...