stone2083

          HtmlParser疑似Bug

          最近的項(xiàng)目中,使用到了HtmlParser(1.5版本).在使用過(guò)程中(如訪問(wèn)url為:http://athena2002.vip.china.alibaba.com/ ),遇到了異常:
          Exception in thread "main" java.lang.IllegalArgumentException: invalid cookie name: Discard
              at org.htmlparser.http.Cookie.
          <init>(Cookie.java:136)
              at org.htmlparser.http.ConnectionManager.parseCookies(ConnectionManager.java:
          1126)
              at org.htmlparser.http.ConnectionManager.openConnection(ConnectionManager.java:
          621)
              at org.htmlparser.http.ConnectionManager.openConnection(ConnectionManager.java:
          792)
              at org.htmlparser.Parser.
          <init>(Parser.java:251)
              at org.htmlparser.Parser.
          <init>(Parser.java:261)
          檢查代碼,發(fā)現(xiàn):
          org.htmlparser.http.Cookie
           1 public Cookie (String name, String value)
           2     {
           3         if (!isToken (name) || name.equalsIgnoreCase ("Comment"// rfc2019
           4                 || name.equalsIgnoreCase ("Discard"// 2019++
           5                 || name.equalsIgnoreCase ("Domain")
           6                 || name.equalsIgnoreCase ("Expires"// (old cookies)
           7                 || name.equalsIgnoreCase ("Max-Age"// rfc2019
           8                 || name.equalsIgnoreCase ("Path")
           9                 || name.equalsIgnoreCase ("Secure")
          10                 || name.equalsIgnoreCase ("Version"))
          11             throw new IllegalArgumentException ("invalid cookie name: " + name);
          12         mName = name;
          13         mValue = value;
          14         mComment = null;
          15         mDomain = null;
          16         mExpiry = null// not persisted
          17         mPath = "/";
          18         mSecure = false;
          19         mVersion = 0;
          20     }
          一旦發(fā)現(xiàn)name值為“Discard”,則拋異常。

          而在org.htmlparser.http.ConnectionManager.parseCookies (URLConnection connection) 解析cookie的代碼中,見(jiàn)代碼片段
          if (key.equals ("domain"))
                                      cookie.setDomain (value);
                                  
          else
                                      
          if (key.equals ("path"))
                                          cookie.setPath (value);
                                      
          else
                                          
          if (key.equals ("secure"))
                                              cookie.setSecure (
          true);
                                          
          else
                                              
          if (key.equals ("comment"))
                                                  cookie.setComment (value);
                                              
          else
                                                  
          if (key.equals ("version"))
                                                      cookie.setVersion (Integer.parseInt (value));
                                                  
          else
                                                      
          if (key.equals ("max-age"))
                                                      {
                                                          Date date 
          = new Date ();
                                                          
          long then = date.getTime () + Integer.parseInt (value) * 1000;
                                                          date.setTime (then);
                                                          cookie.setExpiryDate (date);
                                                      }
                                                      
          else
                                                      {   
          // error,? unknown attribute,
                                                          
          // maybe just another cookie not separated by a comma
                                                          cookie = new Cookie (name, value); //出問(wèn)題的地方
                                                          cookies.addElement (cookie);
                                                      }
          沒(méi)有對(duì)Discard做特殊處理。
          無(wú)奈之下,覆寫(xiě)了此方法,加上對(duì)Discard的處理--直接continue :)

          今天在寫(xiě)blog的時(shí)候,拿了1.6的代碼測(cè)試,發(fā)現(xiàn)沒(méi)有問(wèn)題,分析代碼后發(fā)現(xiàn)
          1. ConnectionManager parserCookie之前,加了條件判斷
          if (getCookieProcessingEnabled ())
            parseCookies (ret);
          默認(rèn)情況下,條件為false
          2. parserCookie的時(shí)候,catch了異常
           1 // error,? unknown attribute,
           2 // maybe just another cookie
           3 // not separated by a comma
           4 try
           5 {
           6     cookie = new Cookie (name,
           7         value);
           8     cookies.addElement (cookie);
           9 }
          10 catch (IllegalArgumentException iae)
          11 {
          12     // should print a warning
          13     // for now just bail
          14     break;
          15 }
          雖然解決了問(wèn)題,但是明顯還沒(méi)有意識(shí)到Discard的問(wèn)題。

          從我的理解看,最合理的解決方案是:
          1. org.htmlparser.http.Cookie中添加 boolean discard方法
          2. org.htmlparser.http.ConnectionManager parserCookies()方法,對(duì)Discard做處理,如有值,則設(shè)置cookie.discard=true

          關(guān)于discard的解釋,見(jiàn)http://www.faqs.org/rfcs/rfc2965.html
          Discard
          OPTIONAL. The Discard attribute instructs the user agent to
          discard the cookie unconditionally when the user agent terminates

          posted on 2011-04-08 20:50 stone2083 閱讀(1873) 評(píng)論(2)  編輯  收藏 所屬分類: java

          Feedback

          # re: HtmlParser疑似Bug 2011-04-09 14:57 penngo

          應(yīng)該使用jsoup解析html,htmlparse解析html速度是比jsoup快,不過(guò)很多地方要自己寫(xiě)代碼處理。除了你說(shuō)的這個(gè)異常外,還有很多地方會(huì)出現(xiàn)異常,需要你自己寫(xiě)代碼處理。易用性和解析結(jié)果的準(zhǔn)確性和jsoup相比差遠(yuǎn)了。  回復(fù)  更多評(píng)論   

          # re: HtmlParser疑似Bug 2011-04-09 19:31 stone2083

          @penngo
          收到,有空的時(shí)候,我去學(xué)習(xí)下jsoup。  回復(fù)  更多評(píng)論   

          主站蜘蛛池模板: 张北县| 拉萨市| 蒙城县| 银川市| 攀枝花市| 长兴县| 志丹县| 武威市| 谷城县| 上思县| 大关县| 凤翔县| 新巴尔虎左旗| 和林格尔县| 佛学| 高台县| 同江市| 微博| 新营市| 富民县| 宁强县| 莒南县| 丰镇市| 江孜县| 宜丰县| 明水县| 金阳县| 柘城县| 南投县| 来宾市| 桓仁| 扶沟县| 松潘县| 平谷区| 玉环县| 揭阳市| 宁南县| 岐山县| 朝阳区| 屯门区| 怀柔区|