古詩詞大全網 - 成語用法 - “response”對象設置響應頭屬性方法是什麽?

“response”對象設置響應頭屬性方法是什麽?

1、HttpWebResponse類的屬性

CharacterSet     ?獲取響應的字符集。

ContentEncoding   ?獲取用於對響應體進行編碼的方法。

ContentLength    ? 獲取請求返回的內容的長度。 (重寫 WebResponse..::.ContentLength。)

ContentType     ? 獲取響應的內容類型。 (重寫 WebResponse..::.ContentType。)

Cookies       ? 獲取或設置與此響應關聯的 Cookie。

Headers       ?獲取來自服務器的與此響應關聯的標頭。 (重寫 WebResponse..::.Headers。)

IsFromCache     獲取壹個 Boolean 值,該值指示此響應是否為從緩存中獲取的。 (繼承自 WebResponse。)

IsMutuallyAuthenticated 獲取壹個 Boolean 值,該值指示客戶端和服務器是否都已經過身份驗證。 (重寫 WebResponse..::.IsMutuallyAuthenticated。)

LastModified     ? 獲取最後壹次修改響應內容的日期和時間。

Method        獲取用於返回響應的方法。

ProtocolVersion    獲取響應中使用的 HTTP 協議的版本。

ResponseUri     獲取響應請求的 Internet 資源的 URI。 (重寫WebResponse..::.ResponseUri。)

Server        ?獲取發送響應的服務器的名稱。

StatusCode     獲取響應的狀態。

StatusDescription   獲取與響應壹起返回的狀態說明。

使用示例

C# 代碼 ? 復制

static void Main(string[] args)

{

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(""); //創建壹個請求示例

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

Console.WriteLine(response.CharacterSet); ? //輸出 utf-8

Console.WriteLine(response.ContentEncoding); //空

Console.WriteLine(response.ContentLength); ?//輸出 10310

Console.WriteLine(response.ContentType); //輸出 text/html charset=utf-8

CookieCollection cc = response.Cookies;

Console.WriteLine(cc.Count); //輸出 0

WebHeaderCollection whc = response.Headers;

Console.WriteLine(whc.Count); ? //輸出 9

foreach (string h in whc.AllKeys)

{

Console.WriteLine(h.ToString() + " " + whc[h].ToString()); ?//輸出所有的響應頭信息

}

Console.WriteLine(response.IsFromCache); //輸出 false 該值指示響應是否從緩存獲取的?

Console.WriteLine(response.IsMutuallyAuthenticated); //輸出 false 客戶端和服務器端都已通過身份認證

Console.WriteLine(response.LastModified); ? //輸出 2013-04-06 21:03:06 最後壹次修改響應的時間和日期

Console.WriteLine(response.Method); //輸出 Get 返回響應的方法

Console.WriteLine(response.ProtocolVersion); //輸出 1.1 響應的HTTP協議的版本

Console.WriteLine(response.ResponseUri); //輸出 響應請求的Interner資源的URI

Console.WriteLine(response.Server); //輸出 BWS/1.0 發送響應的服務器的名稱

Console.WriteLine(response.StatusCode); //輸出 OK 獲取響應的狀態,這個不是狀態碼,而是狀態描述,為什麽不是200呢,奇怪啊,測試了好幾個網站都是OK,而不是200

Console.WriteLine(response.StatusDescription); ?//輸出 OK 這個是狀態描述,

Console.ReadKey();

}

2、HttpWebResponse類的方法

Close       關閉響應流。 (重寫 WebResponse..::.Close()()()。)

CreateObjRef    ?創建壹個對象,該對象包含生成用於與遠程對象進行通信的代理所需的全部相關信息。 (繼承自 MarshalByRefObject。)

Dispose      ? 釋放由 HttpWebResponse 使用的非托管資源,並可根據需要釋放托管資源。

GetLifetimeService 檢索控制此實例的生存期策略的當前生存期服務對象。 (繼承自 MarshalByRefObject。)

GetObjectData   基礎結構。 使用將目標對象序列化所需的數據填充 SerializationInfo。 (重寫 WebResponse..::.GetObjectData(SerializationInfo, StreamingContext)。)

GetResponseHeader 獲取與響應壹起返回的標頭的內容。

GetResponseStream 獲取流,該流用於讀取來自服務器的響應的體。 (重寫 WebResponse..::.GetResponseStream()()()。)

InitializeLifetimeService 獲取控制此實例的生存期策略的生存期服務對象。 (繼承自 MarshalByRefObject。)