HttpWebRequest.Proxy 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定要求的 Proxy 資訊。
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
屬性值
要用來 Proxy 要求 IWebProxy 物件。 預設值是藉由呼叫 Select 屬性來設定。
例外狀況
Proxy 設定為 null
。
要求已透過呼叫 GetRequestStream()、BeginGetRequestStream(AsyncCallback, Object)、GetResponse()或 BeginGetResponse(AsyncCallback, Object)來啟動。
呼叫端沒有要求之作業的許可權。
範例
下列程式代碼範例會使用 Proxy 方法來取得要求的 Proxy 資訊。
// 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)
備註
謹慎
WebRequest
、HttpWebRequest
、ServicePoint
和 WebClient
已經過時,您不應該將它們用於新的開發。 請改用 HttpClient。
Proxy 屬性會識別用來處理因特網資源要求 WebProxy 物件。 若要指定不應使用 Proxy,請將 Proxy 屬性設定為 GlobalProxySelection.GetEmptyWebProxy 方法所傳回的 Proxy 實例。
本機電腦或應用程式組態檔可以指定使用預設 Proxy。 如果指定了 Proxy 屬性,則來自 Proxy 屬性的 Proxy 設定會覆寫本機計算機或應用程式組態檔,而 HttpWebRequest 實例會使用指定的 Proxy 設定。 如果未指定組態檔中的 Proxy,且未指定 Proxy 屬性,HttpWebRequest 類別會使用繼承自本機計算機上的因特網選項的 Proxy 設定。 如果因特網選項中沒有 Proxy 設定,則要求會直接傳送至伺服器。
HttpWebRequest 類別支援本機 Proxy 略過。 如果符合下列任何條件,類別會將目的地視為本機:
目的地包含一般名稱(URL 中沒有點)。
目的地包含回送位址(Loopback 或 IPv6Loopback),或目的地包含指派給本機計算機的 IPAddress。
目的地的網域後綴符合本機計算機的網域後綴 (DomainName)。
呼叫 GetRequestStream、BeginGetRequestStream、GetResponse或 BeginGetResponse 方法,以在要求啟動之後變更 Proxy 屬性會擲回 InvalidOperationException。 如需 Proxy 元素的資訊,請參閱 <defaultProxy>元素 (網路設定)。