Delphi:Indy - 如何在出错时获取响应主体? [英] Delphi: Indy - how to get the response body on error?

查看:30
本文介绍了Delphi:Indy - 如何在出错时获取响应主体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Delphi6 和 XE3.

Delphi6 and XE3.

我想获取请求的真实响应正文.但是服务器报错500.然后 Indy 用错误描述替换响应文本.

I want to get the real response body of a request. But the server makes error 500. Then the Indy replace the response text with the description of the error.

这个主页也是针对非异常操作中的回答500而设计的,我们需要从响应文本中确定要做什么.

This homepage is designed for answer 500 in non abnormal operations too, and we need to determine what to do from response text.

当状态代码为 500 时,我能否从 IdHTTP 或异常对象获取响应文本?

Could I get the response text from IdHTTP or from an Exception object when the status code is 500?

感谢您提供任何信息!

推荐答案

TIdHTTP 遇到服务器错误时,它会引发 EIdHTTPProtocolException 异常,其中它的 ErrorCode 属性包含 HTTP 状态代码(500 等),其 Message 属性包含 HTTP 状态文本(内部错误"等),以及它的 ErrorMessage属性包含响应的正文文本(如果有).因此,例如:

When TIdHTTP encounters a server error, it raises an EIdHTTPProtocolException exception, where its ErrorCode property contains the HTTP status code (500, etc), its Message property contains the HTTP status text ("Internal Error", etc), and its ErrorMessage property contains the body text of the response, if any. So, for example:

try
  IdHTTP1.Get(...);
except
  on E: EIdHTTPProtocolException do begin
    // use E.ErrorCode, E.Message, and E.ErrorMessage as needed...
  end;
end;

这篇关于Delphi:Indy - 如何在出错时获取响应主体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆