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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4444824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:24:37  来源:igfitidea点击:

Avoid "cannot retry due to server authentication, in streaming mode" errors, without any CXF involved

javahttp-authentication

提问by WindowsWeenie

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.

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

回答by mhaller

There is only one place in the JDK where the java.net.HttpRetryExceptionis 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())

在 JDK 中只有一个地方java.net.HttpRetryException抛出 ,就是在使用 HttpURLConnection 并尝试遵循重定向的情况下(请参阅 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.

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

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

尝试设置 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?

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