我正在使用 Delphi 10.4.2 、 Indy 10.6.2.0 。
\n我已经尝试过这个解决方案:Indy HTTP Server URL-encoded request,但我想我误解了一些东西。
\n我从 Web 应用程序获取 JSON,如下所示:
\nprocedure TForm1.IdHTTPServer1CommandGet(AContext: TIdContext;\n ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);\nbegin\n If ARequestInfo.CommandType = hcPOST then\n begin\n\n MyDecodeAndSetParams(ARequestInfo);\n\n Memo1.Lines.Add(ARequestInfo.Params.Text);\nend;\nRun Code Online (Sandbox Code Playgroud)\n我可能做错了什么,但我的结果仍然不正确:
\nD 卡尔斯鲁厄 / S\xc3\x83\xc2\xbcdweststadt, 班霍夫大街\xc3\x83\xc5\xb8e
\n应该是\nS\xc3\xbcdweststadt 和 Bahnhofstra\xc3\x9fe
\n请帮忙。\n谢谢。
\n您显示的解码文本是 UTF-8 被误解为 Latin-1/8bit 时发生的情况。如果您使用的是最新版本的 Indy,那么正如我在对您链接的帖子的回答TIdHTTPServer中所述,几年前(10.4.2 发布后)已更新以处理此问题,所以您应该'MyDecodeAndSetParams()如果您使用的是最新版本的 Indy,则不再需要。
但是,听起来您使用的是旧版本,应该更新 Indy。但是,如果您无法使用旧版本的 Indy,则必须进行更新MyDecodeAndSetParams()。
由于您正在处理POST请求,因此您很可能会陷入application/x-www-form-urlencoded代码部分,并且我敢打赌该部分ARequestInfo.CharSet是空白的,这将导致CharsetToEncoding()回退到 Indy 的 8 位编码而不是 UTF-8。
尝试更改此行MyDecodeAndSetParams():
LEncoding := CharsetToEncoding(ARequestInfo.CharSet);
Run Code Online (Sandbox Code Playgroud)
为此:
if ARequestInfo.CharSet <> '' then
LEncoding := CharsetToEncoding(ARequestInfo.CharSet)
else
LEncoding := IndyTextEncoding_UTF8;
Run Code Online (Sandbox Code Playgroud)
(我已经更新了我的其他答案中的示例)
附带说明:TIdHTTPServer它是多线程的,它的OnCommandGet事件在工作线程的上下文中触发,因此在访问 UI 控件时必须与主 UI 线程同步,例如:
procedure TForm1.IdHTTPServer1CommandGet(AContext: TIdContext;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
begin
If ARequestInfo.CommandType = hcPOST then
begin
MyDecodeAndSetParams(ARequestInfo);
//Memo1.Lines.Add(ARequestInfo.Params.Text);
TThread.Synchronize(nil,
procedure
begin
Memo1.Lines.Add(ARequestInfo.Params.Text);
end
);
end;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
52 次 |
| 最近记录: |