Code exemple:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://some.existing.url"); request.Method = "POST"; request.ContentType = "text/xml"; Byte[] documentBytes = GetDocumentBytes (); using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(documentBytes, 0, documentBytes.Length); requestStream.Flush(); requestStream.Close(); }
When I do request.GetRequestStream ()
, there's nothing to send in the request. From the name of the method, and the intellisense it shows ("Get System.IO.Stream to use to write request data"), nothing indicates that this line of code will connect to the distant server.
But it seems it does...
Can anyone explain to me what HttpWebRequest.GetRequestStream ()
exactly does ?
Thanks for your enlightenments.
The HttpWebRequest class provides support for the properties and methods defined in WebRequest and for additional properties and methods that enable the user to interact directly with servers using HTTP.
This class contains support for HTTP-specific uses of the properties and methods of the WebResponse class. The HttpWebResponse class is used to build HTTP stand-alone client applications that send HTTP requests and receive HTTP responses.
Getting the request stream does not trigger the post, but closing the stream does. Post data is sent to the server in the following way:
The act of flushing and closing the stream is the final step, and once the input stream is closed (i.e. the client has sent what it needs to the server), then the server can return a response.
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