Delphi IdHTTP响应文本

时间:2016-08-24 17:45:01

标签: delphi idhttp

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

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

enter image description here

2 个答案:

答案 0 :(得分:3)

responseText中的

XMLHttpRequest相当于Indy库中的响应“内容”。对TIdHTTP.Get的调用提供了一些获取此内容的不同方法。最简单的方法是将Get函数的结果读作String ...

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

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

答案 1 :(得分:1)

好的,我终于找到了解决方案。为此,我使用了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;

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