HttpWebRequest.Proxy Proprietà

Definizione

Ottiene o imposta le informazioni sul proxy per la richiesta.

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

Valore della proprietà

Oggetto IWebProxy da usare per il proxy della richiesta. Il valore predefinito viene impostato chiamando la proprietà Select.

Eccezioni

Proxy è impostato su null.

Il chiamante non ha l'autorizzazione per l'operazione richiesta.

Esempio

Nell'esempio di codice seguente viene usato il Proxy metodo per ottenere le informazioni sul proxy per la richiesta.

// 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)

Commenti

La Proxy proprietà identifica l'oggetto WebProxy da utilizzare per elaborare le richieste alle risorse Internet. Per specificare che non deve essere usato alcun proxy, impostare la Proxy proprietà sull'istanza del proxy restituita dal GlobalProxySelection.GetEmptyWebProxy metodo .

Il computer locale o il file di configurazione dell'applicazione può specificare che viene usato un proxy predefinito. Se la Proxy proprietà viene specificata, le impostazioni proxy della Proxy proprietà sostituiscono il computer locale o il file di configurazione dell'applicazione e l'istanza HttpWebRequest userà le impostazioni proxy specificate. Se non viene specificato alcun proxy in un file di configurazione e la Proxy proprietà non è specificata, la HttpWebRequest classe utilizza le impostazioni proxy ereditate da Internet Explorer nel computer locale. Se in Internet Explorer non sono presenti impostazioni proxy, la richiesta viene inviata direttamente al server.

La HttpWebRequest classe analizza un elenco di bypass del proxy con caratteri jolly ereditati da Internet Explorer, lo stesso dell'elenco di bypass viene analizzato direttamente da Internet Explorer. Ad esempio, la HttpWebRequest classe analizzerà un elenco di bypass "nt*" da Internet Explorer come espressione regolare di "nt.*". Quindi, un URL di "http://nt.com" ignora il proxy usando la HttpWebRequest classe e l'uso di Internet Explorer.

La HttpWebRequest classe supporta il bypass proxy locale. La classe considera una destinazione locale se vengono soddisfatte le condizioni seguenti:

  • La destinazione contiene un nome flat (nessun punto nell'URL).

  • La destinazione contiene un indirizzo di loopback (Loopback o IPv6Loopback) o la destinazione contiene un IPAddress oggetto assegnato al computer locale.

  • Il suffisso di dominio della destinazione corrisponde al suffisso di dominio del computer locale (DomainName).

La modifica della proprietà dopo l'avvio Proxy della richiesta chiamando il GetRequestStreammetodo , BeginGetRequestStream, GetResponseo BeginGetResponse genera un'eccezione InvalidOperationException. Per informazioni sull'elemento proxy, vedere < defaultProxy> Elemento (impostazioni di rete).

Si applica a

Vedi anche