Reading a web page in Java IOException Premature EOF

后端 未结 5 626
离开以前
离开以前 2020-12-16 19:10

I am frequently getting a \'Premature EOF\' Exception when reading a web page.

The following is the StackTrace

java.io.IOException: Premature EOF
            


        
5条回答
  •  失恋的感觉
    2020-12-16 19:20

    This could be because the server is closing the connection. I have experienced the exact same issue when I had a piece of code which opened a connection, did some other processing, and only then tried to download the contents of the input stream - by the time it to the stream after spending a few seconds on other processing, the server had apparently closed the connection, resulting in IOException: Premature EOF. The solution was to be careful to always immediately handle the contents of the stream - otherwise, you are leaving an HTTP connection open and idle, and eventually the server on the other end of the line will hang up on you.

提交回复
热议问题