如何从TWebRequest的内容字段中检索实际的unicode字符串。当我尝试读取TWebRequest的内容字段以获取我在文本中输入的输入unicode值时,我看到加扰值而不是实际值。 我给出的输入是Добро但在内容字段中我看到值Гобр¾¾。 Response内容类型设置为text / html和charset ='UTF-8'。 任何机构都可以告诉它为什么不显示在文本框中输入的实际值以及如何纠正它。
我正在测试的示例代码
procedure TWebModule1.WebModule1HelloAction(Sender: TObject;
Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
s : string;
PageProducer1 : TPageProducer;
begin
Response.ContentType := 'text/html;charset=UTF-8';
s := Request.ContentFields.Text;
PageProducer1 := TPageProducer.Create(nil);
try
PageProducer1.HTMLFile := 'C:\Hello.tmpl';
PageProducer1.OnHTMLTag := PageProducer1HTMLTag;
Response.Content := PageProducer1.Content + ' ' + 'Entered string:' + s;
finally
PageProducer1.Free;
end;
end;
Hello.tmpl只有文本框和提交按钮
答案 0 :(得分:3)
您可以使用UTF8ToString
功能将UTF-8字符串转换为UnicodeString
。
答案 1 :(得分:0)
您只需使用TEncoding.UTF8.GetString(BytesOf(Request.RawContent))
,它会根据请求标头中定义的字符集返回带有正确代码页的AnsiString。不幸的是,您必须手动处理内容。
如果您确定charset是UTF-8,要获取字符串(UnicodeString),请使用var ct: string;
...
ct := string(Request.GetFieldByName('Content-type')).ToUpper;
if (Pos('CHARSET', ct) > 0) and (Pos('UTF-8', ct) > 0) then
Result := TEncoding.UTF8.GetString(BytesOf(Request.RawContent))
else
Result := TEncoding.ANSI.GetString(BytesOf(Request.RawContent));
。或者,您可以使用以下方法检查标题的原始contentType:
TWebRequest.Content
TWebRequest.ContentFields
和TWebRequest.EncodingFromContentType
在我当前版本的delphi(xe7)中出现错误。它们始终以ANSI编码。 TWebRequest.ContentType
尝试从len
中提取字符集,但此时代码中已删除了contentType中的charset部分。