How Do I...Make a GET request through a proxy?
This sample illustrates how to use the WebRequest and WebResponse classes
to make a GET request on a URI through a proxy.
This sample is very similar to the clientGET sample. The difference
is in the creation and attachment of the proxy object to specify a specific proxy to use.
By default, the proxy settings will be automatically detected for the current user account. To
bypass these settings, a WebProxy is created and (in the constructor) set to the name and
port of the proxy server. This object's BypassProxyOnLocal property is set to true,
which means to bypass the proxy for local LAN servers. The final step is to set this proxy as
the global proxy for all HTTP requests with the line HttpWebRequest.DefaultWebProxy = webProxy;.
To specify that no proxy should be used, you can specify null for the proxy
The function GetPage is where the specific details of making the request can be
found. The rest of the code in this sample is for taking command-line parameters
as well as displaying help usage for the parameters. This sample program is a
command-line utility that runs at the command prompt.
The GetPage function takes a string parameter, which is the URL (or URI)
of the web page to request. This URI is then included as a parameter in a call to
WebRequest.Create which creates a WebRequest object.
The GetResponse function of the WebRequest object is then used to get a
WebResponse object. This object can be used to get the status code of the
response, as well as the actual response stream (a web page, for instance).
Writing out the stream can take several different forms. This example uses a StreamReader
instance to read 256 characters into a character array. It then creates a String from the
character array and writes out the String using Console.WriteLine.
Example
C# ClientGETwithProxy.exe
[This sample can be found at M:\web\users\Sites\AspnetQuickStart\v2.0\QuickStart\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.
|