vex*_*xen 2 delphi indy indy10 delphi-xe2
我TIdHTTPServer在Delphi XE2中使用它作为基本的HTML服务器,以从网络获取请求,处理请求并返回所需的响应。
问题是当某人打开类似的页面时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;
Run Code Online (Sandbox Code Playgroud)
Aux_S1 是 'name=Ð'#$009F'еÑ'#$0082'Ñ'#$0080'ов'
Aux_S2 是 'name=?????????'
有些字母显示正确,而其他字母则没有。
我在做什么错,还是应该如何处理这些请求?
URL不允许包含非ASCII字符。必须将这些字符进行字符集编码为字节,然后%HH将其放入URL中时以格式进行编码。因此,您的客户端实际使用的URL如下所示:
http://localhost:5678/book?name=%D0%9F%D0%B5%D1%82%D1%80%D0%BE%D0%B2
Run Code Online (Sandbox Code Playgroud)
%D0%9F%D0%B5%D1%82%D1%80%D0%BE%D0%B2采用??????UTF-8百分比编码格式。
URL无法指定用于这种编码的字符集。由服务器决定。但是,UTF-8是最常用的字符集编码。
TIdHTTPServerOnCommandGet如果ParseParams属性为true(默认情况下为true),则会在触发事件之前自动分析并解码URL查询字符串。因此,请勿UTF8Decode()直接调用参数字符串,因为它将无法正常工作。
不幸的是,TIdHTTPServer当前不允许您指定用于解码查询字符串的字符集(位于TODO列表上)。它的作用是检查请求是否charset在Content-Type标头中包含属性,如果是,则使用该属性(尽管这不是标准的HTTP服务器行为),否则它将使用Indy的内置8位编码。
后一种情况通常发生在GET请求中,因为它们不携带Content-Type标头。不过,这将对您有利(请参阅下文)。字符串值:
'Ð'#$009F'еÑ'#$0082'Ñ'#$0080'ов'
Run Code Online (Sandbox Code Playgroud)
实际上是原始的UTF-8字节,??????当解码为时会被解释为8位“字符” UnicodeString:
#$00D0 #$009F #$00D0 #$00B5 #$00D1 #$0082 #$00D1 #$0080 #$00D0 #$00BE #$00D0 #$00B2
Run Code Online (Sandbox Code Playgroud)
因此,您可以通过手动将解码后的参数字符串转换回原始字节,然后将它们作为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;
Run Code Online (Sandbox Code Playgroud)
或者,将其设置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;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1267 次 |
| 最近记录: |