idhttp indy post,用utf-8做请求(参数)

non*_*one 6 delphi http indy delphi-2009 indy10

我试图在utf-8中发布请求,但是服务器在Ascii中获取它.

尝试了帖子的TstringList格式.

尝试了流格式

尝试强制TStringStream具有UTF8编码.

尝试将indy更新为xe5 indy

这是一个示例代码:

var
  server:TIdHttp;
  Parameters,response:TStringStream;
begin
  response := TStringStream.Create;
  Parameters := TStringSTream.create(UTF8String('param1=Value1&param2=????/???&param3=Value3'),TEncoding.UTF8);
  Server.Post(TIdURI.URLEncode('http://www.example.com/page.php'),Parameters,response);
end;
Run Code Online (Sandbox Code Playgroud)

现在,阿拉伯语编码在网络嗅探器中作为Ascii传递.

0060 d8 b9 d8 b1 d8 a8 d9 8a 2f d8 b9 d8 b1 d8 a8 26 ........ /......&

如何强制Indy Http id在Utf-8中传递请求参数而不在Ascii中传递?

Rem*_*eau 14

TStringStream在D2009 +使用UnicodeString和是 - 意识到TEncoding所以不要UTF8String手动创建:

var
  server: TIdHttp;
  Parameters,response: TStringStream;
begin
  response := TStringStream.Create;
  Parameters := TStringStream.Create('param1=Value1&param2=????/???&param3=Value3', TEncoding.UTF8);
  Server.Post('http://www.example.com/page.php',Parameters,response);
end;
Run Code Online (Sandbox Code Playgroud)

或者,该TStrings版本默认也编码为UTF-8:

var
  server: TIdHttp;
  Parameters: TStringList;
  Response: TStringStream;
begin
  response := TStringStream.Create;
  Parameters := TStringList.Create;
  Parameters.Add('param1=Value1');
  Parameters.Add('param2=????/???');
  Parameters.Add('param3=Value3');
  Server.Post('http://www.example.com/page.php',Parameters,response);
end;
Run Code Online (Sandbox Code Playgroud)

无论哪种方式,您都应该在调用之前设置请求字符集,Post()以便服务器知道您正在发送UTF-8编码数据:

Server.Request.ContentType := 'application/x-www-form-urlencoded';
Server.Request.Charset := 'utf-8';
Run Code Online (Sandbox Code Playgroud)

  • 我向你保证``Posttr(')`版本的`Post()`编码UTF-8就好了(我知道因为我写了它),所以你要么使用过时版本的Indy,要么你没有正确使用它.而我的`TStringStream`示例*确实*在它们所属的`name = value`对之间有`&`delim. (3认同)