Freigeben über


HttpWebRequest.Proxy-Eigenschaft

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

Namespace: System.Net
Assembly: System (in system.dll)

Syntax

'Declaration
Public Overrides Property Proxy As IWebProxy
'Usage
Dim instance As HttpWebRequest
Dim value As IWebProxy

value = instance.Proxy

instance.Proxy = value
public override IWebProxy Proxy { get; set; }
public:
virtual property IWebProxy^ Proxy {
    IWebProxy^ get () override;
    void set (IWebProxy^ value) override;
}
/** @property */
public IWebProxy get_Proxy ()

/** @property */
public void set_Proxy (IWebProxy value)
public override function get Proxy () : IWebProxy

public override function set Proxy (value : IWebProxy)

Eigenschaftenwert

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

Ausnahmen

Ausnahmetyp Bedingung

ArgumentNullException

Proxy wurde auf NULL (Nothing in Visual Basic) festgelegt.

InvalidOperationException

Die Anforderung wurde durch einen Aufruf von GetRequestStream, BeginGetRequestStream, GetResponse oder BeginGetResponse gestartet.

SecurityException

Der Aufrufer verfügt nicht über die Berechtigung für die angeforderte Operation.

Hinweise

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

Wenn die Proxy-Eigenschaft geändert wird, nachdem die Anforderung durch den Aufruf der GetRequestStream-Methode, der BeginGetRequestStream-Methode, der GetResponse-Methode oder der BeginGetResponse-Methode gestartet wurde, wird eine InvalidOperationException ausgelöst. Informationen über das Proxyelement finden Sie unter DefaultProxy Element (Network Settings).

Beispiel

Im folgenden Codebeispiel wird die Proxy-Methode zum Abrufen der Proxyinformationen für die Anforderung verwendet.

' Create a new request to the mentioned URL.                
Dim myWebRequest As HttpWebRequest = CType(WebRequest.Create("https://www.microsoft.com"), HttpWebRequest)
Dim myProxy As New WebProxy()
' Obtain the 'Proxy' of the  Default browser.  
myProxy = CType(myWebRequest.Proxy, WebProxy)
' Print the Proxy Url to the console.
Console.WriteLine(ControlChars.Cr + "The actual default Proxy settings are {0}", myProxy.Address)
Try
    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
    proxyAddress = Console.ReadLine()
    If proxyAddress.Length = 0 Then
        myWebRequest.Proxy = myProxy
    Else
        Console.WriteLine(ControlChars.Cr + "Please enter the Credentials")
        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)
// Create a new request to the mentioned URL.                
HttpWebRequest myWebRequest=(HttpWebRequest)WebRequest.Create("https://www.microsoft.com");
WebProxy myProxy=new WebProxy();
// Obtain the 'Proxy' of the  Default browser.  
myProxy=(WebProxy)myWebRequest.Proxy;
// Print the Proxy Url to the console.
Console.WriteLine("\nThe actual default Proxy settings are {0}",myProxy.Address);
try
{
    Console.WriteLine("\nPlease enter the new Proxy Address that is to be set:");
    Console.WriteLine("(Example:http://myproxy.example.com:port)");
    string proxyAddress;
    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=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.
HttpWebRequest^ myWebRequest = (HttpWebRequest^)( WebRequest::Create( "https://www.microsoft.com" ) );
WebProxy^ myProxy = gcnew WebProxy;
// Obtain the 'Proxy' of the  Default browser.
myProxy = (WebProxy^)( myWebRequest->Proxy );
// Print the Proxy Url to the console.
Console::WriteLine( "\nThe actual default Proxy settings are {0}", myProxy->Address );
try
{
   Console::WriteLine( "\nPlease enter the new Proxy Address that is to be set:" );
   Console::WriteLine( "(Example:http://myproxy.example.com:port)" );
   String^ proxyAddress;
   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("https://www.microsoft.com");
WebProxy myProxy = new WebProxy();
// Obtain the 'Proxy' of the  Default browser.  
myProxy = (WebProxy)myWebRequest.get_Proxy();

// Print the Proxy Url to the console.
Console.WriteLine("\nThe actual default Proxy settings are {0}",
    myProxy.get_Address());
try {
    Console.WriteLine("\nPlease enter the new Proxy Address "
        +" that is to be set:");
    Console.WriteLine("(Example:http://myproxy.com:port)");
    String proxyAddress;
    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 = new Uri(proxyAddress);

        // Associate the newUri object to 'myProxy' object so that 
        // new myProxy settings can be set.
        myProxy.set_Address(newUri);

        // Create a NetworkCredential object and associate it with 
        // the Proxy property of request object.
        myProxy.set_Credentials(new NetworkCredential(username,
            password));
        myWebRequest.set_Proxy(myProxy);
    }

    Console.WriteLine("\nThe Address of the  new Proxy settings "
        + "are {0}", myProxy.get_Address());
    HttpWebResponse myWebResponse = (HttpWebResponse)
        myWebRequest.GetResponse();

.NET Framework-Sicherheit

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

HttpWebRequest-Klasse
HttpWebRequest-Member
System.Net-Namespace

Weitere Ressourcen

DefaultProxy Element (Network Settings)
Konfigurieren von Internetanwendungen
Proxykonfiguration
Automatisches Erkennen eines Proxys