Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有效URL的java.io.FileNotFoundException_Java_Url_Rss_Ioexception_Rome - Fatal编程技术网

有效URL的java.io.FileNotFoundException

有效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

我使用库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.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
其他可能的情况是,他们正在对IP地址进行某种服务器端过滤,或者存在导致您的请求转到其他IP地址的DNS问题。但这两个问题似乎都与您可以在浏览器中访问提要的事实相矛盾

如果这是用户代理,请查看他们的服务条款,看看他们是否禁止使用某些类型的站点/RSS源。

我尝试了这段代码

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获取页面,结果成功了!看看我的答案。