Description





Description

WebRequest instances use WebProxy instances to override the proxy settings in GlobalProxySelection.

[Note: Local requests are identified by the lack of a period (.) in the authority of the URI, as in "http://webserver/" versus http://www.contoso.com/".]

Example

Figure

The following example sets a WebProxy for a WebRequest. The WebRequest instance uses the proxy to connect to external Internet resources.


using System;

using System.Net;



public class WebProxyExample

{



    public static void Main()

    {



        WebProxy proxyObject =

            new WebProxy("http://proxyserver:80/",true);

        WebRequest req =

            WebRequest.Create("http://www.contoso.com");

        req.Proxy = proxyObject;

    }

}


Figure


using System;

using System.Net;



public class WebProxySample

{

    public static void Main()

    {

        String[] bplist = new String[] {"mysite.com/*",

                                           "http://*.microsoft.com/*",

                                           "http://test.org/test.htm"};

        NetworkCredential nc = new NetworkCredential("test", "secret");

        Uri path = new Uri("http://mysite.com");

        CredentialCache cc = new CredentialCache();

        cc.Add(path, "BASIC", nc);

        WebProxy wp = new WebProxy(new Uri("http://myproxy/"),

            true, bplist, cc);



        Console.WriteLine("WebProxy property values:");

        Console.WriteLine("Address = '{0}'", wp.Address);

        Console.Write("BypassList = ");

        foreach (String item in wp.BypassList)

        {

            Console.Write(item + " ");

        }

        Console.WriteLine();

        Console.WriteLine("BypassProxyOnLocal = {0}",

            wp.BypassProxyOnLocal);

        NetworkCredential found =

            wp.Credentials.GetCredential(path, "BASIC");

        Console.WriteLine("Credentials.UserName = '{0}'",

            found.UserName);

        Console.WriteLine("Credentials.Password = '{0}'",

            found.Password);

        Console.WriteLine();

        Console.WriteLine();

        Console.WriteLine("Press Enter to continue");

        Console.ReadLine();

    }

}


The output is

WebProxy property values:

Address = 'http://myproxy/'

BypassList = mysite.com/* http://*.microsoft.com/* http://test.org/test.htm

BypassProxyOnLocal = True

Credentials.UserName = 'test'

Credentials.Password = 'secret'





Press Enter to continue



     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows