HttpWebRequest.Proxy Egenskap
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Hämtar eller anger proxyinformation för begäran.
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
Egenskapsvärde
Objektet IWebProxy som ska användas för att proxyservern begäran. Standardvärdet anges genom att anropa egenskapen Select .
Undantag
Proxy är inställt på null.
Begäran har startats genom att anropa GetRequestStream(), BeginGetRequestStream(AsyncCallback, Object), GetResponse()eller BeginGetResponse(AsyncCallback, Object).
Anroparen har inte behörighet för den begärda åtgärden.
Exempel
I följande kodexempel används Proxy metoden för att hämta proxyinformationen för begäran.
// 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)
Kommentarer
Caution
WebRequest, HttpWebRequest, ServicePoint, och WebClient är föråldrade och du bör inte använda dem för ny utveckling. Använd HttpClient i stället.
Egenskapen Proxy identifierar objektet som WebProxy ska användas för att bearbeta begäranden till Internetresurser. Om du vill ange att ingen proxy ska användas anger du Proxy egenskapen till den proxyinstans som returneras av GlobalProxySelection.GetEmptyWebProxy metoden.
Den lokala datorn eller programkonfigurationsfilen kan ange att en standardproxy ska användas. Om egenskapen Proxy anges åsidosätter proxyinställningarna från Proxy egenskapen den lokala datorn eller programkonfigurationsfilen och instansen HttpWebRequest använder de angivna proxyinställningarna. Om ingen proxy anges i en konfigurationsfil och Proxy egenskapen är ospecificerad HttpWebRequest använder klassen proxyinställningarna som ärvts från Internetalternativen på den lokala datorn. Om det inte finns några proxyinställningar i Internetalternativ skickas begäran direkt till servern.
Klassen HttpWebRequest stöder lokal proxy bypass. Klassen anser att ett mål är lokalt om något av följande villkor uppfylls:
Målet innehåller ett platt namn (inga punkter i URL:en).
Målet innehåller en loopback-adress (Loopback eller IPv6Loopback) eller målet innehåller en IPAddress tilldelad till den lokala datorn.
Målets domänsuffix matchar den lokala datorns domänsuffix (DomainName).
Proxy När du ändrar egenskapen efter att begäran har startats genom att anropa GetRequestStreammetoden , BeginGetRequestStream, GetResponseeller BeginGetResponse genererar en InvalidOperationException. Information om proxyelementet finns