[Delphi]urlencode转换

admin 2015年7月20日17:39:04DelPhi评论3,399 次浏览阅读模式

[Delphi]urlencode转换  

function URLDecode(const S: string): string;
var
  Idx: Integer;
  Hex: string; 
  Code: Integer;
begin  
  Result := '';
  Idx := 1;
  while Idx <= Length(S) do
  begin
    case S[Idx] of
      '%':
      begin
        if Idx <= Length(S) - 2 then
        begin
          Hex := S[Idx+1] + S[Idx+2];
          Code := SysUtils.StrToIntDef('$' + Hex, -1);
          Inc(Idx, 2);
        end
        else
          Code := -1;
         if Code = -1 then
          raise SysUtils.EConvertError.Create(
            'Invalid hex digit in URL'
          );
        Result := Result + Chr(Code);
      end;
      '+':
        Result := Result + ' '
      else
        Result := Result + S[Idx];
    end;
    Inc(Idx);
  end;
end;

  

function URLEncode(const S: string; const InQueryString: Boolean): string;
var
  Idx: Integer; 
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;

相关文章
weinxin
我的微信
微信扫一扫
admin
  • 本文由 发表于 2015年7月20日17:39:04
  • 转载请务必保留本文链接:https://anyun.org/a/jishuguanzhu/bianchengchalou/delphi/2015/0720/5740.html
评论  0  访客  1
    • 看雪

      阿斯达苏打水

    发表评论

    匿名网友 填写信息

    :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: