Android HttpUrlConnection EOFException

前端 未结 7 1614
自闭症患者
自闭症患者 2020-12-03 06:36

I would like to know if there are known issues on Android with HttpUrlConnection and POST requests. We are experiencing intermittent EOFExceptions when maki

7条回答
  •  渐次进展
    2020-12-03 07:32

    I suspect it might be the server that is at fault here, and the HttpURLConnection is not as forgiving as other implementations. That was the cause of my EOFException. I suspect in my case this would not be intermittent (fixed it before testing the N retry workaround), so the answers above relate to other issues and be a correct solution in those cases.

    My server was using python SimpleHTTPServer and I was wrongly assuming all I needed to do to indicate success was the following:

    self.send_response(200)
    

    That sends the initial response header line, a server and a date header, but leaves the stream in the state where you are able to send additional headers too. HTTP requires an additional new line after headers to indicate they are finished. It appears if this new line isn't present when you attempt to get the result body InputStream or response code etc with HttpURLConnection then it throws the EOFException (which is actually reasonable, thinking about it). Some HTTP clients did accept the short response and reported the success result code which lead to me perhaps unfairly pointing the finger at HttpURLConnection.

    I changed my server to do this instead:

    self.send_response(200)
    self.send_header("Content-Length", "0")
    self.end_headers()
    

    No more EOFException with that code.

提交回复
热议问题