gpt4 book ai didi

delphi - TIdHttp.Post - 无法获取 utf-8 编码

转载 作者:行者123 更新时间:2023-12-03 15:05:45 24 4
gpt4 key购买 nike

如何强制 TIdHttp.post 方法返回以 utf-8 编码的网页?我尝试了 http.Request.ContentEncoding := 'UTF-8' 但它不起作用。这是我的代码:

procedure TeUpdateNews.Check;
var url: string;
Http: TIdHttp;
SS: TStringStream;
param: TStringList;
SWIDESTRING: WideString;
begin
http := TIDHttp.Create(nil);
http.HandleRedirects := true;
http.ReadTimeout := 5000;
http.Request.ContentEncoding := 'UTF-8';

param := TStringList.create;
param.Clear;

url := CONST_SOME_WWW;

try
SS := TStringStream.Create;
try
SWIDESTRING := http.Post(url, param);

// it's not getting utf-8!
ShowMessage(SWIDESTRING);
finally
SS.Free;
param.free;
end;
finally
Http.Free;
end;
end;

最佳答案

好吧,我发现我做错了什么。万一有人会搜索:

您需要在构造函数中设置 TStringStream 编码,因此应该如下所示:

try
// this is it :-)
SS := TStringStream.Create('', TEncoding.UTF8);
try
// using overloaded post method this time
// it writes return in the ss stream
http.Post(url, param, ss);

// now it's getting utf-8, baby!
ShowMessage(ss.DataString);

关于delphi - TIdHttp.Post - 无法获取 utf-8 编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13000321/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com