有效URL的java.io.FileNotFoundException
我使用库rome.dev.java.net获取RSS 代码是有效URL的java.io.FileNotFoundException,java,url,rss,ioexception,rome,Java,Url,Rss,Ioexception,Rome,我使用库rome.dev.java.net获取RSS 代码是 URL feedUrl = new URL("http://planet.rubyonrails.ru/xml/rss"); SyndFeedInput input = new SyndFeedInput(); SyndFeed feed = input.build(new XmlReader(feedUrl)); 您可以检查该URL是否有效,页面是否显示在浏览器中 但我的申请有例外 java.io.FileNotFoundExce
URL feedUrl = new URL("http://planet.rubyonrails.ru/xml/rss");
SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = input.build(new XmlReader(feedUrl));
您可以检查该URL是否有效,页面是否显示在浏览器中
但我的申请有例外
java.io.FileNotFoundException: http://planet.rubyonrails.ru/xml/rss
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1311)
at com.sun.syndication.io.XmlReader.<init>(XmlReader.java:237)
at com.sun.syndication.io.XmlReader.<init>(XmlReader.java:213)
at rssdaemonapp.ValidatorThread.run(ValidatorThread.java:32)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
java.io.FileNotFoundException:http://planet.rubyonrails.ru/xml/rss
位于sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1311)
在com.sun.syndication.io.XmlReader上(XmlReader.java:237)
位于com.sun.syndication.io.XmlReader。(XmlReader.java:213)
运行(ValidatorThread.java:32)
位于java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
位于java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
运行(Thread.java:619)
我不使用任何代理。我在我的PC和生产服务器上遇到了这个异常,只有这个URL,其他URL才起作用。我怀疑它不喜欢Java。你需要伪造你的“用户代理”标题,不确定你的RSS库是否可以
另一个建议是您自己获取数据并将数据提供给提要阅读器。引发该异常的代码如下所示。。。假设我得到了正确的版本:
if (respCode >= 400) {
if (respCode == 404 || respCode == 410) {
throw new FileNotFoundException(url.toString());
} else {
throw new java.io.IOException(
"Server returned HTTP"
+ " response code: " + respCode
+ " for URL: " + url.toString());
}
}
换句话说,当您从Java执行GET时,您将得到404或410响应。现在,当我使用wget
实用程序执行请求时,我得到了200个响应。所以我猜问题是以下之一:
- 当他们遇到一些配置问题时,您碰巧发出了请求
- 他们已经实现了服务器对某些用户代理字符串返回404/410
HttpClient httpClient = new DefaultHttpClient();
HttpGet pageGet = new HttpGet(feedUrl.toURI());
HttpResponse response = httpClient.execute(pageGet);
SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = input.build(new XmlReader(response.getEntity().getContent()));
它起作用了!谢谢你的建议。看起来这是关于用户代理的。我尝试使用apacha HttpClient获取页面,结果成功了!看看我的答案。