url转义 delphi 有没有实例代码 ?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了url转义 delphi 有没有实例代码 ?相关的知识,希望对你有一定的参考价值。
在delphi里面使用URL转义字符。。
Delphi2006中有这样一个单元:HTTPApp
,里面有这样的函数:
function HTTPEncode(const AStr: string): string;
function HTTPDecode(const AStr: string): string;
下面是这两个函数的原代码,如果你的Delphi版本比较低,可以使用下面的代码
function HTTPDecode(const AStr: String): String;
var
Sp, Rp, Cp: PChar;
S: String;
begin
SetLength(Result, Length(AStr));
Sp := PChar(AStr);
Rp := PChar(Result);
Cp := Sp;
try
while Sp^ <> #0 do
begin
case Sp^ of
'+': Rp^ := ' ';
'%': begin
// Look for an escaped % (%%) or %<hex> encoded character
Inc(Sp);
if Sp^ = '%' then
Rp^ := '%'
else
begin
Cp := Sp;
Inc(Sp);
if (Cp^ <> #0) and (Sp^ <> #0) then
begin
S := '$' + Cp^ + Sp^;
Rp^ := Chr(StrToInt(S));
end
else
raise EWebBrokerException.CreateFmt(sErrorDecodingURLText, [Cp - PChar(AStr)]);
end;
end;
else
Rp^ := Sp^;
end;
Inc(Rp);
Inc(Sp);
end;
except
on E:EConvertError do
raise EConvertError.CreateFmt(sInvalidURLEncodedChar,
['%' + Cp^ + Sp^, Cp - PChar(AStr)])
end;
SetLength(Result, Rp - PChar(Result));
end;
function HTTPEncode(const AStr: String): String;
// The NoConversion set contains characters as specificed in RFC 1738 and
// should not be modified unless the standard changes.
const
NoConversion = ['A'..'Z','a'..'z','*','@','.','_','-',
'0'..'9','$','!','''','(',')'];
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;
function htmlEncode(const AStr: String): String;
const
Convert = ['&','<','>','"'];
var
Sp, Rp: PChar;
begin
SetLength(Result, Length(AStr) * 10);
Sp := PChar(AStr);
Rp := PChar(Result);
while Sp^ <> #0 do
begin
case Sp^ of
'&': begin
FormatBuf(Rp^, 5, '&', 5, []);
Inc(Rp,4);
end;
'<',
'>': begin
if Sp^ = '<' then
FormatBuf(Rp^, 4, '<', 4, [])
else
FormatBuf(Rp^, 4, '>', 4, []);
Inc(Rp,3);
end;
'"': begin
FormatBuf(Rp^, 6, '"', 6, []);
Inc(Rp,5);
end;
else
Rp^ := Sp^
end;
Inc(Rp);
Inc(Sp);
end;
SetLength(Result, Rp - PChar(Result));
end;
function HTMLDecode(const AStr: String): String;
var
Sp, Rp, Cp, Tp: PChar;
S: String;
I, Code: Integer;
begin
SetLength(Result, Length(AStr));
Sp := PChar(AStr);
Rp := PChar(Result);
Cp := Sp;
try
while Sp^ <> #0 do
begin
case Sp^ of
'&': begin
Cp := Sp;
Inc(Sp);
case Sp^ of
'a': if AnsiStrPos(Sp, 'amp;') = Sp then do not localize
begin
Inc(Sp, 3);
Rp^ := '&';
end;
'l',
'g': if (AnsiStrPos(Sp, 'lt;') = Sp) or (AnsiStrPos(Sp, 'gt;') = Sp) then do not localize
begin
Cp := Sp;
Inc(Sp, 2);
while (Sp^ <> ';') and (Sp^ <> #0) do
Inc(Sp);
if Cp^ = 'l' then
Rp^ := '<'
else
Rp^ := '>';
end;
'q': if AnsiStrPos(Sp, 'quot;') = Sp then do not localize
begin
Inc(Sp,4);
Rp^ := '"';
end;
'#': begin
Tp := Sp;
Inc(Tp);
while (Sp^ <> ';') and (Sp^ <> #0) do
Inc(Sp);
SetString(S, Tp, Sp - Tp);
Val(S, I, Code);
Rp^ := Chr((I));
end;
else
raise EConvertError.CreateFmt(sInvalidHTMLEncodedChar,
[Cp^ + Sp^, Cp - PChar(AStr)])
end;
end
else
Rp^ := Sp^;
end;
Inc(Rp);
Inc(Sp);
end;
except
on E:EConvertError do
raise EConvertError.CreateFmt(sInvalidHTMLEncodedChar,
[Cp^ + Sp^, Cp - PChar(AStr)])
end;
SetLength(Result, Rp - PChar(Result));
end; 参考技术A 先用AnsiToUtf8,转成UTF-8
然后再用NMURL1控件转
procedure TForm1.Button1Click(Sender: TObject);
var
s:string;
begin
s:= '中国 ';
s:=AnsiToUtf8(s);
NMURL1.InputString:=s;
Edit1.Text:=NMURL1.Encode;
end;
另外,你也可用HTTPEncode
procedure TForm1.Button1Click(Sender: TObject);
var
s:string;
begin
s:= '中国 ';
s:=AnsiToUtf8(s);
Edit1.Text:=HTTPEncode(s);
end;本回答被提问者和网友采纳
以上是关于url转义 delphi 有没有实例代码 ?的主要内容,如果未能解决你的问题,请参考以下文章
Android实例-Delphi在运行时更改Android屏幕旋转(IOS也支持,但还没有写,下午我回来加上。不过我可没有苹果机,测试不了)