For POST requests using HttpWebRequest, when I write to a request stream, at what point does the data get sent? Is it when I close the request stream or when I call GetResponse? Is the GetResponse call required?
The .net documentation does not seem to be very clear about what is really happening
Here's the code I'm curious about:
HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
request.Method = "POST";
request.ContentLength = jsonData.Length;
request.ContentType = "application/json";
Stream requestStream = request.GetRequestStream();
requestStream.Write(jsonData, 0, jsonData.Length);
requestStream.Close();
var response = request.GetResponse() as HttpWebResponse;
Thanks!
Yes, GetResponse
call is must, not only for POST request but for GET, HEAD requests too. Request / data is sent at the point when you call GetResponse
.
Start the sniffer and set breakpoint on your requestStream.Close();
and you will see that request is making when GetResponse()
called.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With