Export (0) Print
Expand All

HttpWebRequest.Proxy Property

Gets or sets proxy information for the request.

Namespace: System.Net
Assembly: System (in system.dll)

public override IWebProxy Proxy { get; set; }
/** @property */
public IWebProxy get_Proxy ()

/** @property */
public void set_Proxy (IWebProxy value)

public override function get Proxy () : IWebProxy

public override function set Proxy (value : IWebProxy)

Property Value

The IWebProxy object to use to proxy the request. The default value is set by calling the GlobalProxySelection.Select property.

Exception typeCondition

ArgumentNullException

Proxy is set to a null reference (Nothing in Visual Basic).

InvalidOperationException

The request has been started by calling GetRequestStream, BeginGetRequestStream, GetResponse, or BeginGetResponse.

SecurityException

The caller does not have permission for the requested operation.

The Proxy property identifies the WebProxy object 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 the GetRequestStream, BeginGetRequestStream, GetResponse, or BeginGetResponse method throws an InvalidOperationException. For information on the proxy element see DefaultProxy Element (Network Settings).

The following code example uses the Proxy method to get the proxy information for the request.

// 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.example.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();

// 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.get_Proxy();

// Print the Proxy Url to the console.
Console.WriteLine("\nThe actual default Proxy settings are {0}",
    myProxy.get_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.set_Address(newUri);

        // Create a NetworkCredential object and associate it with 
        // the Proxy property of request object.
        myProxy.set_Credentials(new NetworkCredential(username,
            password));
        myWebRequest.set_Proxy(myProxy);
    }

    Console.WriteLine("\nThe Address of the  new Proxy settings "
        + "are {0}", myProxy.get_Address());
    HttpWebResponse myWebResponse = (HttpWebResponse)
        myWebRequest.GetResponse();

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft