WebRequest.Proxy Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

When overridden in a descendant class, gets or sets the network proxy to use to access this Internet resource.

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

public virtual IWebProxy Proxy { get; set; }

Property Value

Type: System.Net.IWebProxy

The IWebProxy to use to access the Internet resource.

Exception Condition

Any attempt is made to get or set the property, when the property is not overridden in a descendant class.

The Proxy property identifies the network proxy that the request uses to access the Internet resource. The request is made through the proxy server rather than directly to the Internet resource.


The WebRequest class is an abstract class. The actual behavior of WebRequest instances at run time is determined by the descendant class returned by the WebRequest.Create method. For more information about default values and exceptions, see the documentation for the descendant classes, such as HttpWebRequest and FileWebRequest.

The following example displays the current network proxy address and allows the user to assign a new network proxy address and port number.

// Create a new request to the mentioned URL.				
WebRequest myWebRequest=WebRequest.Create("http://www.contoso.com");

WebProxy myProxy=new WebProxy();
// Obtain the Proxy Prperty of the  Default browser.  

// Print myProxy address to the console.
Console.WriteLine("\nThe actual default Proxy settings are {0}",myProxy.Address);
	Console.WriteLine("\nPlease enter the new Proxy Address to be set ");
	Console.WriteLine("The format of the address should be http://proxyUriAddress:portaddress");
	string proxyAddress;
	proxyAddress =Console.ReadLine();

		Console.WriteLine("\nPlease enter the Credentials");
		string username;
		username =Console.ReadLine();
		string password;
		password =Console.ReadLine();

		// Create a new Uri object.
		Uri newUri=new Uri(proxyAddress);

		// Associate the new Uri object to the myProxy object.

		// Create a NetworkCredential object and is assign to the Credentials property of the Proxy object.
		myProxy.Credentials=new NetworkCredential(username,password);
	Console.WriteLine("\nThe Address of the  new Proxy settings are {0}",myProxy.Address);
	WebResponse myWebResponse=myWebRequest.GetResponse();

	// Print the  HTML contents of the page to the console.
	Stream streamResponse=myWebResponse.GetResponseStream();
	StreamReader streamRead = new StreamReader( streamResponse );
	Char[] readBuff = new Char[256];
	int count = streamRead.Read( readBuff, 0, 256 );
	Console.WriteLine("\nThe contents of the Html pages are :");	
	while (count > 0) 
		String outputData = new String(readBuff, 0, count);
		count = streamRead.Read(readBuff, 0, 256);

	// Close the Stream object.

	// Release the HttpWebResponse Resource.
	Console.WriteLine("\nPress any key to continue.........");
catch(UriFormatException e)
	Console.WriteLine("\nUriFormatException is thrown.Message is {0}",e.Message);
	Console.WriteLine("\nThe format of the myProxy address you entered is invalid");
	Console.WriteLine("\nPress any key to continue.........");

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Windows Phone
Available since 8.1
Return to top