gpt4 book ai didi

delphi - 如何在 TIdHTTPServer 中接收包含外来字符的查询字符串

转载 作者:行者123 更新时间:2023-12-02 01:42:58 26 4
gpt4 key购买 nike

我在 Delphi XE2 中使用 TIdHTTPServer 作为基本的 HTML 服务器来获取来自 Web 的请求,处理它们并返回所需的响应。

问题是当有人打开像 localhost:5678/book?name=Петров 这样的页面时,我无法正确接收名称“Петров”。

此时的过程很简单:

procedure TMain.IdHTTPServer1CommandGet(AContext: TIdContext;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
Aux_S1 : String;
Aux_S2 : String;
begin

Aux_S1 := ARequestInfo.Params[0];

Aux_S2 := System.UTF8Decode(ARequestInfo.Params[0]);

end;

Aux_S1'name=Ð'#$009F'еÑ'#$0082'Ñ'#$0080'Ð⁄в'

Aux_S2'name=�?е�?�?ов'

有些字母可以正确显示,但其他字母则不能。

我做错了什么,或者我应该如何处理这些请求?

最佳答案

URL 不允许包含非 ASCII 字符。此类字符必须经过字符集编码为字节,然后在放入 URL 时以 %HH 格式进行编码。因此,您的客户端实际使用的 URL 更像是这样的:

http://localhost:5678/book?name=%D0%9F%D0%B5%D1%82%D1%80%D0%BE%D0%B2

%D0%9F%D0%B5%D1%82%D1%80%D0%BE%D0%B2 是 UTF-8 百分比编码的 Петров格式。

URL 无法指定用于此类编码的字符集。由服务器决定。不过,UTF-8 是最常用的字符集编码。

如果 ParseParams 属性为 true(这是由默认)。因此,不要直接在参数字符串上调用 UTF8Decode(),因为它不起作用。

不幸的是,TIdHTTPServer 目前不允许您指定用于解码查询字符串(位于 TODO 列表中)的字符集。它的作用是检查请求的 Content-Type header 中是否包含 charset 属性,如果是,则使用它(但这不是标准的 HTTP 服务器行为) ,否则它使用 Indy 的内置 8 位编码。

后一种情况通常发生在 GET 请求中,因为它们不携带 Content-Type header 。不过,这将对您有利(请参阅下文)。字符串值:

'Ð'#$009F'еÑ'#$0082'Ñ'#$0080'ов'

实际上,Петров 的原始 UTF-8 字节在解码为 UnicodeString 时被解释为 8 位“字符”:

#$00D0 #$009F #$00D0 #$00B5 #$00D1 #$0082 #$00D1 #$0080 #$00D0 #$00BE #$00D0 #$00B2 

因此,您可以通过手动将解码的参数字符串转换回原始字节,然后将它们作为 UTF-8 解码回字符串来“修复”此解码不匹配,例如:

procedure TMain.IdHTTPServer1CommandGet(AContext: TIdContext;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
Aux_S1: String;
begin
// if you are not using Indy 10.6+, you can replace
// IndyTextEncoding_UTF8 with TIdTextEncoding.UTF8,
// and IndyTextEncoding_8bit with Indy8BitEncoding...
//
//Aux_S1 := TIdTextEncoding.UTF8.GetString(ToBytes(ARequestInfo.Params[0], Indy8BitEncoding));
Aux_S1 := IndyTextEncoding_UTF8.GetString(ToBytes(ARequestInfo.Params[0], IndyTextEncoding_8bit));

end;

或者,将 ParseParams 设置为 false 并手动解码 ARequestInfo.QueryParams 字符串(来自 URL 的原始百分比编码数据):

procedure DecodeParams(const AValue: String; Params: TStrings);
var
i, j : Integer;
s: string;

// if you are not using Indy 10.6+, you can replace
// IIdTextEncoding with TIdTextEncoding...
//
//LEncoding: TIdTextEncoding;
LEncoding: IIdTextEncoding;
begin
// Convert special characters
// ampersand '&' separates values {Do not Localize}
Params.BeginUpdate;
try
Params.Clear;

// if you are not using Indy 10.6+, you can replace
// IndyTextEncoding_UTF8 with TIdTextEncoding.UTF8...
//
//LEncoding := TIdTextEncoding.UTF8;
LEncoding := IndyTextEncoding_UTF8;

i := 1;
while i <= Length(AValue) do
begin
j := i;
while (j <= Length(AValue)) and (AValue[j] <> '&') do {do not localize}
begin
Inc(j);
end;
s := Copy(AValue, i, j-i);
// See RFC 1866 section 8.2.1. TP
s := ReplaceAll(s, '+', ' '); {do not localize}
Params.Add(TIdURI.URLDecode(s, LEncoding));
i := j + 1;
end;
finally
Params.EndUpdate;
end;
end;

procedure TMain.IdHTTPServer1CommandGet(AContext: TIdContext;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
Aux_S1: String;
begin
DecodeParams(LRequestInfo.QueryParams, ARequestInfo.Params);
Aux_S1 := ARequestInfo.Params[0];
end;

关于delphi - 如何在 TIdHTTPServer 中接收包含外来字符的查询字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32733033/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com