delphi - 使用 Indy 10 接收 Unicode 字符串
问题描述
我正在使用最新的 Delphi 10.4.2 和 Indy 10。
在 REST 服务器中,接收和处理 JSON 命令。它工作正常,除了 Unicode。
像这样的简单 JSON:
{"driverNote": "Test"}
正确显示
如果我现在更改为 Unicode 俄罗斯字符:
{"driverNote": "Статья"}
不知道我应该从哪里开始跟踪这个。我希望ARequestInfo.FormParams
在调试器中具有与变量相同的值s
。
如果我调试 Indy 本身,FormParams
则在此代码中设置:
if LRequestInfo.PostStream <> nil then
begin
// decoding percent-encoded octets and applying the CharSet is handled by
// DecodeAndSetParams() further below...
EnsureEncoding(LEncoding, enc8Bit);
LRequestInfo.FormParams :=
ReadStringFromStream( LRequestInfo.PostStream,
-1,
LEncoding
{$IFDEF STRING_IS_ANSI}, LEncoding{$ENDIF});
DoneWithPostStream(AContext, LRequestInfo); // don't need the PostStream anymore
end;
它使用enc8Bit
. 但我的字符串有 16 位字符。
这在 Indy 中处理不正确吗?
解决方案
您引用的代码片段IdCustomHTTPServer.pas
不是 Indy 的GitHub 存储库中的代码片段。
在官方代码中,TIdHTTPServer
不解码PostStream
toFormParams
除非ContentType
是'application/x-www-form-urlencoded'
:
if LRequestInfo.PostStream <> nil then begin
if TextIsSame(LContentType, ContentTypeFormUrlencoded) then
begin
// decoding percent-encoded octets and applying the CharSet is handled by DecodeAndSetParams() further below...
EnsureEncoding(LEncoding, enc8Bit);
LRequestInfo.FormParams := ReadStringFromStream(LRequestInfo.PostStream, -1, LEncoding{$IFDEF STRING_IS_ANSI}, LEncoding{$ENDIF});
DoneWithPostStream(AContext, LRequestInfo); // don't need the PostStream anymore
end;
end;
该ContentType
检查是在 2010 年添加的,所以我不知道为什么它没有出现在您的版本中。
在您的示例中,ContentType
is 'application/json'
,因此原始 JSON应该在中PostStream
并且FormParams
应该是空白的。
话虽这么说,在您的 Indy 版本中,TIdHTTPServer
只是从 中读取原始字节PostStream
并将每个字节零扩展为FormParams
. 要恢复原始字节,只需将每个字节截断Char
为 8 位Byte
. 例如,您可以在单元中使用 Indy 的ToBytes()
函数,将/指定为字节编码。IdGlobal
enc8Bit
IndyTextEncoding_8Bit
JSON 最常以 UTF-8 传输(在您的示例中就是这种情况),因此当您可以访问任何版本的原始字节时,请确保将 JSON 字节解析为 UTF-8。
推荐阅读
- swift - 如何在swiftui的函数中使用用户输入?
- c# - 如何“使用未分配的局部变量”?
- python - 使用 pk 作为参数传递的 Django 重定向
- unit-testing - jest.fn() v/s jest.mock()?
- nginx - Nginx 与每个 URI 的单独文件夹完全匹配的 uri
- c# - 使用 FTD2XX.DLL 中的 FTCSPI.dll 函数,使用 FT2232H 设备
- python - 根据来自另一个列表的索引从列表列表中获取元素
- cuda - 使用 CUDA 的矩阵的多个 SVD
- html - 卷轴从何而来?
- flutter - 如何允许 webview 中的 mailto 方案颤动