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
屬性值
IWebProxy用來代理請求的物件。 預設值是透過呼叫屬性 Select 來設定的。
例外狀況
Proxy 設定為 null。
請求的啟動方式是呼叫 GetRequestStream()、 BeginGetRequestStream(AsyncCallback, Object)、 GetResponse()或 BeginGetResponse(AsyncCallback, Object)。
呼叫端沒有要求之作業的許可權。
範例
以下程式碼範例使用該 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 != 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、ServicePointWebClient、和 都已過時,你不應該用它們來做新開發。 請改用 HttpClient。
該 Proxy 屬性用 WebProxy 以識別用於處理對網際網路資源請求的物件。 若要指定不應使用 Proxy,請將 Proxy 屬性設定為 GlobalProxySelection.GetEmptyWebProxy 方法所傳回的 Proxy 執行個體。
本機電腦或應用程式組態檔可以指定使用預設 Proxy。 若指定了 Proxy 該屬性,則該屬性中的 Proxy 代理設定會覆蓋本地電腦或應用程式設定檔,實 HttpWebRequest 例將使用指定的代理設定。 如果設定檔 Proxy 中未指定代理且屬性未指定,類別 HttpWebRequest 則使用從本地電腦 Internet options 繼承的代理設定。 如果因特網選項中沒有 Proxy 設定,則要求會直接傳送至伺服器。
HttpWebRequest 類別支援本地 Proxy 略過。 如果符合下列任何條件,類別會將目的地視為本機:
目的地包含一般名稱(URL 中沒有點)。
目的地包含迴圈位址(Loopback 或 IPv6Loopback),或目的地包含指派給本地電腦的位址 IPAddress 。
目的地的網域後綴與本地電腦的網域後綴DomainName()相符。
在請求啟動後,透過呼叫 、 、 或 方法來更改Proxy屬性會拋出一個 InvalidOperationException。BeginGetResponseGetResponseBeginGetRequestStreamGetRequestStream 關於代理元素的資訊,請參見