Compartir por


HttpWebRequest.Proxy Propiedad

Definición

Obtiene o establece 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 va a usar para proxy de la solicitud. El valor predeterminado se establece llamando a la Select propiedad .

Excepciones

El valor de Proxy está establecido en null.

El autor de la 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 del 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 != 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

Cautela

WebRequest, HttpWebRequest, ServicePointy WebClient están obsoletos y no debe usarlos para el nuevo desarrollo. Use HttpClient en su lugar.

La Proxy propiedad identifica el WebProxy objeto 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 de 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 está especificada, la HttpWebRequest clase usa la configuración de proxy heredada de las opciones de Internet en el equipo local. Si no hay ninguna configuración de proxy en las opciones de Internet, la solicitud se envía directamente al servidor.

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 ) o IPv6Loopbackel 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 iniciar la solicitud, al llamar al GetRequestStreammétodo , BeginGetRequestStream, GetResponseo BeginGetResponse se produce una InvalidOperationExceptionexcepción . Para obtener información sobre el elemento proxy vea < defaultProxy> Elemento (Configuración de red).

Se aplica a

Consulte también