2011-07-20 12 views
2

私は、htmlページのソースを読むために、commons-httpclient 3.1を使用しています。これは、gzipとしてのコンテンツエンコーディングを持つページ以外のすべてでうまく動作しています。私は不完全なページソースを取得しています。httpClientを使用してコンテンツエンコーディングgzipでページソースを取得する方法は?

このページでは、Firefoxはコンテンツエンコーディングをgzipとして表示しています。

status code: HTTP/1.1 200 OK 
Date = Wed, 20 Jul 2011 11:29:38 GMT 
Content-Type = text/html; charset=UTF-8 
X-Powered-By = JSF/1.2 
Set-Cookie = JSESSIONID=Zqq2Tm8V74L1LJdBzB5gQzwcLQFx1khXNvcnZjNFsQtYw41J7JQH!750321853; path=/; HttpOnly 
Transfer-Encoding = chunked 
Content- length =-1 

私のコードは、応答を読むために:

HttpClient httpclient = new HttpClient(); 
      httpclient.getParams().setParameter("http.connection.timeout", 
        new Integer(50000000)); 
      httpclient.getParams().setParameter("http.socket.timeout", 
        new Integer(50000000)); 


     // Create a method instance. 
     GetMethod method = new GetMethod(url); 



     // Provide custom retry handler is necessary 
     method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
       new DefaultHttpMethodRetryHandler(3, false)); 
     BufferedReader reader = null; 
      // Execute the method. 
      int statusCode = httpclient.executeMethod(method); 

      if (statusCode != HttpStatus.SC_OK) { 
       System.err.println("Method failed: " 
         + method.getStatusLine()); 
       strHtmlContent = null; 
      } else { 


       InputStream is = method.getResponseBodyAsStream(); 
       reader = new BufferedReader(new InputStreamReader(is,"ISO8859_8")); 
       String line = null; 
       StringBuffer sbResponseBody = new StringBuffer(); 
       while ((line = reader.readLine()) != null) { 
        sbResponseBody.append(line).append("\n"); 
       } 
       strHtmlContent = sbResponseBody.toString(); 

答えて

1

アップグレードHTTPClientの4.1には、以下の

レスポンスヘッダ詳細です。シームレスに圧縮をサポートする必要があります。

+0

ご返信ありがとうございます。私はhttpclient 4.1を使ってみましたが、gzip形式の例外ではなくなっています。 – mahesh

+0

好奇心。あなたが質問に投稿したヘッダーセクションは、実際にはgzipエンコーディングを指定していません。本当に本当ですか? – pap

+0

私は以下のレスポンスを得ました:---------------------------------------- レスポンスgzipでエンコードされています ---------------------------------------- Date = Fri、22 Jul 2011 07:58:44 GMT コンテンツエンコーディング= gzip Content-Length = 5856 Content-Type = text/html; charset = UTF-8 X-Powered-By = JSF/1.2 Set-Cookie = JSDOWNID = 9D2hTptKQ1PqKsMvHcYLyFTVlQ6fTNWK3VtcQcVmBHqFb9fSbvYL!750321853;パス= /; HttpOnly コンテンツ長= -1 コンテンツエンコーディング= null 致命的なトランスポートエラー:GZIP形式ではありません java.io.IOException:GZIP形式ではありません – mahesh

1

私は次のように私はこの問題解決に被っ:

URL url = new URL("http://www.megadevs.com"); 
    HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 

    GZIPInputStream gzip = new GZIPInputStream(conn.getInputStream()); 
    int value = -1; 
    String page = ""; 

    while ((value = gzip.read()) != -1) { 
     char c = (char) value; 
     page += c; 
    } 
    gzip.close(); 

は、この情報がお役に立てば幸いです。

関連する問題