I'm currently working on a third-party-program for a website using its public XML API. I don't want to go into deeper matters about what the program is actually doing or whatsoever because there seems to be a problem right at the beginning. The website's API expects a client to follow redirects and to set a proper user agent to verify the application itself, but the JDOM2 library, which I use for this project, doesn't seem to do any of these things. Neither the SAXBuilder (org.jdom2.input) integrated in the package nor the native HTTPURLConnection (java.net) class seem to do a proper job.
I'm very confused and don't know where to start at all. Is there any way to make the JDOM2 library follow redirects or am I just missing a simple method call?
JDOM uses the URL given to the SAXBuilder to create a URL Connection, and from that connection, it opens an input stream to read the XML content.
While I understand that the HTTP protocol has a redirect functionality, that is something that is handled by the client.... consider this:
The data that will be given to JDOM when it builds from the URL
http://stackoverflow.com/questions/24913206
will be the redirect / HTTP-301 tohttp://stackoverflow.com/questions/24913206/jdom2-follow-redirects-http-error-301
, and the HTML content that makes that human readable.Now, the URL handling API for Java just returns the input stream for JDOM. What you are suggesting is that JDOM should interpret that stream, and automatically redirect.
There are a few problems with this.
The other issue is that this should be either supported natively by Java, or actively by the application.
What are the real solutions:
HTTPUrlConnection.setFollowRedirects(true)
Don't give JDOM a raw URL to build from, but process it yourself: