从 TWebRequest 的内容字段读取 unicode 字符串
我们如何从 TWebRequest 的内容字段中检索实际的 unicode 字符串。当我尝试读取 TWebRequest 的内容字段以获取我在文本中输入的输入 unicode 值时,我看到的是加扰值而不是实际值。 我给出的输入是 Добро,但在内容字段中我看到值 ДÐ⁄брÐ⁄。响应内容类型设置为 text/html 和 charset='UTF-8'。 任何人都可以告诉为什么它不显示在文本框中输入的实际值以及如何纠正这个问题。
我正在测试 Hello.tmpl 的示例代码
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;
只有文本框和提交按钮
How can we retrieve actual unicode string from the content fields of TWebRequest. When i try to read content fields of TWebRequest to get the input unicode value i have entered in a text i see scrambled value instead of the actual.
The input which i gave was Добро but in the content fields i see the value Добро. The Response contenttype is set to text/html and charset='UTF-8'.
Can any body tell why doesn't it show the actual value entered in the text box and how this can be corrected.
sample code which i was testing
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 just has text box and submit button
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
UTF8ToString
函数来转换 UTF -8 字符串到UnicodeString
。You can use the
UTF8ToString
function to convert your UTF-8 string to aUnicodeString
.您只需要使用
TWebRequest.ContentRaw
,它会根据请求标头中定义的字符集返回具有正确代码页的 AnsiString 。不幸的是,您必须手动处理内容。如果您确定字符集是 UTF-8,要获取字符串 (UnicodeString),请使用
TEncoding.UTF8.GetString(BytesOf(Request.RawContent))
。或者,您可以使用以下方式检查标头的原始 contentType:TWebRequest.Content
和TWebRequest.ContentFields
在我当前版本的 delphi (xe7)。它们始终以 ANSI 编码。TWebRequest.EncodingFromContentType
尝试从TWebRequest.ContentType
中提取字符集,但此时 contentType 中的字符集部分已被之前的代码删除。You just need to use
TWebRequest.ContentRaw
which return an AnsiString with correct code page based on charset defined in request header. Unfortunately you will have to process content manually.To get a string (UnicodeString) use
TEncoding.UTF8.GetString(BytesOf(Request.RawContent))
if you are sure that charset is UTF-8. Alternatively you can check original contentType of header with:TWebRequest.Content
andTWebRequest.ContentFields
are bugged in my current version of delphi (xe7). They are always encoded in ANSI.TWebRequest.EncodingFromContentType
try to extract charset fromTWebRequest.ContentType
, but charset part in contentType is already removed by previous code at this point.