Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 708
WindowsWeenie Avatar asked Dec 14 '10 22:12

WindowsWeenie


1 Answers

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?

like image 192
mhaller Avatar answered Oct 16 '22 04:10

mhaller