Share via


Accessing the Internet Through a Proxy

If your site uses a proxy to provide access to the Internet, you must configure a proxy instance to enable your application to communicate with the Web proxy.

The following code example creates a global proxy instance that will enable any WebRequest to use a proxy to communicate with the Internet. The example assumes that the proxy server is named webproxy and that it communicates on port 80, the standard HTTP port.

WebProxy proxyObject = new WebProxy("http://webproxy:80/");
GlobalProxySelection.Select = proxyObject;
[Visual Basic]
Dim proxyObject As WebProxy = New WebProxy("http://webproxy:80/")
GlobalProxySelection.Select = proxyObject

You can override the global proxy selection by assigning an instance that implements the IWebProxy interface to the Proxy property of your WebRequest. The following code example sends a WebRequest to www.contoso.com that overrides the global proxy selection with a proxy server named alternateproxy on port 80.

WebRequest req = new WebRequest.Create("https://www.contoso.com/");
req.Proxy = new WebProxy("http://alternateproxy:80/");
[Visual Basic]
Dim req As WebRequest = new WebRequest.Create("https://www.contoso.com/")
req.Proxy = New WebProxy("http://alternateproxy:80/")

See Also

Using Application Protocols | Accessing the Internet