Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
 Proxy Property
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
HttpWebRequest..::.Proxy Property

Gets or sets proxy information for the request.

Namespace:  System.Net
Assembly:  System (in System.dll)
Visual Basic (Declaration)
Public Overrides Property Proxy As IWebProxy
Visual Basic (Usage)
Dim instance As HttpWebRequest
Dim value As IWebProxy

value = instance.Proxy

instance.Proxy = value
C#
public override IWebProxy Proxy { get; set; }
Visual C++
public:
virtual property IWebProxy^ Proxy {
    IWebProxy^ get () override;
    void set (IWebProxy^ value) override;
}
JScript
public override function get Proxy () : IWebProxy
public override function set Proxy (value : IWebProxy)

Property Value

Type: System.Net..::.IWebProxy
The IWebProxy object to use to proxy the request. The default value is set by calling the GlobalProxySelection..::.Select property.
ExceptionCondition
ArgumentNullException

Proxy is set to nullNothingnullptra 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.

The local computer or application config file may specify that a default proxy be used. If the Proxy property is specified, then the proxy settings from the Proxy property override the local computer or application config file and the HttpWebRequest instance will use the proxy settings specified. If no proxy is specified in a config file and the Proxy property is unspecified, the HttpWebRequest class uses the proxy settings inherited from Internet Explorer on the local computer. If there are no proxy settings in Internet Explorer, the request is sent directly to the server.

The HttpWebRequest class parses a proxy bypass list with wildcard characters inherited from Internet Explorer the same as the bypass list is parsed directly by Internet Explorer. For example, the HttpWebRequest class will parse a bypass list of "nt*" from Internet Explorer as a regular expression of "nt.*". So a URL of "http://nt.com" would bypass the proxy using the HttpWebRequest class and using Internet Explorer.

The HttpWebRequest class supports local proxy bypass. The class considers a proxy to be local if any of the following conditions are met:

  • The proxy contains a flat name (no dots in the URL).

  • The proxy contains a loopback address (Loopback or IPv6Loopback) or the proxy contains an IPAddress assigned to the local computer.

  • The domain suffix of the proxy matches the local computer's domain suffix (DomainName).

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.

Visual Basic
            ' Create a new request to the mentioned URL.                
            Dim myWebRequest As HttpWebRequest = CType(WebRequest.Create("http://www.microsoft.com"), HttpWebRequest)

                  ' Obtain the 'Proxy' of the  Default browser.  
                  Dim proxy as IWebProxy = CType(myWebRequest.Proxy, IWebProxy)
                  ' Print the Proxy Url to the console.
            If Not proxy Is Nothing Then
                Console.WriteLine("Proxy: {0}", proxy.GetProxy(myWebRequest.RequestUri))
            Else
                Console.WriteLine("Proxy is null; no proxy will be used")
            End If

            Dim myProxy As New WebProxy()

            Console.WriteLine(ControlChars.Cr + "Please enter the new Proxy Address that is to be set ")
            Console.WriteLine("(Example:http://myproxy.example.com:port)")
            Dim proxyAddress As String
            Try
                proxyAddress = Console.ReadLine()
                If proxyAddress.Length = 0 Then
                    myWebRequest.Proxy = myProxy
                Else
                    Console.WriteLine(ControlChars.Cr + "Please enter the Credentials (may not be needed)")
                    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");

// Obtain the 'Proxy' of the  Default browser.  
IWebProxy proxy = myWebRequest.Proxy;
// Print the Proxy Url to the console.
if (proxy != null)
{
    Console.WriteLine("Proxy: {0}", proxy.GetProxy(myWebRequest.RequestUri));
} 
else
{
    Console.WriteLine("Proxy is null; no proxy will be used");
}

WebProxy myProxy=new WebProxy();

Console.WriteLine("\nPlease enter the new Proxy Address that is to be set:");
Console.WriteLine("(Example:http://myproxy.example.com:port)");
string proxyAddress;

try
{
    proxyAddress =Console.ReadLine();
    if(proxyAddress.Length>0)
    {
        Console.WriteLine("\nPlease enter the Credentials (may not be needed)");
        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();
Visual C++
// Create a new request to the mentioned URL.
HttpWebRequest ^ myWebRequest =
    (HttpWebRequest ^) (WebRequest::Create("http://www.microsoft.com"));

// Obtain the 'Proxy' of the  Default browser.  
IWebProxy ^ proxy = myWebRequest->Proxy;
// Print the Proxy Url to the console.
if (proxy) 
{
    Console::WriteLine("Proxy: {0}",
        proxy->GetProxy(myWebRequest->RequestUri));
} 
else 
{
    Console::WriteLine("Proxy is null; no proxy will be used");
}

WebProxy ^ myProxy = gcnew WebProxy;

Console::WriteLine("\nPlease enter the new Proxy Address that is to be set:");
Console::WriteLine("(Example:http://myproxy.example.com:port)");
String ^ proxyAddress;

try 
{
    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 = gcnew 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 =
            gcnew NetworkCredential(username, password);
        myWebRequest->Proxy = myProxy;
    }
    Console::WriteLine("\nThe Address of the  new Proxy settings are {0}",
                  myProxy->Address);
    HttpWebResponse ^ myWebResponse =
        (HttpWebResponse ^) (myWebRequest->GetResponse());
CPP_OLD
// 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.example.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());

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

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0
Community Content   What is Community Content?
Add new content RSS  Annotations
Host Not Found Errors      Steve Butler MSFT   |   Edit   |   Show History
Do you have WebRequest code that works great in a console application, but gives "Host Not Found" exception errors when run in IIS? Are you running in an environment behind a firewall? If so almost certainly need to set a proxy for your connections either in code or configuration.

"Host Not Found" is the classic exception for this situation - but it may manifest itself in other forms e.g. "Cannot Connect To <host>" etc.


Processing
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker