Many times (not always) my application calls HttpWebRequest.GetRequestStream() (please note: getRequest, not getResponse!) but the call doesn’t return.
The program “freeze” on that line. If I pause it in VS I can see that it’s waiting for the return from the call.
How is this possible? Where is the problem ?
This is the code
Using request As System.Net.HttpWebRequest = DirectCast(System.Net.HttpWebRequest.Create(cApiUrl), System.Net.HttpWebRequest)
request.AllowWriteStreamBuffering = False
request.Method = "POST"
request.KeepAlive = False
request.ContentLength = postBytes.Length
request.ContentType = "application/json; charset=utf-8"
request.Accept = "application/json;"
request.Timeout = 60000
Try
Using postDataStream As System.IO.Stream = request.GetRequestStream()
Have you tried to hit start in VS again?
I mean: hit pause, than start.
It might run again.
I have this problem often lately.
If this happens to you as well, and your code is short and simple, please provide it to GHI so they can investigate this problem. My project is quite to complex to send it over.
Are you sure the server is responding correctly? When using the HttpWebRequest class it opens up a socket. When the socket tries to connect to an endpoint it does so by a blocking call. If the socket can’t establish a connection it will hang due to a bug no matter what timeout you have set. This is also true for when the server response takes too long.
I solved this by only using socket communication and implementing a custom timeout for the socket.connect method.
A windows firewall blocked the non standard port on the development server, so no connection was possible.
I didn’t test this because I didn’t know the socket is opened before the GetResponse().
However this behavior is really dangerous: a board in production can be blocked and freezed just because a tcp connection can’t be estabilished!