java.io.IOException:读取Transfer-Encoding时的过早EOF:chunked http响应

时间:2012-05-11 18:29:45

标签: java http http-headers

我写了一个小命令行程序来读取网页并将响应主体打印到终端。

这适用于大多数网站,但是当从这个网址读取分块响应时:http://www.pampers.co.uk/home我得到以下回溯:

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)`

我已经硬编码了charset以匹配此页面的响应。我应该采取哪些步骤来调试此问题?我假设这不是Java错误,而是Web服务器返回的问题 - 但是我已经在我的网络浏览器中查看了该页面,并且还使用curl下载了它,两者都没有问题。

代码如下,应该易于复制和粘贴并独立运行:

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)

2 个答案:

答案 0 :(得分:0)

尝试这个.....这样它更容易......并且记住不要用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();
        }
    }
}

答案 1 :(得分:0)

您是否尝试过使用IOUtils.toString(inputStream)

Apache Commons IOUtils