delphi - 我在使用 urldecode 方法时遇到错误
问题描述
使用该方法解码 url 编码的字符时出现以下错误TNetEncoding.URL.Decode()
:
目标多字节代码页中不存在 Unicode 字符的映射
url参数是
?call=ExportEventPerspectiveAsCSV&min=2020-03-09%2000:00:00&max=2020-03-09%2023:59:59&type=csv&folder=%F6%E7
我的代码:
var
Str: TStringList;
begin
Str:= TStringList.Create();
Str.Text := TNetEncoding.URL.Decode(ARequestInfo.QueryParams);// URLDecode(Str.Text);
Str.Text := StringReplace(Str.Text, '&', #13, [rfReplaceAll]);
end;
解决方案
默认情况下,TNetEncoding.URL
将编码的字节序列解码为 UTF-8,但%F6%E7
不代表有效的 UTF-8 字节序列,因此无法将其解码为 UTF-8,因此出现“无映射”错误。
AEncoding
您需要在 的可选参数中指定正确的字符集编码(在这种情况下您必须弄清楚它的含义)TURLEncoding.Decode()
,例如:
var
Str: TStringList;
Enc: TEncoding;
begin
Str := TStringList.Create;
try
Enc := TEncoding.GetEncoding('TheCharsetHere'); // <-- !!!
try
Str.Text := TNetEncoding.URL.Decode(ARequestInfo.QueryParams, [TDecodeOption.PlusAsSpaces], Enc);
finally
Enc.Free;
end;
Str.Text := StringReplace(Str.Text, '&', #13, [rfReplaceAll]);
finally
Str.Free;
end;
end;
也就是说,您确实需要在解码之前拆分值对,而不是在解码后拆分它们。这样,&
编码为的字符%26
不会受到虐待,例如:
var
Str: TStringList;
Enc: TEncoding;
I: Integer;
begin
Str := TStringList.Create;
try
Enc := TEncoding.GetEncoding('TheCharsetHere'); // <-- !!!
try
Str.Delimiter := '&';
Str.StrictDelimiter := True;
Str.DelimitedText := ARequestInfo.QueryParams;
for I := 0 to Str.Count-1 do begin
Str[I] := TNetEncoding.URL.Decode(Str[I], [TDecodeOption.PlusAsSpaces], Enc);
end;
finally
Enc.Free;
end;
finally
Str.Free;
end;
end;
推荐阅读
- reactjs - 如何使用 formik 和 typescript、钩子构建自定义 onSubmit 函数
- vue.js - Vue构建后第一次加载耗时5分钟以上
- spring-boot - Spring-batch 未从 setTablePrefix 获取表前缀
- javascript - 如何使用环境变量在 Nuxt 中动态导入 SCSS 样式表
- oracle - 如何仅使用 Oracle 工具将数据库(或仅表)从 PostgreSQL 迁移(转换)到 Oracle?
- hp-uft - 我需要在 UFT One 中添加哪些插件,以便框架类应为 SAPFrame?
- swiftui - 在 SwiftUI 中从左到右排列 HStack 中的项目
- mysql - 获取包含一对多的行。但不是另一个
- python - JSONDecodeError Django Javascript JSON
- angular - NgRx 在调度动作后执行动作(异步)