I've googled this, as well as checked the documentation, but not been able to come up with an answer. So now I'm hoping that someone here at least has a good suggestion on where to start.
I have (abbreviated for clarity):
import java.net.URL;
import java.net.HttpURLConnection;
URL myUrlObject = new URL("some ungodly long URL");
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection connection = (HttpURLConnection) myUrlObject.openConnection();
This works fine, in principle. However, I'm a bit concerned about calling the static setFollowRedirects()
in a server application (the code runs as a servlet), and how this might interact with multiple concurrent instances, possibly of code that will want to follow redirects automatically. So, I would much rather set that on an instance object, then use that instance object to open the connection. The problem is that I cannot instantiate a HttpURLConnection object directly, and in my experimentation, it seems like no matter what I do (other than doing setFollowRedirects(false) before), openConnection() will by default follow any redirections it encounters. I'm stuck with the implementation I've got of the application on the other side of the URL (which can return HTTP 200 OK or 302 Found under normal circumstances, and either way, I need to capture the initial response).
Also important to note is that actually accessing the resource pointed to by myUrlObject can potentially be relatively 开发者_如何学Gocostly in terms of time, and when finished, I can expect my code to be executed by many users in reasonably quick succession (thus causing concurrent execution). Even for this reason alone, I don't want to use an external locking mechanism around the openConnection() call.
Any suggestions on how best to proceed?
Have you tried HttpURLConnection.setInstanceFollowRedirects()
?
HttpURLConnection connection = (HttpURLConnection) myUrlObject.openConnection();
connection.setInstanceFollowRedirects(false);
Doesn't
public void setInstanceFollowRedirects(boolean followRedirects)
do what you want?
精彩评论