Delphi Indy 404后无响应内容 [英] Delphi Indy no response content after 404

查看:163
本文介绍了Delphi Indy 404后无响应内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当Indy收到404时,我无法获得响应内容,它始终为空。

When Indy get a 404, I cannot get the response content, its always empty.

例如,使用Indy访问 https://www.etsy.com/people/car ,我得到一个404,contentstream(或分配的流)只是空的。
使用THttpClient进行相同的操作,我还得到了404,但是还获得了响应流中404页面的内容。

For example, using Indy to access https://www.etsy.com/people/car, I get a 404 and contentstream (or assigned stream) is just empty. Doing the same with THttpClient, I also get a 404, but also get the content of the 404 page in the response stream.

所以我的问题是,

推荐答案


使用Indy进行访问时,如何获得Indy的响应内容? https://www.etsy.com/people/car ,我得到了404和contentstream (或分配的流)为空。

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

根据设计,这是默认行为,可防止因意外错误数据而破坏输出

This is the default behavior, by design, to prevent corrupting the output with unexpected error data that was not requested.


我如何获得Indy 404的响应内容?

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

有两种可能的方式:


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

  1. By default, TIdHTTP raises an EIdHTTPProtocolException exception on an HTTP error. The response data will be in its ErrorMessage property.

您可以通过以下两种方法避免引发异常:

You can avoid the raised exception by either:


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

  • enabling the hoNoProtocolErrorException flag in the TIdHTTP.HTTPOptions property.

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

using the overloaded version of TIdHTTP.Get() that has an AIgnoreReplies parameter, passing 404 (or any other error response code you want) in that parameter.

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

Either way, by default the response data will still be discarded. You can avoid that by enabling the recently added hoWantProtocolErrorContent flag in the TIdHTTP.HTTPOptions property (see New TIdHTTP flags and OnChunkReceived event).

这篇关于Delphi Indy 404后无响应内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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