有效 URL 的 java.io.FileNotFoundException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2794006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 12:54:20  来源:igfitidea点击:

java.io.FileNotFoundException for valid URL

javaurlrssioexceptionrome

提问by Alexei

I use library rome.dev.java.net to fetch RSS.

我使用库 rome.dev.java.net 来获取 RSS。

Code is

代码是

URL feedUrl = new URL("http://planet.rubyonrails.ru/xml/rss");
SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = input.build(new XmlReader(feedUrl));

You can check that http://planet.rubyonrails.ru/xml/rssis valid URL and the page is shown in browser.

您可以检查http://planet.rubyonrails.ru/xml/rss是有效的 URL 并且该页面显示在浏览器中。

But I get exception from my application

但我从我的应用程序中得到了例外

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)

I don't use any proxy. I get this exception on my PC and on the production server and only for this URL, other URLs are working.

我不使用任何代理。我在我的 PC 和生产服务器上收到此异常,并且仅对于此 URL,其他 URL 正在工作。

回答by ZZ Coder

I suspect it doesn't like Java. You need to fake your "User-Agent" header, not sure if it's doable with your RSS library.

我怀疑它不喜欢 Java。您需要伪造您的“用户代理”标头,不确定它是否适用于您的 RSS 库。

Another suggestion is that you fetch the data yourself and feed the data to the feed reader.

另一个建议是您自己获取数据并将数据提供给提要阅读器。

回答by Stephen C

The code that is throwing that exception looks like this ... assuming I've got the right version:

抛出该异常的代码如下所示......假设我有正确的版本:

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());
    }
}

In other words, when you are doing the GET from Java, you are getting a 404 or 410 response. Now when I do the request using the wgetutility, I get a 200 response. So my guess is that the problem is one of the following:

换句话说,当您从 Java 执行 GET 时,您会收到 404 或 410 响应。现在,当我使用该wget实用程序执行请求时,会收到 200 响应。所以我的猜测是问题是以下之一:

  • You happened to make the request when they were suffering from some configuration problem.
  • They have implemented their server to return 404 / 410 for certain User-Agent strings.
  • 当他们遇到一些配置问题时,您碰巧提出了请求。
  • 他们已经实现了他们的服务器来为某些用户代理字符串返回 404 / 410。

Other possibilities are that they are doing some kind of server-side filtering on IP addresses or that there is some DNS problem that is causing your requests to go to a different IP address. But both of these seem to be contradicted by the fact that you can access the feed in your browser.

其他可能性是他们正在对 IP 地址进行某种服务器端过滤,或者存在一些 DNS 问题导致您的请求转到不同的 IP 地址。但这两者似乎与您可以在浏览器中访问提要这一事实相矛盾。

If this is the User-Agent, take a look at their terms of service to see if they have a banned certain kinds of use of their site / RSS feed.

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

回答by Alexei

I tried this code

我试过这个代码

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()));

It works! Thank for your suggestions. Looks like this is about user-agent.

有用!感谢您的建议。看起来这是关于用户代理的。