Search code examples
.nethttphttpwebresponse

HttpWebRequest.GetResponse hangs on response with no content


I've written a small "web server" that replays a pre-crafted response to everything. In this case:

HTTP/1.1 404 Not Found\r\n
\r\n

The following client code hangs until the TCP timeout kicks in:

using (WebResponse response = WebRequest.Create(url).GetResponse()) { }

I've spied on the conversation with Wireshark, and all the data gets sent and received properly. GetResponse keeps waiting after receiving the response quoted above. But it shouldn't - it's a valid HTTP response, right? What's wrong?

When I add any content to the response, everything works as expected.


Solution

  • I suspect the client is getting confused by the lack of a Content-Length header - it doesn't know how much content it might receive, so it's waiting for the server to close the connection, which also isn't happening.

    Try adding

    Content-Length: 0
    

    to the response headers.