How Do I...? Common Tasks QuickStart Tutorial
How Do I...Make a GET request asynchronously?
This sample illustrates how to use the WebRequest and WebResponse classes to make a GET request on
a URI asynchronously.
This sample is similar to the other WebRequest samples, with the difference found in the Begin
and End async-style function calls. Put simply, instead of calling GetResponse, a call
is made to the function BeginGetResponse, with the callback function and a state object specified as parameters.
This function RespCallback, takes an object of type IASyncResult. This object contains an
object AsyncState that is the same state object passed to the call toBeginGetResponse. The state object
is casted to a custom type RequestState that can be used to get a handle to the original HttpWebRequest.
Calling the original request's EndGetResponse method will return a WebResponse object (which is cast as an HttpWebResponse
in this case.) Finally, the response is read asynchronously from the response by getting the value of the ResponseStream
property on the response object.
From here, this example is like the others. It pulls out the response from the stream and writes it
out to the console.
Example
VB ClientGETasync.exe
[This sample can be found at H:\Home\WU_000036_efe47225c86ca62f325a01d8519bc002\Webs\aspnet.sk\quickstarts\QuickStartv20\howto\samples\net\WebRequests\
To build this sample, open the SDK command prompt and navigate to the above path. Build the sample using the build tool msbuild
passing the solution file as the first parameter: msbuild mySample.sln. The compiled executable will be found in the sub directory \bin
directory.]
Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright � 2005 Microsoft Corporation. All rights reserved.
|