UnitHTTPAppCategoryHTTP utilitiesDelphi syntax:function HTTPEncode(const AStr: String): string;C++ syntax:extern PACKAGE AnsiString __fastcall HTTPEncode(const AnsiString AStr);DescriptionHTTPEncode converts all characters in the AStr parameter except for the letters A through Z (and a through z), numerals 0 through 9, the asterisk (*), dollar sign ($), exclamation point (!), at sign (@), period (.), underscore (_), single quote (?, comma (,) parentheses, and hyphen (-). Spaces are converted to plus characters (+), and all other characters are converted into hex values preceded by the percent sign (%).For example, the string% ?is converted into%%+%3fHTTPEncode returns the converted string.
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;