Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is HttpWebRequest.GetResponse required to complete a POST?

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!

like image 240
rysama Avatar asked Sep 25 '12 05:09

rysama


2 Answers

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.

like image 155
Coder Avatar answered Sep 18 '22 02:09

Coder


Start the sniffer and set breakpoint on your requestStream.Close(); and you will see that request is making when GetResponse() called.

like image 28
tsionyx Avatar answered Sep 18 '22 02:09

tsionyx