Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Net;
- using System.IO;
- using System.Text;
- using System.Threading;
- class HttpWebRequestBeginGetRequest
- {
- private static ManualResetEvent allDone = new ManualResetEvent(false);
- public static void Main(string[] args)
- {
- // Create a new HttpWebRequest object.
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://192.168.0.107");
- request.ContentType = "plain/text";
- request.Method = "GET";
- // start the asynchronous operation
- request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);
- // Keep the main thread from continuing while the asynchronous
- // operation completes. A real world application
- // could do something useful such as updating its user interface.
- allDone.WaitOne();
- }
- private static void GetRequestStreamCallback(IAsyncResult asynchronousResult)
- {
- HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
- // End the operation
- Stream postStream = request.EndGetRequestStream(asynchronousResult);
- postStream.Close();
- // Start the asynchronous operation to get the response
- request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
- }
- private static void GetResponseCallback(IAsyncResult asynchronousResult)
- {
- HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
- // End the operation
- HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
- Stream streamResponse = response.GetResponseStream();
- StreamReader streamRead = new StreamReader(streamResponse);
- string responseString = streamRead.ReadToEnd();
- Console.WriteLine(responseString);
- // Close the stream object
- streamResponse.Close();
- streamRead.Close();
- // Release the HttpWebResponse
- response.Close();
- allDone.Set();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement