日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

HttpClient解析服务器返回的response出现乱码

發(fā)布時(shí)間:2023/12/8 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HttpClient解析服务器返回的response出现乱码 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

引用處:
【問(wèn)題解決】HttpClient解析服務(wù)器返回的response出現(xiàn)亂碼

問(wèn)題場(chǎng)景
最近在用httpClient做網(wǎng)絡(luò)爬蟲(chóng)的時(shí)候,遇到了一個(gè)不大不小的問(wèn)題,當(dāng)使用HttpGet向指定網(wǎng)址發(fā)送請(qǐng)求后,接收到的Response無(wú)法正常解析,出現(xiàn) 口口??這樣的亂碼,編碼也考慮到了中文編碼,具體代碼如下:

//處理邏輯 HttpResponse response = HttpUtils.doGet(baseUrl + title + postUrl, headers); InputStream is = getInputStreamFromResponse(response); responseText = Utils.getStringFromInputStream(in); result = EncodeUtils.unicdoeToGB2312(responseText);//上面使用到的函數(shù)public static HttpResponse doGet(String url, Map<String, String> headers) {HttpClient client = createHttpClient();HttpGet getMethod = new HttpGet(url);HttpResponse response = null;response = client.execute(getMethod);return response;}public static String getStringFromStream(InputStream in) {StringBuilder buffer = new StringBuilder();BufferedReader reader = null;reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));String line = null;while ((line = reader.readLine()) != null) {buffer.append(line + "\n");}reader.close();return buffer.toString();}

解析到的結(jié)果如下圖所示,一團(tuán)亂麻:

解決方案
上面的代碼基本邏輯是沒(méi)有問(wèn)題的,也考慮到了中文的編碼,但是卻有一個(gè)很隱秘的陷阱在里面,一般的網(wǎng)站都先將網(wǎng)頁(yè)壓縮后再傳回給瀏覽器,減少傳輸時(shí)間,如下圖所示:

而上面的處理邏輯則沒(méi)有考慮到Response的inputStream是經(jīng)過(guò)壓縮的,需要使用對(duì)應(yīng)的數(shù)據(jù)流對(duì)象處理,圖中使用的content-encoding是gzip格式,則需要使用GZIPInputStream對(duì)其進(jìn)行處理,只需要對(duì)上文中的函數(shù)public static String getStringFromStream(InputStream in)改進(jìn)即可,如下所示:

public static String getStringFromResponse(HttpResponse response) {if (response == null) {return null;}String responseText = "";InputStream in = getInputStreamFromResponse(response);Header[] headers = response.getHeaders("Content-Encoding");for(Header h : headers){if(h.getValue().indexOf("gzip") > -1){//For GZip responsetry{GZIPInputStream gzin = new GZIPInputStream(is);InputStreamReader isr = new InputStreamReader(gzin,"utf-8");responseText = Utils.getStringFromInputStreamReader(isr);}catch (IOException exception){exception.printStackTrace();}break;}}responseText = Utils.getStringFromStream(in);return responseText;}

最終得到的結(jié)果就是人能夠看懂的了:

問(wèn)題原因
在分析服務(wù)器返回response時(shí),只注意到content-type是text/html,表明我們可以用文本解析的方式來(lái)獲取response的內(nèi)容,如果content-type是excel,則表明我們可以用excel軟件來(lái)讀取內(nèi)容。content-type表明的是內(nèi)容是以何種格式組織的。但是我卻忽略了content-encoding這個(gè)字段,content-encoding字段表明的是服務(wù)器以何種方式來(lái)對(duì)傳輸?shù)膬?nèi)容進(jìn)行額外編碼,例如壓縮,如果content-encoding是gzip,則表明服務(wù)器是以Gzip格式壓縮數(shù)據(jù),而數(shù)據(jù)本身的格式可以是文本,也可以是視頻等。兩者需要區(qū)分對(duì)待。但是需要注意的是,有的服務(wù)器雖然返回的是gzip的content-encoding,而實(shí)際上卻并沒(méi)有對(duì)內(nèi)容進(jìn)行g(shù)zip編碼,所以有可能會(huì)出現(xiàn)gzip解碼失敗。

(官方原文解釋)
RFC 2616 for HTTP 1.1 specifies how web servers must indicate encoding transformations using the Content-Encoding header. Although on the surface, Content-Encoding (e.g., gzip, deflate, compress) and Content-Type(e.g., x-application/x-gzip) sound similar, they are, in fact, two distinct pieces of information. Whereas servers use Content-Type to specify the data type of the entity body, which can be useful for client applications that want to open the content with the appropriate application, Content-Encoding is used solely to specify any additional encoding done by the server before the content was transmitted to the client. Although the HTTP RFC outlines these rules pretty clearly, some web sites respond with “gzip” as the Content-Encoding even though the server has not gzipped the content.
Our testing has shown this problem to be limited to some sites that serve Unix/Linux style “tarball” files. Tarballs are gzip compressed archives files. By setting the Content-Encoding header to “gzip” on a tarball, the server is specifying that it has additionally gzipped the gzipped file. This, of course, is unlikely but not impossible or non-compliant.
Therein lies the problem. A server responding with content-encoding, such as “gzip,” is specifying the necessary mechanism that the client needs in order to decompress the content. If the server did not actually encode the content as specified, then the client’s decompression would fail.

總結(jié)

以上是生活随笔為你收集整理的HttpClient解析服务器返回的response出现乱码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 久久国产影院 | 91久久久久久久久久久 | 天天狠狠操 | 一级 黄 色 片69 | 亚洲天堂五月天 | 一级美女大片 | 欧美视频一区二区三区四区 | 免费国产一区二区 | 少妇一级淫片免费观看 | 人人爽人人爽人人爽 | 91麻豆映画传媒 | 国产校园春色 | 日韩欧美视频一区二区 | 天天做天天看 | 91成人免费观看 | 亚洲美女视频在线 | va视频在线观看 | 天天干天天日 | 成人免费看高清电影在线观看 | 一色屋免费视频 | 蜜桃在线一区二区三区 | 亚洲综合在线一区二区 | 九九热精品在线观看 | 最新一区二区三区 | 真人真事免费毛片 | 国产一级大片在线观看 | 性做久久久久 | 毛片在线免费播放 | 久久久国产精品视频 | 日本成人一区 | 91免费视频黄| 免费毛片在线播放免费 | 色臀av| 欧美激情伊人 | 女王脚交玉足榨精调教 | 亚洲理论片 | 美女国产免费 | 午夜一区在线 | 大香伊人中文字幕精品 | 在线免费观看一区二区 | 91色影院 | 亚洲视频精品在线 | 日韩三级久久 | 国产777 | 国产自偷自拍视频 | 日本乱码视频 | 老司机在线永久免费观看 | 国产成人亚洲欧洲在线 | 在线观看无遮挡 | 不卡的日韩av| 成人黄色一级片 | 免费黄色网址观看 | 日韩欧美有码 | 男男h黄动漫啪啪无遮挡软件 | 亚洲日本欧美 | 国产成人精品一区二区三区福利 | 国产乱子伦精品视频 | aaa毛片视频 | 日韩精品免费一区二区在线观看 | 欧美大片免费播放器 | 阿v天堂在线 | 一级片aaa | 亚洲一区二区三区四区av | 91偷拍精品一区二区三区 | 久久国产精品国产精品 | 日韩aⅴ在线观看 | 欧美国产大片 | 成人91免费视频 | 一级少妇精品久久久久久久 | 国产激情无码一区二区 | 欧美国产日韩视频 | 日韩av综合在线 | 99色这里只有精品 | 久久久网站 | 国产成人精品电影 | 丰满少妇被猛烈进入无码 | 视频网站在线观看18 | 后进极品美女白嫩翘臀 | 人民的名义第二部 | 第一次破处视频 | 青青草福利 | 办公室大战高跟丝袜秘书经理ol | 欧美日韩网站 | 欧美极品videos精品 | 欧美综合激情网 | 黄色av不卡 | 人操人操 | 麻豆av导航| 国内精品国产三级国产aⅴ久 | 国产日韩欧美不卡 | 美女被猛网站 | 亚洲影院一区二区三区 | xxx国产| 午夜精品久久久久久久久久久 | 尤物精品视频在线观看 | 精品日韩久久 | 亚洲国产精品麻豆 | 69国产精品| wwwwww国产|