HttpWebRequest.Proxy Propiedad

Definición

Obtiene o establece la información de proxy para la solicitud.

public:
 virtual property System::Net::IWebProxy ^ Proxy { System::Net::IWebProxy ^ get(); void set(System::Net::IWebProxy ^ value); };
public override System.Net.IWebProxy? Proxy { get; set; }
public override System.Net.IWebProxy Proxy { get; set; }
member this.Proxy : System.Net.IWebProxy with get, set
Public Overrides Property Proxy As IWebProxy

Valor de propiedad

Objeto IWebProxy que se utiliza para especificar el servidor proxy de la solicitud. El valor predeterminado se establece mediante una llamada a la propiedad Select.

Excepciones

El valor de Proxy está establecido en null.

El autor de llamada no tiene permiso para la operación solicitada.

Ejemplos

En el ejemplo de código siguiente se usa el Proxy método para obtener la información de proxy de la solicitud.

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

Comentarios

La propiedad Proxy identifica el objeto WebProxy que se va a usar para procesar solicitudes a recursos de Internet. Para especificar que no se debe usar ningún proxy, establezca la propiedad Proxy en la instancia de proxy devuelta por el método GlobalProxySelection.GetEmptyWebProxy.

El equipo local o el archivo de configuración de la aplicación pueden especificar que se use un proxy predeterminado. Si se especifica la Proxy propiedad , la configuración de proxy de la Proxy propiedad invalida el archivo de configuración del equipo local o la aplicación y la HttpWebRequest instancia usará la configuración de proxy especificada. Si no se especifica ningún proxy en un archivo de configuración y la Proxy propiedad no se especifica, la HttpWebRequest clase usa la configuración de proxy heredada de Internet Explorer en el equipo local. Si no hay ninguna configuración de proxy en Internet Explorer, la solicitud se envía directamente al servidor.

La HttpWebRequest clase analiza una lista de omisión de proxy con caracteres comodín heredados de Internet Explorer igual que la lista de omisión se analiza directamente mediante Internet Explorer. Por ejemplo, la HttpWebRequest clase analizará una lista de omisión de "nt*" de Internet Explorer como una expresión regular de "nt.*". Por lo tanto, una dirección URL de "http://nt.com" omitiría el proxy mediante la HttpWebRequest clase y el uso de Internet Explorer.

La clase HttpWebRequest admite la omisión del proxy local. La clase considera que un destino es local si se cumple alguna de las condiciones siguientes:

  • El destino contiene un nombre plano (sin puntos en la dirección URL).

  • El destino contiene una dirección de bucle invertido (Loopback o IPv6Loopback) o el destino contiene un IPAddress asignado al equipo local.

  • El sufijo de dominio del destino coincide con el sufijo de dominio del equipo local (DomainName).

Al cambiar la Proxy propiedad después de que se haya iniciado la solicitud llamando al GetRequestStreammétodo , BeginGetRequestStreamGetResponse, o BeginGetResponse , se produce una InvalidOperationExceptionexcepción . Para obtener información sobre el elemento proxy, consulte < defaultProxy> Elemento (Configuración de red).

Se aplica a

Consulte también