我在使用urldecode方法时遇到错误

问题描述 投票:0回答:1

[使用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;
delphi urldecode
1个回答
0
投票

[默认情况下,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
© www.soinside.com 2019 - 2024. All rights reserved.