[使用TNetEncoding.URL.Decode()
方法解码URL编码的字符时出现以下错误:
目标多字节代码页中不存在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()
参数中指定正确的字符集编码(您必须弄清楚这种情况下的含义),例如:
TURLEncoding.Decode()
也就是说,您确实需要拆分值对before对其进行解码,而不是拆分它们after解码。这样,编码为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