求一函数,Delphi版的urlencodeASP中
Service.URLEnocde("/user")=%2Fuser%2F
Service.URLEnocde("我爱你")=%CE%D2%B0%AE%C4%E3急求Delphi对应函数,万分谢谢

解决方案 »

  1.   

    用fastnet的TNMURL构件
       NMURL1.InputString:='/user/';
       Edit1.Text:=NMURL1.Encode;
       NMURL1.InputString:='我爱你';
       Edit2.Text:=NMURL1.Encode;
      

  2.   


    http://www.torry.net/dpfl/dzurl.htmlunit DzURL;{ By Alexander Dzyubenko
         [email protected]
         http://www.dzsoft.com }interfaceuses SysUtils;function UrlEncode(const DecodedStr: String; Pluses: Boolean): String;
    // Encodes standard string into URL data format.
    // Example: http://www.dzsoft.com -> http%3A%2F%2Fwww.dzsoft.com%2F
    // Pluses parameter specifies whether spaces will be 
    // encoded as '+' or as '%20'function UrlDecode(const EncodedStr: String): String;
    // Decodes URL data into a readable string.
    // Example: http%3A%2F%2Fwww.dzsoft.com%2F -> http://www.dzsoft.comfunction HexToInt(HexStr: String): Int64;
    // Taken from http://www.delphi3000.com/article.asp?id=1412implementationfunction UrlEncode(const DecodedStr: String; Pluses: Boolean): String;
    var
      I: Integer;
    begin
      Result := '';
      if Length(DecodedStr) > 0 then
        for I := 1 to Length(DecodedStr) do
        begin
          if not (DecodedStr[I] in ['0'..'9', 'a'..'z',
                                           'A'..'Z', ' ']) then
            Result := Result + '%' + IntToHex(Ord(DecodedStr[I]), 2)
          else if not (DecodedStr[I] = ' ') then
            Result := Result + DecodedStr[I]
          else
            begin
              if not Pluses then
                Result := Result + '%20'
              else
                Result := Result + '+';
            end;
        end;
    end;function UrlDecode(const EncodedStr: String): String;
    var
      I: Integer;
    begin
      Result := '';
      if Length(EncodedStr) > 0 then
      begin
        I := 1;
        while I <= Length(EncodedStr) do
        begin
          if EncodedStr[I] = '%' then
            begin
              Result := Result + Chr(HexToInt(EncodedStr[I+1]
                                           + EncodedStr[I+2]));
              I := Succ(Succ(I));
            end
          else if EncodedStr[I] = '+' then
            Result := Result + ' '
          else
            Result := Result + EncodedStr[I];      I := Succ(I);
        end;
      end;
    end;function HexToInt(HexStr: String): Int64;
    var RetVar : Int64;
        i : byte;
    begin
      HexStr := UpperCase(HexStr);
      if HexStr[length(HexStr)] = 'H' then
         Delete(HexStr,length(HexStr),1);
      RetVar := 0;  for i := 1 to length(HexStr) do begin
          RetVar := RetVar shl 4;
          if HexStr[i] in ['0'..'9'] then
             RetVar := RetVar + (byte(HexStr[i]) - 48)
          else
             if HexStr[i] in ['A'..'F'] then
                RetVar := RetVar + (byte(HexStr[i]) - 55)
             else begin
                Retvar := 0;
                break;
             end;
      end;  Result := RetVar;
    end;end.
      

  3.   

    function HTTPEncode(const AStr: string): string;
    const
      NoConversion = ['A'..'Z', 'a'..'z', '*', '@', '.', '_', '-'];
    var
      Sp, Rp: PChar;
    begin
      SetLength(Result, Length(AStr) * 3);
      Sp := PChar(AStr);
      Rp := PChar(Result);
      while Sp^ <> #0 do
      begin
        if Sp^ in NoConversion then
          Rp^ := Sp^
        else if Sp^ = ' ' then
          Rp^ := '+'
        else
        begin
          FormatBuf(Rp^, 3, '%%%.2x', 6, [Ord(Sp^)]);
          Inc(Rp, 2);
        end;
        Inc(Rp);
        Inc(Sp);
      end;
      SetLength(Result, Rp - PChar(Result));
    end;