Delphi Indy 404后无响应内容

Delphi Indy no response content after 404

当 Indy 得到 404 时,我无法获取响应内容,它总是空的。

例如,使用 Indy 访问 https://www.etsy.com/people/car,我收到 404 并且内容流(或分配的流)是空的。 对 THttpClient 做同样的事情,我也得到了一个 404,但也在响应流中得到了 404 页面的内容。

所以我的问题是,当 Indy 出现 404 时,我如何才能获得响应内容?

using Indy to access https://www.etsy.com/people/car, I get a 404 and contentstream (or assigned stream) is just empty.

这是设计的默认行为,以防止使用未请求的意外错误数据破坏输出。

how can I get the response content with Indy when its a 404?

有两种可能的方法:

  1. 默认情况下,TIdHTTP 在出现 HTTP 错误时引发 EIdHTTPProtocolException 异常。响应数据将在其 ErrorMessage 属性.

  2. 您可以通过以下任一方式避免引发的异常:

    • TIdHTTP.HTTPOptions 属性 中启用 hoNoProtocolErrorException 标志。

    • 使用具有 AIgnoreReplies 参数的 TIdHTTP.Get() 的重载版本,在该参数中传递 404(或您想要的任何其他错误响应代码)。

    无论哪种方式,默认情况下响应数据仍将被丢弃。您可以通过在 TIdHTTP.HTTPOptions 属性 中启用最近添加的 hoWantProtocolErrorContent 标志来避免这种情况(参见 New TIdHTTP flags and OnChunkReceived event)。