HttpWebRequest.Proxy Eigenschaft

Definition

Ruft Proxyinformationen für die Anforderung ab oder legt diese fest.

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

Eigenschaftswert

Das IWebProxy-Objekt, das als Proxy für die Anforderung verwendet werden soll. Der Standardwert wird durch Aufrufen der Select-Eigenschaft festgelegt.

Ausnahmen

Für Proxy ist null festgelegt.

Der Aufrufer verfügt nicht über die Berechtigung für den angeforderten Vorgang.

Beispiele

Im folgenden Codebeispiel wird die Proxy -Methode verwendet, um die Proxyinformationen für die Anforderung abzurufen.

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

Hinweise

Die Eigenschaft Proxy identifiziert das WebProxy-Objekt, das zum Verarbeiten von Anforderungen an Internetressourcen verwendet wird. Um anzugeben, dass kein Proxy verwendet werden soll, legen Sie die Eigenschaft Proxy auf die Proxyinstanz fest, die von der Methode GlobalProxySelection.GetEmptyWebProxy zurückgegeben wird.

In der Konfigurationsdatei des lokalen Computers oder der Anwendung kann angegeben werden, dass ein Standardproxy verwendet werden soll. Wenn die Proxy -Eigenschaft angegeben ist, setzen die Proxyeinstellungen der Proxy Eigenschaft den lokalen Computer oder die Anwendungskonfigurationsdatei außer Kraft, und die HttpWebRequest instance verwendet die angegebenen Proxyeinstellungen. Wenn in einer Konfigurationsdatei kein Proxy angegeben ist und die Proxy Eigenschaft nicht angegeben ist, verwendet die HttpWebRequest -Klasse die Proxyeinstellungen, die vom Internet Explorer auf dem lokalen Computer geerbt wurden. Wenn in internet Explorer keine Proxyeinstellungen vorhanden sind, wird die Anforderung direkt an den Server gesendet.

Die HttpWebRequest -Klasse analysiert eine Proxyumgehungsliste mit Platzhalterzeichen, die vom Internet geerbt werden Explorer wie die Umgehungsliste direkt vom Internet Explorer analysiert wird. Beispielsweise analysiert die HttpWebRequest -Klasse eine Umgehungsliste von "nt*" aus dem Internet Explorer als regulärer Ausdruck von "nt.*". Daher würde eine URL von "http://nt.com" den Proxy mithilfe der HttpWebRequest -Klasse und der Internet-Explorer umgehen.

Die HttpWebRequest-Klasse unterstützt die lokale Proxyumgehung. Die Klasse betrachtet ein Ziel als lokal, wenn eine der folgenden Bedingungen erfüllt ist:

  • Das Ziel enthält einen flachen Namen (keine Punkte in der URL).

  • Das Ziel enthält eine Loopbackadresse (Loopback oder IPv6Loopback), oder das Ziel enthält eine IPAddress, die dem lokalen Computer zugewiesen ist.

  • Das Domänensuffix des Ziels entspricht dem Domänensuffix des lokalen Computers (DomainName).

Wenn Sie die Proxy -Eigenschaft ändern, nachdem die Anforderung gestartet wurde, indem Sie die GetRequestStream- , BeginGetRequestStreamGetResponse- oder BeginGetResponse -Methode aufrufen, wird eine InvalidOperationExceptionausgelöst. Informationen zum Proxyelement finden Sie unter < defaultProxy> Element (Netzwerkeinstellungen).

Gilt für:

Weitere Informationen