導航:首頁 > 源碼編譯 > httpclient獲取網頁源碼

httpclient獲取網頁源碼

發布時間:2023-09-13 21:50:39

『壹』 java 獲取網頁源代碼---有效防止亂碼

前段時間做過這類功能,如何有效防止亂碼,我們必須先知道一個網頁的編碼方式,是utf-8,還是gbk。

1.HttpURLConnection.getContentType();直接讀取,效率高,但有很多時候讀不到。只是text/html就完事了,沒有charset.

2.使用第三方的HttpClient,執行效率較高。但讀取網頁頭header也只適用部分站,很多網站服務段不設置,結果就讀成了null.

3.最沒有效率的判斷方法就是使用inputStreamReader先把正頁的html源碼讀取出來,之後截取charset後面編碼。得到編碼之後重新再讀取一遍。但是效率很低。

做個總結:

/**
* 取得頁面編碼
*
* @param url
* @return
*/
public String getCharset(String url) throws Exception {
// log.info("進入讀頁面的關鍵詞:" + keyword);
String charset = "";
int c;
HttpURLConnection httpurlcon = null;
// log.info("url:"+url);
// log.info("charset:"+charset);

log.info("url:" + url);

URL httpurl = new URL(url);
// System.out.println(url+str);

httpurlcon = (HttpURLConnection) httpurl.openConnection();
// google需要身份
httpurlcon.setRequestProperty("User-agent", "Mozilla/4.0");
charset = httpurlcon.getContentType();
log.info("charset1:" + charset);
// 如果可以找到
if (charset.indexOf("charset=") != -1)
charset = charset.substring(charset.indexOf("charset=")
+ "charset=".length(), charset.length());
// 否則讀取response.Header頭
else {
charset = this.getContentCharset();
log.info("charset2:" + charset);
}
// 如果charset還是為空,那麼直接讀網頁來截取
if (charset == null) {
charset = this.readPageCharset(url);
log.info("charset31:" + charset);

}

return charset;
}

閱讀全文

與httpclient獲取網頁源碼相關的資料

熱點內容
程序員那麼愛心 瀏覽:300
字元a經過md5加密 瀏覽:413
綠色的小蝴蝶是個什麼app 瀏覽:11
python編程輸入數字輸出年月日英文 瀏覽:622
程序員槍手 瀏覽:743
gm28伺服器怎麼設置 瀏覽:538
餓了么網站源碼 瀏覽:328
天選程序員真的有用嗎 瀏覽:914
微信登錄伺服器什麼意思 瀏覽:349
溯源碼粘碎圖 瀏覽:133
qq綁定郵箱pop伺服器地址 瀏覽:721
卡羅拉空調壓縮機價格 瀏覽:890
華潤it程序員 瀏覽:552
51單片機c語言秒錶 瀏覽:271
php一周前的時間 瀏覽:851
windows文件夾輸入列表 瀏覽:918
php做網頁聊天系統 瀏覽:888
滑鼠光學感測器讀取單片機 瀏覽:165
食品批號的app是什麼 瀏覽:194
文件夾復原快捷鍵 瀏覽:391