public BeginGetRequestStream ( AsyncCallback callback, Object state ) : IAsyncResult | ||
callback | AsyncCallback | |
state | Object | |
return | IAsyncResult |
// create a request HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.example.com/api"); // begin async request for request stream IAsyncResult asyncResult = request.BeginGetRequestStream((result) => { // callback when stream is obtained using (Stream postDataStream = request.EndGetRequestStream(result)) { // write data to stream byte[] postData = Encoding.UTF8.GetBytes("name=value"); postDataStream.Write(postData, 0, postData.Length); } }, null); // check operation status while (!asyncResult.IsCompleted) { // do other work }
// create a request HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.example.com/api"); // begin async request for request stream IAsyncResult asyncResult = request.BeginGetRequestStream(null, null); // wait for stream to be obtained using (Stream postDataStream = request.EndGetRequestStream(asyncResult)) { // write data to stream byte[] postData = Encoding.UTF8.GetBytes("name=value"); postDataStream.Write(postData, 0, postData.Length); } // send request and get response var response = (HttpWebResponse)await request.GetResponseAsync();This example shows how to send a POST request with data using async/await. It waits for the request stream to be obtained using EndGetRequestStream, then sends the request and reads the response. The HttpWebRequest class is part of the System.Net namespace in the .NET Framework library.
public BeginGetRequestStream ( AsyncCallback callback, Object state ) : IAsyncResult | ||
callback | AsyncCallback | |
state | Object | |
return | IAsyncResult |