<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    stone2083

    HtmlParser疑似Bug

    最近的項目中,使用到了HtmlParser(1.5版本).在使用過程中(如訪問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)
    檢查代碼,發現:
    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     }
    一旦發現name值為“Discard”,則拋異常。

    而在org.htmlparser.http.ConnectionManager.parseCookies (URLConnection connection) 解析cookie的代碼中,見代碼片段
    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); //出問題的地方
                                                    cookies.addElement (cookie);
                                                }
    沒有對Discard做特殊處理。
    無奈之下,覆寫了此方法,加上對Discard的處理--直接continue :)

    今天在寫blog的時候,拿了1.6的代碼測試,發現沒有問題,分析代碼后發現
    1. ConnectionManager parserCookie之前,加了條件判斷
    if (getCookieProcessingEnabled ())
      parseCookies (ret);
    默認情況下,條件為false
    2. parserCookie的時候,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 }
    雖然解決了問題,但是明顯還沒有意識到Discard的問題。

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

    關于discard的解釋,見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 閱讀(1868) 評論(2)  編輯  收藏 所屬分類: java

    Feedback

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

    應該使用jsoup解析html,htmlparse解析html速度是比jsoup快,不過很多地方要自己寫代碼處理。除了你說的這個異常外,還有很多地方會出現異常,需要你自己寫代碼處理。易用性和解析結果的準確性和jsoup相比差遠了。  回復  更多評論   

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

    @penngo
    收到,有空的時候,我去學習下jsoup。  回復  更多評論   

    主站蜘蛛池模板: 色婷婷7777免费视频在线观看| 国产成人久久AV免费| 成人免费毛片观看| 亚洲免费在线观看视频| 91精品导航在线网址免费| 亚洲AV日韩AV永久无码下载| 99在线免费观看视频| 亚洲欧洲日本精品| 国产片AV片永久免费观看| 亚洲精品伊人久久久久| 毛片网站免费在线观看| 中文字幕精品三区无码亚洲| 精品国产免费观看久久久| 国产精品亚洲精品久久精品| 国产免费啪嗒啪嗒视频看看| igao激情在线视频免费| 亚洲av无码一区二区乱子伦as | 91手机看片国产永久免费| 亚洲综合日韩中文字幕v在线| 99热这里只有精品免费播放| 亚洲精品动漫在线| 国内外成人免费视频| 无套内射无矿码免费看黄| 亚洲人成人无码网www电影首页| 99久久99热精品免费观看国产| 亚洲国产最大av| 俄罗斯极品美女毛片免费播放| 成人片黄网站色大片免费观看cn| 亚洲国产另类久久久精品黑人| 最近中文字幕无免费| 亚洲另类自拍丝袜第五页| 亚洲日本一区二区三区在线不卡| 无码囯产精品一区二区免费| 亚洲日本久久一区二区va| 亚洲国产综合精品一区在线播放| 免费国产99久久久香蕉| 亚洲欧美日韩中文高清www777| 亚洲中文字幕成人在线| 亚洲精品在线免费观看| 免费一级毛suv好看的国产网站 | 国产偷国产偷亚洲清高APP|