避免“在流模式下由于服务器身份验证而无法重试”错误,不涉及任何 CXF

发布于 2024-10-07 11:25:47 字数 623 浏览 4 评论 0原文

我已经在很多地方搜索过处理过这个 HttpRetryException 问题的其他人,但是我发现的所有人都在一些名为 CXF 的 apache 服务中遇到了这个问题,而我没有使用该服务。我使用的是java.net.HttpURLConnection。我创建一个连接,使用 setRequestProperty 作为“授权”,获取输出流,写入一堆字节,然后尝试读取回复输入流。大多数情况下这是有效的,但有时我会遇到上面提到的异常。我无法避免流式传输,因为有时我需要编写比内存中可以存储的文件更大的文件,无论如何,我发现的搜索结果表明这不是真正的问题。他们通常会提供类似于 BindingProvider.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "username"); 的解决方案。 bindingProvider.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "密码"); 我没有使用 cxf 或 HttpURLConnection 上的任何其他包装器,并且没有可供引用的服务或绑定提供程序。使用 setRequestProperty 设置的用户名和密码在大多数情况下都可以很好地进行身份验证。我希望我能说出要可靠地复制错误需要哪些特定的先决条件,但到目前为止它已经被击中或错过了。

I've searched a number of places for other people who have dealt with this HttpRetryException problem, but all the ones I found encountered it with some apache service called CXF, which I am not using. What I am using is the java.net.HttpURLConnection. I create a connection, use setRequestProperty for "Authorization", get an output stream, write a bunch of bytes and then try to read the reply input stream. Most of the time this works, but sometimes I get the exception mentioned above. I can't avoid streaming because sometimes I need to write larger files than can be stored in memory, and at any rate most of the results I found searching indicate that isn't the real issue. They usually give solutions along the lines of bindingProvider.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "username");
bindingProvider.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "password");

I am not using cxf or any other wrapper over HttpURLConnection, and have no service or binding provider to reference. And the username and password set with setRequestProperty works fine for authentication most of the time. I wish I could say what particular preconditions are necessary to reliably replicate the error, but so far it has been hit or miss.

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

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

发布评论

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

评论(1

一笔一画续写前缘 2024-10-14 11:25:47

JDK 中只有一处会抛出 java.net.HttpRetryException ,即使用 HttpURLConnection 并尝试遵循重定向的情况(请参阅 sun.net.www .protocol.http.HttpURLConnection.followRedirect())

所以基本上,服务器响应了 HTTP 状态代码 3xx(304 和 306 除外),现在尝试跟随 Location: 给出的位置HTTP 标头。但由于启用了流式传输,因此它无法遵循重定向。

尝试设置 java.net.HttpURLConnection.setInstanceFollowRedirects(false)

虽然我宁愿检查服务器为什么首先发送 HTTP 重定向。根据您的描述,我了解到您正在使用 HTTP POST 执行较大的上传,对吗?

There is only one place in the JDK where the java.net.HttpRetryException is thrown, and that is in the case when a HttpURLConnection is used and it tries to follow a redirect (see sun.net.www.protocol.http.HttpURLConnection.followRedirect())

So basically, the server responded with a HTTP Status code of 3xx (except 304 and 306) and now tries to follow to the location given by the Location: HTTP header. But since streaming is enabled, it cannot follow the redirect.

Try setting java.net.HttpURLConnection.setInstanceFollowRedirects(false)

Although i'd rather check why the server is sending a HTTP redirect in the first place. From your description, I understand that you're performing a larger upload using HTTP POST, is that correct?

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