Indy和REST-我可以防止例外吗? [英] Indy and REST - Can I prevent exceptions?
问题描述
有没有一种方法可以防止Indy在任何/所有HTTP状态上引发异常?
Is there a way to prevent Indy from raising exceptions on any/all HTTP statuses?
我对IgnoreReplies数组的理解是,它将阻止这些状态从曾经回来,但这不是我想要的。我希望所有状态都返回,并且没有一个引发异常。例如,有很多REST服务都返回404,这被认为是完美的有效响应。
My understanding of the IgnoreReplies array is that it'll prevent those statuses from ever coming back, but that's not what I want. I want ALL statuses coming back, and none of them to raise an exception. There are plenty of REST services that return 404 for example, and that's considered a perfectly "valid" response.
我真的不希望我的代码的1/2在异常处理程序中,所以有没有办法让当前的Indy只返回所有内容?
I really don't want 1/2 of my code in exception handlers, so is there way to get the current Indy to just return everything?
推荐答案
对不起,但是您必须使用 IgnoreReplies
参数告诉 TIdHTTP
特定的HTTP回复代码,不会引发异常。当前,尚无办法告诉 TIdHTTP
不在一般情况下针对所有可能的答复引发异常。如果您想在不引发异常的情况下收到不成功的答复(例如404),则必须告诉 TIdHTTP
,例如:
Sorry, but you have to use the IgnoreReplies
parameter to tell TIdHTTP
which specific HTTP reply codes to not raise an exception for. There is currently no way to tell TIdHTTP
to not raise an exception for all possible replies generically. If you want to receive a non-success reply, like 404, without raising an exception then you have to tell TIdHTTP
that, eg:
IdHTTP.Get('http://host/path', [404]);
请注意,此语法仅适用于 GET
请求,而不是其他请求,例如 POST
(除非直接调用受保护的 TIdHTTP.DoRequest()
方法)。
Note that this syntax is only available for GET
requests, not other requests, like POST
(unless you call the protected TIdHTTP.DoRequest()
method directly).
Indy是专门针对异常处理而设计的。如果要有效使用Indy,则必须接受例外,而不是避免例外。将 TIdHTTP.Get()
或 TIdHTTP.Post()
包裹在小的<$ c $中,有什么麻烦? c> try / except 块?如果您不想处理所有可能的异常,则可以单独使用 except
块处理 EIdHTTPProtocolException
。
Indy is specifically designed around exception handling. You have to embrace exceptions, not avoid them, if you want to use Indy effectively. What is so troublesome about wrapping TIdHTTP.Get()
or TIdHTTP.Post()
in a small try/except
block? If you don't want to handle all possible exceptions, then you can have the except
block handle EIdHTTPProtocolException
by itself.
try
IdHTTP.Get('http://host/path');
except
on E: EIdHTTPProtocolException do
begin
if E.ErrorCode <> 404 then Raise;
end;
end;
更新:多考虑一下,可能值得添加一个新的标记 TIdHTTP.HTTPOptions
属性可为所有状态代码禁用该异常。我正在考虑。
Update: thinking about it more, it might be worth adding a new flag to the TIdHTTP.HTTPOptions
property to disable the exception for all status codes. I'm considering it.
更新:新的 hoNoProtocolErrorException
标志现在已经存在添加到 TIdHTTP.HTTPOptions
属性:
Update: a new hoNoProtocolErrorException
flag has now been added to the TIdHTTP.HTTPOptions
property:
IdHTTP.HTTPOptions := IdHTTP.HTTPOptions + [hoNoProtocolErrorException];
IdHTTP.Get('http://host/path');
// use IdHTTP.ResponseCode as needed...
更新:此外,还有一个 hoWantProtocolErrorContent
标志。当启用 hoNoProtocolErrorException
并发生HTTP错误时,如果启用 hoWantProtocolErrorContent
,则错误内容将返回给调用方(在 String
返回值中或在 AResponseContent
输出流中,具体取决于哪个版本的 Get()
/ Post()
被调用),否则内容将被丢弃。
Update: in addition, there is also a hoWantProtocolErrorContent
flag as well. When hoNoProtocolErrorException
is enabled and an HTTP error occurs, if hoWantProtocolErrorContent
is enabled then the error content will be returned to the caller (either in a String
return value or in an AResponseContent
output stream, depending on which version of Get()
/Post()
is being called), otherwise the content will be discarded.
这篇关于Indy和REST-我可以防止例外吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!