急求一函数,Delphi版的urlencode

yancey 2005-11-02 06:58:40
求一函数,Delphi版的urlencode

ASP中
Service.URLEnocde("/user")=%2Fuser%2F
Service.URLEnocde("我爱你")=%CE%D2%B0%AE%C4%E3

急求Delphi对应函数,万分谢谢
...全文
503 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
getit911 2005-11-02
  • 打赏
  • 举报
回复
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;
getit911 2005-11-02
  • 打赏
  • 举报
回复

http://www.torry.net/dpfl/dzurl.html

unit DzURL;

{ By Alexander Dzyubenko
alexander@dzsoft.com
http://www.dzsoft.com }

interface

uses 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.com

function HexToInt(HexStr: String): Int64;
// Taken from http://www.delphi3000.com/article.asp?id=1412

implementation

function 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.
柯本 2005-11-02
  • 打赏
  • 举报
回复
用fastnet的TNMURL构件
NMURL1.InputString:='/user/';
Edit1.Text:=NMURL1.Encode;
NMURL1.InputString:='我爱你';
Edit2.Text:=NMURL1.Encode;

5,392

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧