JDOM2 - 关注重定向(HTTP错误301) [英] JDOM2 - Follow Redirects (HTTP Error 301)
问题描述
我目前正在使用其公共XML API为网站开发第三方程序。我不想深入讨论该计划实际上在做什么或者无论如何,因为一开始似乎存在问题。该网站的API期望客户端遵循重定向并设置适当的用户代理来验证应用程序本身,但我用于此项目的JDOM2库似乎没有做任何这些事情。包中集成的SAXBuilder(org.jdom2.input)和本地HTTPURLConnection(java.net)类似乎都没有正常工作。
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.
我很困惑,不知道从哪里开始。有没有办法让JDOM2库遵循重定向或我只是缺少一个简单的方法调用?
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使用URL给SAXBuilder创建一个URL连接,并从该连接打开一个输入流来读取XML内容。
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.
虽然我知道HTTP协议有一个重定向功能,这是由客户端处理的......考虑一下:
While I understand that the HTTP protocol has a redirect functionality, that is something that is handled by the client.... consider this:
# curl -i 'http://stackoverflow.com/questions/24913206'
HTTP/1.1 301 Moved Permanently
Cache-Control: public, no-cache="Set-Cookie", max-age=60
Content-Type: text/html; charset=utf-8
Expires: Wed, 23 Jul 2014 18:44:06 GMT
Last-Modified: Wed, 23 Jul 2014 18:43:06 GMT
Location: /questions/24913206/jdom2-follow-redirects-http-error-301
Vary: *
X-Frame-Options: SAMEORIGIN
Set-Cookie: prov=xxxx.yyyy.zzzz; domain=.stackoverflow.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly
Date: Wed, 23 Jul 2014 18:43:05 GMT
Content-Length: 174
<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="/questions/24913206/jdom2-follow-redirects-http-error-301">here</a>.</h2>
</body></html>
从URL http构建时将提供给JDOM的数据://stackoverflow.com/questions/24913206
将重定向/ HTTP-301改为 http://stackoverflow.com/questions/24913206/jdom2-follow-redirects- http-error-301
,以及使人类可读的HTML内容。
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 to http://stackoverflow.com/questions/24913206/jdom2-follow-redirects-http-error-301
, and the HTML content that makes that human readable.
现在,Java的URL处理API只返回输入JDOM的流。你的建议是JDOM应该解释那个流,并自动重定向。
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.
这有一些问题。
- JDOM甚至不知道它是HTTP URL。它通常是文件名或FTP URL等。
- 如果您不想关注重定向怎么办?
- 等
另一个问题是,这应该由Java本地支持,或者由应用程序主动支持。
The other issue is that this should be either supported natively by Java, or actively by the application.
真正的解决方案是什么:
What are the real solutions:
- 告诉应用程序中的所有HTTP请求使用以下方式跟踪重定向:
HTTPUrlConnection.setFollowRedirects(true )
-
不要为JDOM提供构建的原始URL,而是自己处理:
- Tell all HTTP requests in your application to follow redirects using:
HTTPUrlConnection.setFollowRedirects(true)
Don't give JDOM a raw URL to build from, but process it yourself:
URL httpurl = new URL(.....);
HTTPURLConnection conn = (HTTPUrlConnection)httpurl.openConnection();
conn.setInstanceFollowRedirects(true);
conn.connect();
Document doc = saxBuilder.build(conn.getInputStream());
这篇关于JDOM2 - 关注重定向(HTTP错误301)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!