Author: Lou Adler
How to encode a HTTP URL?
Answer:
1 function HTTPEncode(const AStr: string): string;
2 const
3 NoConversion = ['A'..'Z', 'a'..'z', '*', '@', '.', '_', '-'];
4 var
5 Sp, Rp: PChar;
6 begin
7 SetLength(Result, Length(AStr) * 3);
8 Sp := PChar(AStr);
9 Rp := PChar(Result);
10 while Sp^ <> #0 do
11 begin
12 if Sp^ in NoConversion then
13 Rp^ := Sp^
14 else if Sp^ = ' ' then
15 Rp^ := '+'
16 else
17 begin
18 FormatBuf(Rp^, 3, '%%%.2x', 6, [Ord(Sp^)]);
19 Inc(Rp, 2);
20 end;
21 Inc(Rp);
22 Inc(Sp);
23 end;
24 SetLength(Result, Rp - PChar(Result));
25 end;
26
27 procedure TForm1.Button1Click(Sender: TObject);
28 begin
29 Edit1.Text := HTTPEncode(Edit1.Text);
30 end;
|