如何使用 TIdHttp 获取 DELETE 的响应文本?

How to get the response text of a DELETE with TIdHttp?

Indy 的 TIdHttp class 在最近的版本中有一个 Delete() 例程。但出于某种原因,这是一个过程,而不是一个函数。 Put()Get()等都是return响应体内容的函数。作为字符串或将其传送到 TStream。这与 Delete() 是不可能的,这与 DELETE 的定义相矛盾:

A successful response SHOULD be 200 (OK) if the response includes an entity describing the status, 202 (Accepted) if the action has not yet been enacted, or 204 (No Content) if the action has been enacted but the response does not include an entity.

Source.

然后我尝试使用 GetResponse(),但这只是优雅地关闭了我的连接,而没有填写响应。

那么如何从 DELETE 响应中读取响应正文的内容?

您需要更新 Indy 安装。 2013 年 9 月在 SVN 修订版 5056 中针对此功能请求添加了用于获取 DELETE 方法响应的重载:

Add overloads for TIdHTTP Delete() and Options() methods that can output a response's message body

如果您出于某种原因不想更新 Indy,您可以子类化 TIdHTTP 组件并添加一个将响应流传递给 DoRequest 方法的方法,例如:

type
  TIdHTTP = class(IdHTTP.TIdHTTP)
  public
    procedure DeleteEx(AURL: string; AResponseContent: TStream);
  end;

implementation

{ TIdHTTP }

procedure TIdHTTP.DeleteEx(AURL: string; AResponseContent: TStream);
begin
  DoRequest(Id_HTTPMethodDelete, AURL, nil, AResponseContent, []);
end;