Java實現Web版RSS閱讀器(四)定製自己的Rss解析庫myrsslib4j

來源:互聯網
上載者:User

在上篇博文《Web版RSS閱讀器(三)——解析線上Rss訂閱》中,已經提到了遇到的問題,這裡再詳細說一下。

在解析rss格式的訂閱時,遇到的最主要的問題是,出現了“Server returned HTTP response code: 403 for URL: http://xxxxxx”的錯誤,百度一下就知道,這是在網站訪問中很常見的一個錯誤,伺服器理解客戶的請求,但拒絕處理它。即拒絕訪問!接著查資料,得知某些伺服器(比如CSDN部落格)拒絕java作為用戶端進行對其的訪問,所以在解析時,會拋異常。

不讓訪問怎麼辦,別怕,我們上有政策,下有對策。通過設定User-Agent來欺騙伺服器,從而訪問伺服器。

connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");    //用UA偽裝訪問連線物件

但是折騰了半天,發現只有修改rsslib4j.jar才能給連線物件設定UA。只好找源碼修改一下了,N久之後,在Google Code上獵取到一個開源的項目newrsslib4j,它是在rsslib4j的基礎上修改而來的,項目開源首頁:http://code.google.com/p/newrsslib4j/。滿懷欣喜的下載下來,結果發現,依舊有403的問題。一狠心,自己來做一個rsslib,然後就checkout了newrsslib4j的源碼,自己動手改動。

1. 修改403 forbidden問題。

修改org.gnu.stealthp.rsslib包中的RssParser類的setXmlResource()方法,給URLConnection對象,添加UA。

/**  * Set rss resource by URL  * @param ur the remote url  * @throws RSSException  */public void setXmlResource(URL ur) throws RSSException{    try{                URLConnection con = u.openConnection();                //-----------------------------      //添加時間:2013-08-14 21:00:17      //人員:@龍軒      //部落格:http://blog.csdn.net/xiaoxian8023      //新增內容:由於伺服器屏蔽java作為用戶端訪問rss,所以設定User-Agent      con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");      //-----------------------------            con.setReadTimeout(10000);   String charset = Charset.guess(ur);   is = new InputSource (new UnicodeReader(con.getInputStream(),charset));      if (con.getContentLength() == -1 && is == null){        this.fixZeroLength();      }    }catch(IOException e){      throw new RSSException("RSSParser::setXmlResource fails: "+e.getMessage());    }  }

修改org.mozilla.intl.chardet包中的Charset類的guess()方法,注釋掉原來的InputStream對象,建立URLConnection,設定User-Agent,通過URLConnection對象建立InputStream :

//judge from url    public static String guess(URL url) throws IOException {                //-----------------------------      //修改時間:2013-08-14 21:00:17      //人員:@龍軒      //部落格:http://blog.csdn.net/xiaoxian8023      //修改內容:注釋InputStream,建立URLConnection,設定User-Agent,通過URLConnection對象建立InputStream                //InputStream in = url.openStream();      URLConnection con = url.openConnection();      con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");      InputStream in = con.getInputStream();      //-----------------------------            return guess(in);  }

更多精彩內容:http://www.bianceng.cn/Programming/Java/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.