我寫了一個小命令行程序來讀取網頁並將響應主體打印到終端。java.io.IOException:讀取Transfer-Encoding時出現EOF:分塊http響應
這非常適用於大多數網站,閱讀從這個網址一分塊響應然而,當: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)`
我硬編碼字符集以匹配該頁面的響應。我應該採取哪些措施來調試此問題?我假設這不是Java錯誤,而是Web服務器返回的問題 - 但是我已經在我的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)
感謝您的回答。這裏的問題真的是關於如何調試這個問題。雖然這個解決方案似乎沒有導致拋出相同的異常,但仍然不打印所有返回的HTML - 它的打印量與我的代碼完全相同。在Web瀏覽器或Wireshark中查看響應,您可以看到響應以關閉html標記結束。 – CodeBuddy