gpt4 book ai didi

java - 从 HttpClient 中的 ProtocolException 中恢复

转载 作者:行者123 更新时间:2023-11-29 08:59:47 26 4
gpt4 key购买 nike

从 HttpClient 下载时,我有时会得到这样的堆栈跟踪:

java.net.SocketTimeoutException: Read timed out, at java.net.SocketInputStream.socketRead0(Native Method), 
...
at org.apache.commons.httpclient.ContentLengthInputStream.read(ContentLengthInputStream.java:170),
[wrapped]
org.apache.commons.httpclient.ProtocolException: The server example.com failed to respond with a valid HTTP response,

我尝试使用自定义 HttpMethodRetryHandler 从这些错误中恢复,但似乎我什至没有输入 retryMethod()。如果我正确理解HttpMethodDirector类的代码,可能是因为SocketTimeoutException中的wrapped exception是ProtocolException,它继承自HttpException,因此不符合恢复条件。

 while (true) {
execCount++;
try {
...
} catch (HttpException e) {
// filter out protocol exceptions which cannot be recovered from
throw e;
} catch (IOException e) {
// test if this method should be retried

http://hc.apache.org/httpclient-3.x/exception-handling.html

ProtocolException signals a violation of the HTTP specification. It is important to note that HTTP proxies and HTTP servers can have different level of HTTP specification compliance. It may be possible to recover from some HTTP protocol exceptions by configuring HttpClient to be more lenient about non-fatal protocol violations.

我怎样才能做到这一点?是否有 API 允许这样做,或者我是否应该实现机制来重试因 HttpException 而失败的请求?

最佳答案

定义了许多http.protocol.* 配置参数here .您需要仔细查看它们中的每一个,并弄清楚您需要的最佳宽大度是多少。

关于java - 从 HttpClient 中的 ProtocolException 中恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18192528/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com