Delphi IdHTTP 响应文本

Delphi IdHTTP response text

如何在Delphi的IdHTTP中获取类似于JS ajax响应的响应文本?有包含页面内容的标准 responseText 属性,IdHTTP 也有 responseCode 属性,但它表示 HTTP 状态代码。

例如,在 JS ajax 响应中有 statusCode 和 responseText(见图)。

XMLHttpRequest 中的

responseText 等同于 Indy 库中响应的 "Content"。调用 TIdHTTP.Get 提供了几种不同的方法来获取此内容。最简单的方法是将 Get 函数的结果读取为 String...

var
  ResponseText: String;
begin
  ResponseText := IdHTTP1.Get('www.google.com');
  ...
end;

根据内容类型,您可能希望使用 Get 的其他重载之一,例如可以填充 TStream.

好的,我终于找到了解决方案。为此,我使用了 try ... catch 运算符和 EIdHTTPProtocolException:

try
  Result := id_http.Post(url, params);
except
  on E: EIdHTTPProtocolException  do
    if (id_http.ResponseCode = 400) then
      Result := E.ErrorMessage
    else
      Result := E.Message;
end;

抱歉,如果我的问题最初不清楚。