function URLencode(str:string):string;
var
i:integer;
temp:string;
begin
for i:= 1 to length(str) do
begin
temp:=temp+'%'+inttohex(integer(str[i]),0);
end;
result:=temp;
end;
var
i:integer;
temp:string;
begin
for i:= 1 to length(str) do
begin
temp:=temp+'%'+inttohex(integer(str[i]),0);
end;
result:=temp;
end;
function URLdecode(str:string):string;
var
temp:string;
i:integer;
begin
setlength(temp,length(str) div 3);
i:=1;
while(i<length(str)) do
begin
temp[(i+2)div 3]:=char(strtoint('$'+str[i+1]+str[i+2]));
inc(i,3);
end;
Result:=temp;
end;
var
temp:string;
i:integer;
begin
setlength(temp,length(str) div 3);
i:=1;
while(i<length(str)) do
begin
temp[(i+2)div 3]:=char(strtoint('$'+str[i+1]+str[i+2]));
inc(i,3);
end;
Result:=temp;
end;
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;
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit1.Text := HTTPEncode(Edit1.Text);
end;
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;
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit1.Text := HTTPEncode(Edit1.Text);
end;
转载于:https://blog.51cto.com/wesoho/215116