乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      HttpClient對(duì)URL編碼的處理方式解惑! - Java - JavaEye論壇

       汲取者 2010-04-06

      HttpClient是Apache基金下jakarta commons項(xiàng)目中的一個(gè)小項(xiàng)目,該項(xiàng)目封裝了對(duì)遠(yuǎn)程地址下載的一些功能,最新版本為3.0。該項(xiàng)目地址:http://jakarta./commons/httpclient

      最近在編寫Spider的時(shí)候就用到了HttpClient。在使用過(guò)程中發(fā)現(xiàn)一個(gè)有趣現(xiàn)象:有些URL的編碼方式是 utf-8,有些URL的編碼方式是gbk。他總能夠正確識(shí)別,但是有些他又不能識(shí)別(抓取回來(lái)后是亂碼)。調(diào)用的是:httpMethod.getResponseBodyAsString();  方法。

      在進(jìn)行進(jìn)一步分析時(shí),發(fā)現(xiàn)他對(duì)在http頭信息中有charset描述的就正確正常識(shí)別。如:

      1. HTTP/1.1 200 OK   
      2. Connection: close   
      3. Content-Type: text/html; charset=utf-8  
      4. Set-Cookie: _session_id=066875c3c0530c06c0204b96db403560; domain=; path=/   
      5. Vary: Accept-Encoding   
      6. Cache-Control: no-cache   
      7. Content-Encoding: gzip   
      8. Content-Length: 8512  
      9. Date: Fri, 16 Mar 2007 09:02:52 GMT   
      10. Server: lighttpd/1.4.13  

       而沒(méi)有charset描述信息時(shí),就會(huì)是亂碼。再查看相關(guān)文檔時(shí),可以指定URL的編碼方式。如:HttpClientParams.setContentCharset("gbk");, 指定了編碼后,就能夠正確識(shí)別對(duì)應(yīng)編碼的URL了。問(wèn)題出現(xiàn)了,因URL編碼不一樣,Spider不可能把URL的編碼方式寫死。并且只有在抓取回來(lái)后才 知道編碼是否正確。于是再仔細(xì)研究一下httpclient的源代碼,發(fā)現(xiàn)他使用編碼的順序是:http頭信息的 charset,如果頭信息中沒(méi)有charset,則查找HttpClientParams的contentCharset,如 果沒(méi)有指定編碼,則是ISO-8859-1。

      1. /**  
      2.    * Returns the character set from the Content-Type header.  
      3.    *   
      4.    * @param contentheader The content header.  
      5.    * @return String The character set.  
      6.    */  
      7.   protected String getContentCharSet(Header contentheader) {   
      8.       LOG.trace("enter getContentCharSet( Header contentheader )");   
      9.       String charset = null;   
      10.       if (contentheader != null) {   
      11.           HeaderElement values[] = contentheader.getElements();   
      12.           // I expect only one header element to be there   
      13.           // No more. no less   
      14.           if (values.length == 1) {   
      15.               NameValuePair param = values[0].getParameterByName("charset");   
      16.               if (param != null) {   
      17.                   // If I get anything "funny"    
      18.                   // UnsupportedEncondingException will result   
      19.                   charset = param.getValue();   
      20.               }   
      21.           }   
      22.       }   
      23.       if (charset == null) {   
      24.           charset = getParams().getContentCharset();   
      25.           if (LOG.isDebugEnabled()) {   
      26.               LOG.debug("Default charset used: " + charset);   
      27.           }   
      28.       }   
      29.       return charset;   
      30.   }   
      31.   
      32.   
      33.   /**    
      34.   * Returns the default charset to be used for writing content body,     
      35.   * when no charset explicitly specified.    
      36.   * @return The charset    
      37.   */     
      38.  public String getContentCharset() {      
      39.      String charset = (String) getParameter(HTTP_CONTENT_CHARSET);      
      40.      if (charset == null) {      
      41.          LOG.warn("Default content charset not configured, using ISO-8859-1");      
      42.          charset = "ISO-8859-1";      
      43.      }      
      44.      return charset;      
      45.  }     

      這個(gè)該死的iso-8859-1害了多少人啊(Tomcat對(duì)提交的數(shù)據(jù)處理默認(rèn)也是iso-8859-1)??!

      經(jīng)過(guò)仔細(xì)思考后,決定httpclient再封裝一次,思路如下:

      1. 先不設(shè)定HttpClientParams的charset;
      2. executemethod后,再檢查http頭信息中的charset是否存在; 
      3. 如果charset存在,返回httpMethod.getResponseBodyAsString();  ;
      4. 如果charset不存在,則先調(diào)用httpMethod.getResponseBodyAsString();得到 html后,再分析html head的meta的charset <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">;
      5. 從meta中分析出charset后,設(shè)置到HttpClientParams的contentCharset;
      6. 再調(diào)用httpMethod.getResponseBodyAsString(),并返回該值。

      經(jīng)過(guò)以上思路處理后,發(fā)現(xiàn)抓回來(lái)的URL再也沒(méi)有亂碼了。爽!

      以上步驟中,就是第四步稍微麻煩一些,不過(guò),也可以利用第三方的html paser工具來(lái)分析meta的charset!

      如果沒(méi)有特別注明,本Blog文章豈為原創(chuàng)。

      轉(zhuǎn)貼請(qǐng)注明出處:    http://netbus.

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多