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有无生成空格的函数,like vb的space(1)

Delphi 遍历某字段并插入到combobox

Delphi 通过pid获取进程名

Delphi处理http请求自定义header

Delphi的一些开发技巧和方法

Delphi判断不同格式的时间是否相等

Delphi的unicode与gb2312转转换,汉字unicode转gb2312

Delphi中实现文件拷贝的三种方法

tstrings 的用法

Delphi检测android mock位置

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



打赏

取消

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

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

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

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

评论

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