WebRequest類:html
對統一資源標識符 (URI) 發出請求。 這是一個 abstract 類。web
命名空間: System.Net
程序集: System(位於 System.dll)
緩存
繼承層次結構安全
System.Object
System.MarshalByRefObject
System.Net.WebRequest
System.IO.Packaging.PackWebRequest
System.Net.FileWebRequest
System.Net.FtpWebRequest
System.Net.HttpWebRequest服務器
語法:
網絡
[SerializableAttribute]public abstract class WebRequest : MarshalByRefObject, ISerializable
構造函數異步
名稱 | 說明 | |
---|---|---|
![]() |
WebRequest() | 初始化 WebRequest 類的新實例。async |
![]() |
WebRequest(SerializationInfo,StreamingContext) | 從 WebRequest 和 SerializationInfo 類的指定實例初始化 StreamingContext 類的新實例。ide |
屬性函數
名稱 | 說明 | |
---|---|---|
![]() |
AuthenticationLevel | 獲取或設置用於此請求的身份驗證和模擬的級別。 |
![]() |
CachePolicy | 獲取或設置此請求的緩存策略。 |
![]() |
ConnectionGroupName | 當在子類中重寫時,獲取或設置請求的鏈接組的名稱。 |
![]() |
ContentLength | 當在子類中被重寫時,獲取或設置所發送的請求數據的內容長度。 |
![]() |
ContentType | 當在子類中被重寫時,獲取或設置所發送的請求數據的內容類型。 |
![]() |
CreatorInstance | 已過期。 當在子類中重寫時,獲取從 IWebRequestCreate 類派生的工廠對象,該類用於建立爲生成對指定 URI 的請求而實例化的 WebRequest。 |
![]() |
Credentials | 當在子類中被重寫時,獲取或設置用於對 Internet 資源請求進行身份驗證的網絡憑據。 |
![]() ![]() |
DefaultCachePolicy | 獲取或設置此請求的默認緩存策略。 |
![]() ![]() |
DefaultWebProxy | 獲取或設置全局 HTTP 代理。 |
![]() |
Headers | 當在子類中被重寫時,獲取或設置與請求關聯的標頭名稱/值對的集合。 |
![]() |
ImpersonationLevel | 獲取或設置當前請求的模擬級別。 |
![]() |
Method | 當在子類中被重寫時,獲取或設置要在此請求中使用的協議方法。 |
![]() |
PreAuthenticate | 當在子類中被重寫時,指示是否對請求進行預身份驗證。 |
![]() |
Proxy | 當在子類中被重寫時,獲取或設置用於訪問此 Internet 資源的網絡代理。 |
![]() |
RequestUri | 當在子類中被重寫時,獲取與請求關聯的 Internet 資源的 URI。 |
![]() |
Timeout | 獲取或設置請求超時以前的時間長度(以毫秒爲單位)。 |
![]() |
UseDefaultCredentials | 當在子代類中重寫時,獲取或設置一個 Boolean 值,該值控制 DefaultCredentials 是否隨請求一塊兒發送。 |
名稱 | 說明 | |
---|---|---|
![]() ![]() |
ISerializable.GetObjectData(SerializationInfo,StreamingContext) | 此 API 支持 產品 基礎結構,不該從代碼直接使用。 當在子代類中重寫時,使用序列化 WebRequest 所須要的數據來填充 SerializationInfo 實例。 |
WebRequest 是 abstract 基類,用於從 Internet 中訪問數據的.NET Framework 的請求/響應模型。 使用請求/響應模型的應用程序能夠在應用程序的實例的工做一種協議不可知的方式從 Internet 請求數據 WebRequest 類時特定於協議的子代類執行請求的詳細信息。
從特定的 URI,例如 Web 頁的服務器上的應用程序發送請求。 URI 用於肯定要從列表中建立的正確子代類 WebRequest 後代註冊應用程序。 WebRequest 後代一般被註冊來處理特定的協議,如 HTTP 或 FTP,但也能夠註冊來處理對特定服務器或服務器上的路徑的請求。
WebRequest 類將引起 WebException 中發生錯誤時訪問 Internet 資源時。 Status 屬性是一個 WebExceptionStatus 值,該值指示錯誤的來源。 當 Status 是 WebExceptionStatus.ProtocolError, 、 Response 屬性包含 WebResponse 收到來自 Internet 資源。
由於 WebRequest 類是 abstract 類的實際行爲 WebRequest 實例在運行時由子代類返回 Create 方法。 默認值和異常有關的詳細信息,請參閱文檔對於子代類中,如 HttpWebRequest 和 FileWebRequest。
![]() |
---|
使用 Create 方法初始化新 WebRequest 實例。 請不要使用 WebRequest 構造函數。
若是使用普通用戶的憑據運行的應用程序建立 WebRequest 對象,該應用程序不能訪問證書安裝在本地計算機存儲區中,除非權限顯式授予了用戶若要這樣作。
繼承自 WebRequest, ,必須重寫如下成員︰ Method, ,RequestUri, ,Headers, ,ContentLength, ,ContentType, ,Credentials, ,PreAuthenticate, ,GetRequestStream, ,BeginGetRequestStream, ,EndGetRequestStream, ,GetResponse, ,BeginGetResponse, ,和 EndGetResponse。 此外,必須提供實現 IWebRequestCreate 接口,它定義 Create 方法在調用時使用 Create。 您必須註冊的類的實現 IWebRequestCreate 接口,使用 RegisterPrefix 方法或配置文件。
下面的示例演示如何建立 WebRequest 實例,並返回響應。
using System; using System.IO; using System.Net; using System.Text; namespace Examples.System.Net { public class WebRequestGetExample { public static void Main () { // Create a request for the URL. WebRequest request = WebRequest.Create ("http://www.contoso.com/default.html"); // If required by the server, set the credentials. request.Credentials = CredentialCache.DefaultCredentials; // Get the response. HttpWebResponse response = (HttpWebResponse)request.GetResponse (); // Display the status. Console.WriteLine (response.StatusDescription); // Get the stream containing content returned by the server. Stream dataStream = response.GetResponseStream (); // Open the stream using a StreamReader for easy access. StreamReader reader = new StreamReader (dataStream); // Read the content. string responseFromServer = reader.ReadToEnd (); // Display the content. Console.WriteLine (responseFromServer); // Cleanup the streams and the response. reader.Close (); dataStream.Close (); response.Close (); } } }
To access the requested URI or any URI that the request is redirected to. Associated enumeration: F:System.Net.NetworkAccess.Connect.
備註:轉自https://msdn.microsoft.com/zh-cn/library/system.net.webrequest(v=vs.110).aspx