从 TWebRequest 的内容字段读取 unicode 字符串

发布于 2024-12-28 10:41:32 字数 856 浏览 2 评论 0原文

我们如何从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情感失落者 2025-01-04 10:41:32

您可以使用 UTF8ToString 函数来转换 UTF -8 字符串到 UnicodeString

You can use the UTF8ToString function to convert your UTF-8 string to a UnicodeString.

浮光之海 2025-01-04 10:41:32

您只需要使用 TWebRequest.ContentRaw ,它会根据请求标头中定义的字符集返回具有正确代码页的 AnsiString 。不幸的是,您必须手动处理内容。

如果您确定字符集是 UTF-8,要获取字符串 (UnicodeString),请使用 TEncoding.UTF8.GetString(BytesOf(Request.RawContent))。或者,您可以使用以下方式检查标头的原始 contentType:

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));

TWebRequest.ContentTWebRequest.ContentFields 在我当前版本的 ()。它们始终以 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:

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));

TWebRequest.Content and TWebRequest.ContentFields are bugged in my current version of (). They are always encoded in ANSI. TWebRequest.EncodingFromContentType try to extract charset from TWebRequest.ContentType, but charset part in contentType is already removed by previous code at this point.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文