Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi 从TWebRequest的内容字段读取unicode字符串_Delphi_Web Applications_Unicode_Delphi Xe2_Xe7 - Fatal编程技术网

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

Delphi 从TWebRequest的内容字段读取unicode字符串,delphi,web-applications,unicode,delphi-xe2,xe7,Delphi,Web Applications,Unicode,Delphi Xe2,Xe7,我们如何从TWebRequest的内容字段中检索实际的unicode字符串。当我试图读取TWebRequest的内容字段以获取我在文本中输入的unicode输入值时,我看到的是加扰的值,而不是实际值。 我给出的输入是БбБ,但在内容字段中,我看到了值ГобÐо。响应contenttype设置为text/html和charset='UTF-8'。 任何主体都可以告诉我们为什么不显示文本框中输入的实际值,以及如何更正 我正在测试的示例代码 procedure TWebModule1.WebM

我们如何从TWebRequest的内容字段中检索实际的unicode字符串。当我试图读取TWebRequest的内容字段以获取我在文本中输入的unicode输入值时,我看到的是加扰的值,而不是实际值。 我给出的输入是БбБ,但在内容字段中,我看到了值ГобÐо。响应contenttype设置为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只有文本框和submit按钮

您可以使用该函数将UTF-8字符串转换为UnicodeString您只需使用TWebRequest.ContentRaw即可,它根据请求头中定义的字符集返回具有正确代码页的AnsiString。不幸的是,您必须手动处理内容

要获取字符串(UnicodeString),请使用
TEncoding.UTF8.GetString(BytesOf(Request.RawContent))
,如果您确定字符集是UTF-8。或者,您可以使用以下选项检查标题的原始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.Content
TWebRequest.ContentFields
在我当前版本的()中有错误。它们总是用ANSI编码
TWebRequest.EncodingFromContentType
尝试从
TWebRequest.ContentType
中提取字符集,但此时ContentType中的字符集部分已被以前的代码删除。

感谢您的回复,UTF8ToString工作得很好,但是否因为字符集设置为UTF-8,字符串是ut8编码的,我们需要转换为unicode字符串。是否有任何方法或设置可以在请求对象上设置,以便它自动进行转换HTTP有效负载是八位字节的任意序列。内容类型(和内容编码)字段告诉您如何解释它。您的应用程序负责读取正确的头字段并以正确的方式处理有效负载。这将正常工作,但会发出警告W1058隐式字符串转换,可能会将数据从“string”丢失到“RawByteString”。将其与使用RawContent而不是Content结合使用,警告就会消失。