Java:传递用户代理变量以从网络服务器获取 RSS 数据

标签 java rss user-agent

我一直在尝试获取 馈通我一直收到 403 错误。我四处搜寻,显然是由于空 变量。

这是我迄今为止尝试过的:

try {
          url = new URL("http://*****.com/feed/");
          InputStream is = null;
          try {

                URLConnection con = url.openConnection();   
                con.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
                con.connect();
                is = con.getInputStream();
                feed = FeedParser.parse(con.getURL());
            } catch (IOException e) {
                System.out.println("error");
                try
                {
                    throw e;
                }
                catch (IOException e1)
                {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }   
            } finally {
                if( is != null)
                    try
                    {
                        is.close();
                    }
                    catch (IOException e)
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
            }

      } catch (MalformedURLException e) {
       e.printStackTrace();
      } catch (FeedIOException e) {
       e.printStackTrace();
      } catch (FeedXMLParseException e) {
       e.printStackTrace();
      } catch (UnsupportedFeedException e) {
       e.printStackTrace();
      }

      int items = feed.getItemCount();

      for (int i = 1; i <= items; i++) {

       FeedItem item = feed.getItem(i-1);

       System.out.println(i+" Title: " + item.getTitle());

      }

我无法使其正常工作,我确信我做得不正确。我用来解析 RSS 提要的库是 .

提前致谢。

最佳答案

Feed4j 不支持设置请求属性。所以你不能这样做,除非你修改 FeedParser class像这样的事情

public static Feed parse(URL url, String userAgent) throws IOException, FeedIOException, FeedXMLParseException, UnsupportedFeedException {
    try {
        URLConnection con = url.openConnection();
        if (userAgent != null) {
            con.addRequestProperty("User-Agent", userAgent);
        }
        con.connect();
        InputStream is = con.getInputStream();
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(is);
        int code = FeedRecognizer.recognizeFeed(document);
        switch (code) {
        case FeedRecognizer.RSS_1_0:
            return TypeRSS_1_0.feed(url, document);
        case FeedRecognizer.RSS_2_0:
            return TypeRSS_2_0.feed(url, document);
        case FeedRecognizer.ATOM_0_3:
            return TypeAtom_0_3.feed(url, document);
        case FeedRecognizer.ATOM_1_0:
            return TypeAtom_1_0.feed(url, document);
        default:
            throw new UnsupportedFeedException();
        }
    } catch (DocumentException e) {
        throw new FeedXMLParseException(e);
    }
}

也可在github

关于Java:传递用户代理变量以从网络服务器获取 RSS 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29188254/

相关文章:

.htaccess - Android/iOS htaccess 检测和重定向

ruby - 如何为 Ruby 的 RestClient 设置用户代理?

java - 如何强制代码样式格式化作为构建的一部分?

java - 如何使用Spring Boot Web应用程序将对象放入AWS S3存储桶

java - 为什么我会收到 java.io.InvalidClassException?

android - 在解析 xml 时,在一些以 read more 结尾的字行之后获取的不是完整数据

wordpress - WordPress 是否有 Feedburner 插件可以更改页面上的 RSS <link/> 标签

java - 如何在 java/j2ee 中以编程方式为新闻网站生成 RSS?

configuration - Nginx 代理或重写取决于用户代理

java - 使用 Maven\issues with Nexus 调试 Grails 命令 "refresh-dependencies"