Delphi - 将 TStringList 传递给类似于 .Post 方法的 IdHTTP.Get 方法

Delphi - passing TStringList to IdHTTP.Get method similar to .Post method

使用 IdHTTP.Post 方法的工作代码示例:

var
param: TStringList;
spost: string;
...
param := TStringList.Create;
param.Add('something=from somewhere');
param.Add('something1=from somewhere1');
param.Add('something2=from somewhere2');
...
IdHTTP.Request.ContentType := 'application/x-www-form-urlencoded';
spost := IdHTTP.Post('https://url.com/v1/something?', param);
Memo1.Lines.Add(spost); //dump the response to a Memo
param.Free;

您如何传递(执行 same/elicit 相同的行为)TStringListIdHTTP.Get 方法,类似于您在使用 IdHTTP.Post 方法时的做法?

GET 请求通常不携带正文(某些服务器出于非标准目的滥用此请求),因此 TIdHTTP.Get() 没有任何接受正文数据作为输入的重载。

使用 TIdHTTP 在 GET 请求中发送正文的唯一方法是直接调用 TIdCustomHTTP.DoRequest() 方法。但是,它被声明为 protected,因此您必须使用 descendant/accessor class 才能到达它,例如:

type
  TIdHTTPAccess = class(TIdHTTP)
  end;

TIdHTTPAccess(IdHTTP).DoRequest('GET', URL, ReqStream, RespStream, []);

其中 ReqStream 是可选的 TStream,其中包含您要发送的所需请求正文,RespStream 是可选的 TStream,用于接收响应正文。