2012-05-11 11 views
0

Webページを読み込んでレスポンスボディを端末に出力するための小さなコマンドラインプログラムを作成しました。このURLからチャンクの応答を読んしかしときjava.io.IOException:Transfer-Encodingを読むときの早すぎるEOF:チャンクされたhttp応答

これは、ほとんどのサイトに適しています:私は、このページからの応答を一致させる文字セットをハードコーディングしました

java.io.IOException: Premature EOF 
at sun.net.www.http.ChunkedInputStream.readAheadBlocking(ChunkedInputStream.java:538) 
at sun.net.www.http.ChunkedInputStream.readAhead(ChunkedInputStream.java:582) 
at sun.net.www.http.ChunkedInputStream.read(ChunkedInputStream.java:669) 
at java.io.FilterInputStream.read(FilterInputStream.java:116) 
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:2668) 
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264) 
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306) 
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158) 
at java.io.InputStreamReader.read(InputStreamReader.java:167) 
at java.io.Reader.read(Reader.java:123) 
at HttpPageReader.main(HttpPageReader.java:44)` 

:私は、次のバックトレースを取得http://www.pampers.co.uk/home。この問題をデバッグするにはどのような手順を取る必要がありますか?私はこれがJavaのバグではないと仮定していますが、Webサーバーから返されるものの問題です。しかし、私はWebブラウザでそのページを見て、カールを使って問題なくダウンロードしました。次のように

コードは、コピー&ペーストが簡単で、実行は、スタンドアロンする必要があります

import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import java.util.logging.Logger; 

public class HttpPageReader 
{  
private final static Logger logger = Logger.getLogger(HttpPageReader.class.getName()); 

private static final int SECOND_IN_MILLI_SECONDS = 1000; 
private static final int TIME_OUT_MILLI_SECONDS = 10 * SECOND_IN_MILLI_SECONDS; 

public static void main(String[] args) 
{ 
    if (args.length != 1) 
    { 
     logger.warning("Please provide a url to download"); 
     System.exit(1); 
    } 
    logger.info("Downloading url " + args[0] + "..."); 

    try 
    { 
     URL url = new URL(args[0]); 

     HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); 
     httpConn.setUseCaches(false); 
     httpConn.setRequestProperty("User-Agent", "My User Agent"); 
     httpConn.setRequestProperty("Accept", "*/*"); 
     httpConn.setConnectTimeout(TIME_OUT_MILLI_SECONDS); 
     httpConn.setReadTimeout(TIME_OUT_MILLI_SECONDS); 

     InputStreamReader inputStreamReader = new InputStreamReader(httpConn.getInputStream(), "utf-8");       
     char chars[] = new char[1000]; 
     int numRead = inputStreamReader.read(chars); 
     StringBuffer stringBuffer = new StringBuffer(); 
     while (numRead != -1) 
     { 
      stringBuffer.append(new String(chars, 0, numRead)); 
      numRead = inputStreamReader.read(chars); 
     } 

     logger.info("done");    
     logger.info(stringBuffer.toString()); 

    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 

} 
} 

アップデート:IOUtils.toStringを使用して(httpConn.getInputStream()、 "UTF-8")提案されているようかなり同じバックトレースを提供します。だから問題は残っていますが、この問題をどのようにデバッグするのですか?

を与える:

java.io.IOException: Premature EOF 
at sun.net.www.http.ChunkedInputStream.readAheadBlocking(ChunkedInputStream.java:538) 
at sun.net.www.http.ChunkedInputStream.readAhead(ChunkedInputStream.java:582) 
at sun.net.www.http.ChunkedInputStream.read(ChunkedInputStream.java:669) 
at java.io.FilterInputStream.read(FilterInputStream.java:116) 
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:2668) 
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264) 
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306) 
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158) 
at java.io.InputStreamReader.read(InputStreamReader.java:167) 
at java.io.Reader.read(Reader.java:123) 
at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1928) 
at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1907) 
at org.apache.commons.io.IOUtils.copy(IOUtils.java:1884) 
at org.apache.commons.io.IOUtils.copy(IOUtils.java:1834) 
at org.apache.commons.io.IOUtils.toString(IOUtils.java:705) 
at org.apache.commons.io.IOUtils.toString(IOUtils.java:730) 
at HttpPageReader.main(HttpPageReader.java:40) 

答えて

0

が.....このようにその簡単に...これを試してみて、uは本当にそれらを必要とするまでのn番のものを使用してプログラムをオーバーロードいけない覚えて......

このコードを貼り付けて試してみてください...自分のやり方で修正してみてください。

package com.my; 

import java.io.IOException; 
import java.io.InputStream; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.Scanner; 

public class TestSrc { 
    public static void main(String[] args) { 
     try { 
      URL url = new URL("http://www.pampers.co.uk/home"); 
      try { 
       InputStream i = url.openStream(); 
       Scanner scan = new Scanner(i); 

       while (scan.hasNextLine()) { 
        System.out.println(scan.nextLine()); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } 
    } 
} 
+0

回答ありがとうございます。ここでの問題は、実際にこの問題をデバッグする方法です。このソリューションは、同じ例外がスローされないように見えますが、返されたすべてのHTMLを印刷するわけではありません。つまり、コードとまったく同じ量を印刷します。 WebブラウザまたはWiresharkのいずれかの応答を見ると、閉じるHTMLタグで応答が完了したことがわかります。 – CodeBuddy

0

あなたがIOUtils.toString(inputStream)を使用してみましたか?

Apache Commons IOUtils

+0

また 'IOUtils.toString(inputStream、charset)' – phanneman

+0

ちょうどそれを試して、ほとんど同じバックトレースを取得します。質問を詳細に更新します。 – CodeBuddy

関連する問題