是否有这个.net方法的Delphi等价物:
Url.UrlEncode()
注意
我已经好几年没用Delphi了.在我阅读答案时,我注意到当前标记的答案有几个备注和备选方案.我没有机会测试它们,所以我的答案基于最受欢迎的.
为了您自己的利益,请检查以后的答案,并在决定提出最佳答案后,以便每个人都可以从您的经验中受益.
Moh*_*man 95
看看indy IdURI单元,它在TIdURI类中有两个静态方法用于Encode/Decode URL.
uses
IdURI;
..
begin
S := TIdURI.URLEncode(str);
//
S := TIdURI.URLDecode(str);
end;
Run Code Online (Sandbox Code Playgroud)
Ali*_*ter 19
另一种简单的方法是在HTTPApp单元中使用HTTPEncode函数 - 非常粗略
Uses
HTTPApp;
function URLEncode(const s : string) : string;
begin
result := HTTPEncode(s);
end
Run Code Online (Sandbox Code Playgroud)
ska*_*adt 13
另一种选择是使用SyapCode单元中具有简单URL编码方法(以及许多其他方法)的Synapse库.
uses
SynaCode;
..
begin
s := EncodeUrl( str );
//
s := DecodeUrl( str );
end;
Run Code Online (Sandbox Code Playgroud)
WeG*_*ars 13
更新2018:下面显示的代码似乎已过时.看雷米的评论.
class function TIdURI.ParamsEncode(const ASrc: string): string;
var
i: Integer;
const
UnsafeChars = '*#%<> []'; {do not localize}
begin
Result := ''; {Do not Localize}
for i := 1 to Length(ASrc) do
begin
if CharIsInSet(ASrc, i, UnsafeChars) or (not CharIsInSet(ASrc, i, CharRange(#33,#128))) then begin {do not localize}
Result := Result + '%' + IntToHex(Ord(ASrc[i]), 2); {do not localize}
end else begin
Result := Result + ASrc[i];
end;
end;
end;
Run Code Online (Sandbox Code Playgroud)
来自印地.
无论如何,Indy工作不正常,所以你需要看看这篇文章:http:
//marc.durdin.net/2012/07/indy-tiduri-pathencode-urlencode-and-paramsencode-and-more/
Rad*_*dík 13
我自己创建了这个函数来编码除了非常安全的字符之外的所 特别是我有+问题.请注意,您无法使用此函数对整个URL进行编码,但您需要包含您想要没有特殊含义的部分,通常是变量的值.
function MyEncodeUrl(source:string):string;
var i:integer;
begin
result := '';
for i := 1 to length(source) do
if not (source[i] in ['A'..'Z','a'..'z','0','1'..'9','-','_','~','.']) then result := result + '%'+inttohex(ord(source[i]),2) else result := result + source[i];
end;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
72335 次 |
| 最近记录: |