在訪問 http://sz.58.com/bijibenweixiu/ 時, 不能讀出內容, HttpClient 程序最后以"socketTimeout"結束,
看到有以下信息:
ResponseHeaders - get - Date = Fri, 24 Aug 2012 07:48:35 GMT
ResponseHeaders - get - Server = Apache-Coyote/1.1
ResponseHeaders - get - Last-Modified = Fri, 24 Aug 2012 07:48:35 GMT
ResponseHeaders - get - Cache-Control = max-age=30
ResponseHeaders - get - Content-Type = text/html;charset=UTF-8
ResponseHeaders - get - Vary = Accept-Encoding
ResponseHeaders - get - Set-Cookie = A2B=I; Domain=58.com; Expires=Sat, 25-Aug-2012 07:48:35 GMT; Path=/
ResponseHeaders - get - Keep-Alive = timeout=15, max=99
ResponseHeaders - get - Connection = Keep-Alive
ResponseHeaders - get - Transfer-Encoding = chunked
查 http://hi.baidu.com/tk_ayj/blog/index/18 有以下說明, 但應如何處理呢:
23. Transfer-Encoding: WEB 服務器表明自己對本響應消息體(不是消息體里面的對象)作了怎樣的編碼,比如是否分塊(chunked)。
例如:Transfer-Encoding: chunked
24. Vary: WEB服務器用該頭部的內容告訴 Cache 服務器,在什么條件下才能用本響應所返回的對象響應后續的請求。
假如源WEB服務器在接到第一個請求消息時,其響應消息的頭部為:Content-Encoding: gzip; Vary: Content-Encoding
那么 Cache 服務器會分析后續請求消息的頭部,檢查其 Accept-Encoding,是否跟先前響應的 Vary 頭部值一致,即是否使用
相同的內容編碼方法,這樣就可以防止 Cache 服務器用自己 Cache 里面壓縮后的實體響應給不具備解壓能力的瀏覽器。
例如:Vary:Accept-Encoding
http://hi.baidu.com/andylo25/blog/item/434cc3106f78bdcda6ef3f52.html
以下是否解決問題?
注: 經測試, 按以下修改了代碼, 仍沒有解決問題.
3.4 chunked 編碼不規范的問題
有時候Web 服務器生成HTTP Response 是無法在Header 就確定消息大小的, 這時一般來說服務器將不會提供Content- Length
的頭信息, 而采用Chunked 編碼動態的提供body 內容的長度。Chunked 編碼使用若干個Chunk 串連而成, 由一個標明長度為0 的chunk 標示結束。使用十分廣泛的Tomcat Web 服務器大量采用了Chunked 編碼方案, 然而早期的Tomcat Web 服務器實現並不十分規范, 並沒有以標明長度為0 的chunk 標示內容傳輸結束。
因此HttpClient 在接收這些早期的Tomcat Web 服務器的Http 響就會導致解析錯誤。分析HttpClient 源碼發現, ChunkedInputStream 類負責在HttpClient 中解析chunked 編碼, 修改一個此類中 getChunkSizeFromInputStream(final InputStream in) 方法, 可使標准的和上述非標准的chunked 編碼均可正常解析。具體修改方法如下:
private static int getChunkSizeFromInputStream(final InputStream in)
throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// States: 0=normal, 1=\r was scanned, 2=inside quoted string, - 1=end
int state = 0;
while (state ! = - 1) {
int b = in.read();
if (b == - 1) {
return 0;//新增加語句
throw new IOException("chunked stream ended unexpectedly");//原始語句, 需將其去掉或注釋掉}