Export (0) Print
Expand All

HttpWebRequest.Proxy Property

Gets or sets proxy information for the request.

[Visual Basic]
Overrides Public Property Proxy As IWebProxy
[C#]
public override IWebProxy Proxy {get; set;}
[C++]
public: __property IWebProxy* get_Proxy();
public: __property void set_Proxy(IWebProxy*);
[JScript]
public override function get Proxy() : IWebProxy;
public override function set Proxy(IWebProxy);

Property Value

The WebProxy instance to use to proxy the request. The default value is set by calling GlobalProxySelection.Select.

Exceptions

Exception Type Condition
ArgumentNullException Proxy is set to a null reference (Nothing in Visual Basic).
InvalidOperationException The request has been started by calling GetRequestStream, BeginGetRequestStream, GetResponse, BeginGetResponse.
SecurityException The caller does not have permission for the requested operation.

Remarks

The Proxy property identifies the WebProxy instance to use to process requests to Internet resources. To specify that no proxy should be used, set the Proxy property to the proxy instance returned by the GlobalProxySelection.GetEmptyWebProxy method.

Changing the Proxy property after the request has been started by calling GetRequestStream, BeginGetRequestStream, GetResponse, or BeginGetResponse method will throw an InvalidOperationException.

Example

[Visual Basic, C#, C++] The following example uses the Proxy method to get the proxy information for the request.

[Visual Basic] 
' Create a new request to the mentioned URL.                
Dim myWebRequest As HttpWebRequest = CType(WebRequest.Create("http://www.microsoft.com"), HttpWebRequest)
Dim myProxy As New WebProxy()
' Obtain the 'Proxy' of the  Default browser.  
myProxy = CType(myWebRequest.Proxy, WebProxy)
' Print the Proxy Url to the console.
Console.WriteLine(ControlChars.Cr + "The actual default Proxy settings are {0}", myProxy.Address)
Try
    Console.WriteLine(ControlChars.Cr + "Please enter the new Proxy Address that is to be set ")
    Console.WriteLine("(Example:http://myproxy.com:port)")
    Dim proxyAddress As String
    proxyAddress = Console.ReadLine()
    If proxyAddress.Length = 0 Then
        myWebRequest.Proxy = myProxy
    Else
        Console.WriteLine(ControlChars.Cr + "Please enter the Credentials")
        Console.WriteLine("Username:")
        Dim username As String
        username = Console.ReadLine()
        Console.WriteLine(ControlChars.Cr + "Password:")
        Dim password As String
        password = Console.ReadLine()
        ' Create a new Uri object.
        Dim newUri As New Uri(proxyAddress)
        ' Associate the newUri object to 'myProxy' object so that new myProxy settings can be set.
        myProxy.Address = newUri
        ' Create a NetworkCredential object and associate it with the Proxy property of request object.
        myProxy.Credentials = New NetworkCredential(username, password)
        myWebRequest.Proxy = myProxy
    End If
    Console.WriteLine(ControlChars.Cr + "The Address of the  new Proxy settings are {0}", myProxy.Address)
    Dim myWebResponse As HttpWebResponse = CType(myWebRequest.GetResponse(), HttpWebResponse)

[C#] 
// Create a new request to the mentioned URL.                
HttpWebRequest myWebRequest=(HttpWebRequest)WebRequest.Create("http://www.microsoft.com");
WebProxy myProxy=new WebProxy();
// Obtain the 'Proxy' of the  Default browser.  
myProxy=(WebProxy)myWebRequest.Proxy;
// Print the Proxy Url to the console.
Console.WriteLine("\nThe actual default Proxy settings are {0}",myProxy.Address);
try
{
    Console.WriteLine("\nPlease enter the new Proxy Address that is to be set:");
    Console.WriteLine("(Example:http://myproxy.com:port)");
    string proxyAddress;
    proxyAddress =Console.ReadLine();
    if(proxyAddress.Length>0)
    
    {
        Console.WriteLine("\nPlease enter the Credentials ");
        Console.WriteLine("Username:");
        string username;
        username =Console.ReadLine();
        Console.WriteLine("\nPassword:");
        string password;
        password =Console.ReadLine();                    
        // Create a new Uri object.
        Uri newUri=new Uri(proxyAddress);
        // Associate the newUri object to 'myProxy' object so that new myProxy settings can be set.
        myProxy.Address=newUri;
        // Create a NetworkCredential object and associate it with the Proxy property of request object.
        myProxy.Credentials=new NetworkCredential(username,password);
        myWebRequest.Proxy=myProxy;
    }
    Console.WriteLine("\nThe Address of the  new Proxy settings are {0}",myProxy.Address);
    HttpWebResponse myWebResponse=(HttpWebResponse)myWebRequest.GetResponse();

[C++] 
// Create a new request to the mentioned URL.
HttpWebRequest* myWebRequest =
   dynamic_cast<HttpWebRequest*>(WebRequest::Create(S"http://www.microsoft.com"));
WebProxy* myProxy = new WebProxy();
// Obtain the 'Proxy' of the  Default browser.
myProxy=dynamic_cast<WebProxy*>(myWebRequest->Proxy);
// Print the Proxy Url to the console.
Console::WriteLine(S"\nThe actual default Proxy settings are {0}", myProxy->Address);
try {
   Console::WriteLine(S"\nPlease enter the new Proxy Address that is to be set:");
   Console::WriteLine(S"(Example:http://myproxy.com:port)");
   String* proxyAddress;
   proxyAddress =Console::ReadLine();
   if (proxyAddress->Length>0) {
      Console::WriteLine(S"\nPlease enter the Credentials ");
      Console::WriteLine(S"Username:");
      String* username;
      username =Console::ReadLine();
      Console::WriteLine(S"\nPassword:");
      String* password;
      password =Console::ReadLine();
      // Create a new Uri object.
      Uri* newUri = new Uri(proxyAddress);
      // Associate the newUri object to 'myProxy' object so that new myProxy settings can be set.
      myProxy->Address=newUri;
      // Create a NetworkCredential object and associate it with the Proxy property of request object.
      myProxy->Credentials = new NetworkCredential(username, password);
      myWebRequest->Proxy=myProxy;
   }
   Console::WriteLine(S"\nThe Address of the  new Proxy settings are {0}",
      myProxy->Address);
   HttpWebResponse* myWebResponse =
      dynamic_cast<HttpWebResponse*>(myWebRequest->GetResponse());

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

.NET Framework Security: 

See Also

HttpWebRequest Class | HttpWebRequest Members | System.Net Namespace

Show:
© 2014 Microsoft