HttpWebRequest.Proxy Właściwość

Definicja

Pobiera lub ustawia informacje o serwerze proxy dla żądania.

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

Wartość właściwości

Obiekt IWebProxy używany do serwera proxy żądania. Wartość domyślna jest ustawiana przez wywołanie Select właściwości.

Wyjątki

Proxy jest ustawiona na nullwartość .

Obiekt wywołujący nie ma uprawnień do żądanej operacji.

Przykłady

Poniższy przykład kodu używa Proxy metody , aby uzyskać informacje o serwerze proxy dla żądania.

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

Uwagi

Właściwość Proxy identyfikuje WebProxy obiekt używany do przetwarzania żądań do zasobów internetowych. Aby określić, że nie należy używać żadnego serwera proxy, ustaw Proxy właściwość na wystąpienie serwera proxy zwrócone przez metodę GlobalProxySelection.GetEmptyWebProxy .

Plik konfiguracji komputera lokalnego lub aplikacji może określać, że jest używany domyślny serwer proxy. Proxy Jeśli właściwość zostanie określona, ustawienia serwera proxy z Proxy właściwości zastąpią plik konfiguracji komputera lokalnego lub aplikacji, a HttpWebRequest wystąpienie będzie używać określonych ustawień serwera proxy. Jeśli żaden serwer proxy nie jest określony w pliku konfiguracji, a Proxy właściwość jest nieokreślona, HttpWebRequest klasa używa ustawień serwera proxy dziedziczonych z programu Internet Explorer na komputerze lokalnym. Jeśli w programie Internet Explorer nie ma ustawień serwera proxy, żądanie jest wysyłane bezpośrednio do serwera.

Klasa HttpWebRequest analizuje listę obejścia serwera proxy z symbolami wieloznacznymi dziedziczone z programu Internet Explorer tak samo jak lista obejścia jest analizowana bezpośrednio przez program Internet Explorer. Na przykład HttpWebRequest klasa przeanalizuje listę obejścia "nt*" z programu Internet Explorer jako wyrażenie regularne "nt.*". Dlatego adres URL "http://nt.com" pomija serwer proxy przy użyciu HttpWebRequest klasy i przy użyciu programu Internet Explorer.

Klasa HttpWebRequest obsługuje obejście lokalnego serwera proxy. Klasa uważa, że miejsce docelowe jest lokalne, jeśli zostaną spełnione jakiekolwiek z następujących warunków:

  • Miejsce docelowe zawiera płaską nazwę (bez kropek w adresie URL).

  • Miejsce docelowe zawiera adres sprzężenia zwrotnego (Loopback lub IPv6Loopback) lub miejsce docelowe zawiera przypisaną IPAddress do komputera lokalnego.

  • Sufiks domeny miejsca docelowego jest zgodny z sufiksem domeny komputera lokalnego (DomainName).

Proxy Zmiana właściwości po uruchomieniu żądania przez wywołanie GetRequestStreammetody , , BeginGetRequestStreamGetResponselub BeginGetResponse zgłasza błąd InvalidOperationException. Aby uzyskać informacje na temat elementu serwera proxy, zobacz < defaultProxy> Element (ustawienia sieciowe).

Dotyczy

Zobacz też