Indy HTTP Server URL编码的请求 [英] Indy HTTP Server URL-encoded request
问题描述
当Indy HTTP服务器收到url请求时,如/ a?location =%D1%82%D0%B5%D1%81%D1%82(utf-8 url-encoded value),location字段位于requestinfo的Params具有不可读的值ÑÐμÑÑ。
When Indy HTTP server got request with url like a /find?location=%D1%82%D0%B5%D1%81%D1%82 (utf-8 url-encoded value), the "location" field in requestinfo's Params has unreadable value ÑеÑÑ.
如何获得可读值?
Indy HTTPServer ver 10.6.0.4975
How to get readable value? Indy HTTPServer ver 10.6.0.4975
推荐答案
TIdHTTPServer
目前使用 Content-Type
请求标头中指定的字符集解析输入参数,如果没有指定字符集,则使用Indy的8位编码。这是 TIdHTTPServer
的已知限制,因为目前没有选项告诉它使用用户定义的字符集解码参数。因此,您必须手动解析 ARequestInfo.QueryParams
和/或 ARequestInfo.UnparsedParams
属性,例如通过调用 TIdURI.URLDecode()
在 AByteEncoding
参数中直接使用UTF-8编码,例如:
TIdHTTPServer
currently parses input parameters using the charset specified in the Content-Type
request header, and if there is no charset specified than Indy's 8bit encoding is used instead. This is a known limitation of TIdHTTPServer
as there is currently no option to tell it to decode the parameters using a user-defined charset. So you will have to manually parse the ARequestInfo.QueryParams
and/or ARequestInfo.UnparsedParams
property, such as by calling TIdURI.URLDecode()
directly with a UTF-8 encoding in its AByteEncoding
parameter, eg:
procedure MyDecodeAndSetParams(ARequestInfo: TIdHTTPRequestInfo);
var
i, j : Integer;
value: s: string;
LEncoding: IIdTextEncoding;
begin
if IsHeaderMediaType(ARequestInfo.ContentType, 'application/x-www-form-urlencoded') then
begin
value := ARequestInfo.FormParams;
LEncoding := CharsetToEncoding(ARequestInfo.CharSet);
end else
begin
value := ARequestInfo.QueryParams;
LEncoding := IndyTextEncoding_UTF8;
end;
ARequestInfo.Params.BeginUpdate;
try
ARequestInfo.Params.Clear;
i := 1;
while i <= Length(value) do
begin
j := i;
while (j <= Length(value)) and (value[j] <> '&') do
begin
Inc(j);
end;
s := StringReplace(Copy(value, i, j-i), '+', ' ', [rfReplaceAll]);
ARequestInfo.Params.Add(TIdURI.URLDecode(s, LEncoding));
i := j + 1;
end;
finally
ARequestInfo.Params.EndUpdate;
end;
end;
procedure TForm1.IdHTTPServer1CommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
begin
MyDecodeAndSetParams(ARequestInfo);
...
end;
这篇关于Indy HTTP Server URL编码的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!