代码之家  ›  专栏  ›  技术社区  ›  Kalinia

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

  •  1
  • Kalinia  · 技术社区  · 11 年前

    我一直在尝试获取 馈通,馈通 我一直收到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提要的库是 .

    提前感谢。

    1 回复  |  直到 11 年前
        1
  •  0
  •   janih    11 年前

    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