|
1 | 1 | /**
|
2 | 2 | * File name: Begingetresponse.cs
|
3 | 3 | * This program shows how to use BeginGetResponse and EndGetResponse methods of the
|
4 |
| - * HttpWebRequest class. It also shows how to create a customized timeout. |
5 |
| - * This is important in case od asynchronous request, because the NCL classes do |
6 |
| - * not provide any off-the-shelf asynchronous timeout. |
7 |
| - * It uses an asynchronous approach to get the response for the HTTP Web Request. |
8 |
| - * The RequestState class is defined to chekc the state of the request. |
| 4 | + * HttpWebRequest class. |
| 5 | + * It uses the APM pattern to get the response for the HTTP Web Request. |
| 6 | + * The RequestState class is defined to check the state of the request. |
9 | 7 | * After a HttpWebRequest object is created, its BeginGetResponse method is used to start
|
10 | 8 | * the asynchronous response phase.
|
11 | 9 | * Finally, the EndGetResponse method is used to end the asynchronous response phase .*/
|
@@ -43,22 +41,21 @@ public static void Main()
|
43 | 41 | {
|
44 | 42 | try
|
45 | 43 | {
|
46 |
| - // Create a HttpWebrequest object to the desired URL. |
47 |
| - WebRequest httpWebRequest = WebRequest.Create("http://www.contoso.com"); |
48 |
| - httpWebRequest.Timeout = 10_000; // Set 10sec timeout. |
| 44 | + // Create a WebRequest object to the desired URL. |
| 45 | + WebRequest webRequest = WebRequest.Create("http://www.contoso.com"); |
| 46 | + webRequest.Timeout = 10_000; // Set 10sec timeout. |
49 | 47 |
|
50 | 48 | // Create an instance of the RequestState and assign the previous myHttpWebRequest
|
51 | 49 | // object to its request field.
|
52 |
| - RequestState requestState = new RequestState(httpWebRequest); |
| 50 | + RequestState requestState = new RequestState(webRequest); |
53 | 51 |
|
54 | 52 | // Start the asynchronous request.
|
55 |
| - IAsyncResult result = httpWebRequest.BeginGetResponse(new AsyncCallback(ResponseCallback), requestState); |
| 53 | + IAsyncResult result = webRequest.BeginGetResponse(new AsyncCallback(ResponseCallback), requestState); |
56 | 54 |
|
57 |
| - // The response came in the allowed time. The work processing will happen in the |
58 |
| - // callback function. |
| 55 | + // Wait for the response or for failure. The processing happens in the callback. |
59 | 56 | allDone.WaitOne();
|
60 | 57 |
|
61 |
| - // Release the HttpWebResponse resource. |
| 58 | + // Release the WebResponse resources. |
62 | 59 | requestState.Response?.Close();
|
63 | 60 | }
|
64 | 61 | catch (WebException e)
|
@@ -105,12 +102,12 @@ private static void ResponseCallback(IAsyncResult asynchronousResult)
|
105 | 102 | {
|
106 | 103 | try
|
107 | 104 | {
|
108 |
| - // State of request is asynchronous. |
| 105 | + // AsyncState is an instance of RequestState. |
109 | 106 | RequestState requestState = (RequestState)asynchronousResult.AsyncState;
|
110 | 107 | WebRequest request = requestState.Request;
|
111 | 108 | requestState.Response = request.EndGetResponse(asynchronousResult);
|
112 | 109 |
|
113 |
| - // Read the response into a Stream object. |
| 110 | + // Read the response into a Stream. |
114 | 111 | Stream responseStream = requestState.Response.GetResponseStream();
|
115 | 112 | requestState.ResponseStream = responseStream;
|
116 | 113 |
|
|
0 commit comments