如何在Delphi的TMS WEB Core中编解码URL字符串?
适用于TMS WEB Core的URL编解码方案
在TMS WEB Core环境中,System.NetEncoding、IdURI、HTTPApp这类VCL/FMX专属单元确实无法使用,不过有两种官方推荐的方式实现URL编解码:
1. 直接调用JavaScript原生API
由于TMS WEB Core最终编译为JavaScript运行在浏览器环境,直接使用Web标准的encodeURIComponent和decodeURIComponent函数是最简便的方式,代码示例如下:
编码实现
function URLEncode(const AInput: string): string; begin Result := TJSWindow(window).encodeURIComponent(AInput); end;
解码实现
function URLDecode(const AInput: string): string; begin Result := TJSWindow(window).decodeURIComponent(AInput); end;
2. 使用TMS WEB Core内置工具类
TMS WEB Core的Web.HTTPUtils单元提供了封装好的URL处理方法,在uses列表添加该单元后,可直接调用:
HTTPEncode:完成URL编码HTTPDecode:完成URL解码
示例代码:
uses Web.HTTPUtils; // 编码操作 var EncodedStr: string; begin EncodedStr := HTTPEncode('需要编码的内容'); end; // 解码操作 var DecodedStr: string; begin DecodedStr := HTTPDecode('已编码的内容'); end;
注意:HTTPEncode/HTTPDecode与JS原生函数的编码规则略有差异——前者会将空格转为+,而encodeURIComponent会将空格转为%20,可根据实际需求选择。
内容的提问来源于stack exchange,提问作者Shaun Roselt
相关产品推荐
相关产品推荐

