如何在 Apache HttpClient.execute(HttpMethod) 中处理 IOException 和 HttpException

发布于 2024-07-29 23:02:46 字数 684 浏览 1 评论 0原文

文档(复制如下)没有详细说明 IOException 和 HttpException 在其上下文中的含义。

我的问题是:在 HttpClient.execute(HttpMethod) 方法中发生 IOException 后,我是否必须丢弃该对象并创建一个新的 HttpClient 才能再次执行方法? 或者在其中一些异常发生后仍然可以使用(HttpClient 对象)执行方法?

executeMethod

public int executeMethod(HttpMethod method)
                  throws IOException,
                         HttpException
Executes the given HTTP method.
Parameters:
method - the HTTP method to execute.
Returns:
the method's response code
Throws:
IOException - If an I/O (transport) error occurs. Some transport exceptions can be recovered from.
HttpException - If a protocol exception occurs. Usually protocol exceptions cannot be recovered from.

The documentation (copied below) doesn't say a lot about what IOException and HttpException means in its context.

My question is: after an IOException occurs in a HttpClient.execute(HttpMethod) method, do I have to discard the object and create a new HttpClient in order to execute methods again? Or is it still usable (the HttpClient object) to execute methods after some of these exceptions occur?

executeMethod

public int executeMethod(HttpMethod method)
                  throws IOException,
                         HttpException
Executes the given HTTP method.
Parameters:
method - the HTTP method to execute.
Returns:
the method's response code
Throws:
IOException - If an I/O (transport) error occurs. Some transport exceptions can be recovered from.
HttpException - If a protocol exception occurs. Usually protocol exceptions cannot be recovered from.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡忘如思 2024-08-05 23:02:46

您可以根据需要多次重复使用 HttpClient 对象。 我正在使用 3.0 版本,无法确定这是否在更新的版本中发生了变化。

关于异常,注释更多地针对 HttpMethod 而不是客户端。

“可以恢复某些传输异常”的含义是,您可以重新发送请求并获得有效的响应。 当您无法发送请求或未收到服务器的响应时(例如,服务器已关闭,或者无法处理正在接收的所有请求),IOExceptions 往往会发生。

“通常无法恢复协议异常”的含义是,您向其发出 HTTP 请求的服务器未发送符合 HTTP 协议的响应。 通常,重新发送请求不会产生不同的结果。

我意识到这个答案可能有点晚了,对您有用,但未来的读者仍然可能受益。

这是有关 HttpClient 异常处理的有用指南(无论如何都是旧版本) 。 它可能会移动,因此这里有一个有用的搜索 用于查找页面。

You can reuse the HttpClient object as many times as you want. I'm working with version 3.0, and cannot speak to whether or not this has changed in more recent versions.

In regard to the exceptions, the comments are directed more at the HttpMethod than the client.

What it means by, "Some transport exceptions can be recovered from", is that you may be able to resend the request and get a valid response. IOExceptions tend to happen when you can't send a request or do not receive a response from the server (e.g. the server is down, or it cannot handle all the requests it is receiving).

What it means by, "Usually protocol exceptions cannot be recovered from", is that the server you're making an HTTP request to is not sending a response that meets the HTTP protocol(s). Usually, resending the request will not yield a different result.

I realize this answer is probably a little late to be useful to you, but future readers may still benefit.

This is a useful guide on exception handling with HttpClient (legacy versions anyway). It may move around, so here's a useful search for finding the page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文