在图片上,您可以看到我的RSS(http://www.fyens.dk/rss/sport)的原始网址已更改为转到移动网站(http://mobil.fyens.dk/modules/mobile)。我怎么能避免这个?我无法从移动网站上阅读RSS源。
try {
URL url = new URL("http://www.fyens.dk/rss/sport");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream is = conn.getInputStream();
DocumentBuilderFactory dbf = DocumentBuilderFactory
.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(is);
Element element = document.getDocumentElement();
NodeList nodeList = element.getElementsByTagName("item");
if (nodeList.getLength() > 0) {
for (int i = 0; i < nodeList.getLength(); i++) {
程序跳转到
catch (Exception e) {
e.printStackTrace();
}
到达行
Document document = db.parse(is);
答案 0 :(得分:1)
该网站最有可能基于用户代理进行重定向。您想使用不同的用户代理字符串来欺骗网站。
尝试做:
conn.setRequestProperty("User-Agent", "The user agent you want to use");
您将需要使用与桌面浏览器对应的用户代理字符串。请查看此列表:http://www.useragentstring.com/pages/Chrome/