HttpWebResponse 類別

定義

提供 WebResponse 類別的 HTTP 特定實作。Provides an HTTP-specific implementation of the WebResponse class.

public ref class HttpWebResponse : System::Net::WebResponse, IDisposable, System::Runtime::Serialization::ISerializable
[System.Serializable]
public class HttpWebResponse : System.Net.WebResponse, IDisposable, System.Runtime.Serialization.ISerializable
type HttpWebResponse = class
    inherit WebResponse
    interface ISerializable
    interface IDisposable
Public Class HttpWebResponse
Inherits WebResponse
Implements IDisposable, ISerializable
繼承
屬性
實作

範例

下列範例會從 HttpWebRequest傳回 HttpWebResponseThe following example returns an HttpWebResponse from an HttpWebRequest.

HttpWebRequest^ HttpWReq = dynamic_cast<HttpWebRequest^>(WebRequest::Create( "http://www.contoso.com" ));
HttpWebResponse^ HttpWResp = dynamic_cast<HttpWebResponse^>(HttpWReq->GetResponse());

// Insert code that uses the response object.
HttpWResp->Close();
HttpWebRequest HttpWReq = 
(HttpWebRequest)WebRequest.Create("http://www.contoso.com");

HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
// Insert code that uses the response object.
HttpWResp.Close();
Dim HttpWReq As HttpWebRequest = _
   CType(WebRequest.Create("http://www.contoso.com"), HttpWebRequest)
       
Dim HttpWResp As HttpWebResponse = _
   CType(HttpWReq.GetResponse(), HttpWebResponse)
' Insert code that uses the response object.
HttpWResp.Close()

備註

這個類別包含 WebResponse 類別之屬性和方法的 HTTP 特定用法支援。This class contains support for HTTP-specific uses of the properties and methods of the WebResponse class. HttpWebResponse 類別是用來建立 HTTP 獨立用戶端應用程式,以傳送 HTTP 要求和接收 HTTP 回應。The HttpWebResponse class is used to build HTTP stand-alone client applications that send HTTP requests and receive HTTP responses.

注意

請勿將 HttpWebResponse 與用於 ASP.NET 應用程式的 HttpResponse 類別混淆,而且其方法和屬性會透過 ASP 公開。NET 的內建函式 Response 物件。Do not confuse HttpWebResponse with the HttpResponse class that is used in ASP.NET applications and whose methods and properties are exposed through ASP.NET's intrinsic Response object.

您絕對不要直接建立 HttpWebResponse 類別的實例。You should never directly create an instance of the HttpWebResponse class. 相反地,請使用呼叫所傳回的實例 HttpWebRequest.GetResponseInstead, use the instance returned by a call to HttpWebRequest.GetResponse. 您必須呼叫 Stream.CloseHttpWebResponse.Close 方法,才能關閉回應並釋放連線以供重複使用。You must call either the Stream.Close or the HttpWebResponse.Close method to close the response and release the connection for reuse. 不需要同時呼叫 Stream.CloseHttpWebResponse.Close,但這麼做並不會造成錯誤。It is not necessary to call both Stream.Close and HttpWebResponse.Close, but doing so does not cause an error.

從網際網路資源傳回的通用標頭資訊會公開為類別的屬性。Common header information returned from the Internet resource is exposed as properties of the class. 如需完整清單,請參閱下表。See the following table for a complete list. 其他標頭可以從 Headers 屬性讀取為名稱/值組。Other headers can be read from the Headers property as name/value pairs.

下表顯示可透過 HttpWebResponse 類別的屬性取得的一般 HTTP 標頭。The following table shows the common HTTP headers that are available through properties of the HttpWebResponse class.

頁首Header 屬性Property
內容編碼Content-Encoding ContentEncoding
內容-長度Content-Length ContentLength
Content-TypeContent-Type ContentType
上次修改時間Last-Modified LastModified
伺服器Server Server

來自網際網路資源的回應內容會藉由呼叫 GetResponseStream 方法,以 Stream 傳回。The contents of the response from the Internet resource are returned as a Stream by calling the GetResponseStream method.

建構函式

HttpWebResponse()

初始化 HttpWebResponse 類別的新執行個體。Initializes a new instance of the HttpWebResponse class.

HttpWebResponse(SerializationInfo, StreamingContext)

從指定的 HttpWebResponseSerializationInfo 執行個體,初始化 StreamingContext 類別的新執行個體。Initializes a new instance of the HttpWebResponse class from the specified SerializationInfo and StreamingContext instances.

屬性

CharacterSet

取得回應的字元集。Gets the character set of the response.

ContentEncoding

取得用來將回應主體編碼的方法。Gets the method that is used to encode the body of the response.

ContentLength

取得由要求傳回的內容長度。Gets the length of the content returned by the request.

ContentType

取得回應的內容類型。Gets the content type of the response.

Cookies

取得或設定與這個回應關聯的 Cookie。Gets or sets the cookies that are associated with this response.

Headers

取得與伺服器的這個回應關聯的標頭。Gets the headers that are associated with this response from the server.

IsFromCache

取得 Boolean 值,指出這個回應是否從快取中取得。Gets a Boolean value that indicates whether this response was obtained from the cache.

(繼承來源 WebResponse)
IsMutuallyAuthenticated

取得 Boolean 值,指出用戶端和伺服器是否都已驗證。Gets a Boolean value that indicates whether both client and server were authenticated.

LastModified

取得最近修改回應內容的日期和時間。Gets the last date and time that the contents of the response were modified.

Method

取得用來傳回回應的方法。Gets the method that is used to return the response.

ProtocolVersion

取得回應使用的 HTTP 通訊協定版本。Gets the version of the HTTP protocol that is used in the response.

ResponseUri

取得回應要求之網際網路資源的 URI。Gets the URI of the Internet resource that responded to the request.

Server

取得傳送回應的伺服器名稱。Gets the name of the server that sent the response.

StatusCode

取得回應的狀態。Gets the status of the response.

StatusDescription

取得隨回應傳回的狀態描述。Gets the status description returned with the response.

SupportsHeaders

取得指出是否支援標頭的值。Gets a value that indicates whether headers are supported.

方法

Close()

關閉回應資料流。Closes the response stream.

CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(繼承來源 MarshalByRefObject)
Dispose()

釋放由 WebResponse 物件使用的 Unmanaged 資源。Releases the unmanaged resources used by the WebResponse object.

(繼承來源 WebResponse)
Dispose(Boolean)

釋放 HttpWebResponse 所使用的 Unmanaged 資源,並選擇性處置 Managed 資源。Releases the unmanaged resources used by the HttpWebResponse, and optionally disposes of the managed resources.

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

傳回 HttpWebResponse 執行個體的雜湊值。Returns a hash value for a HttpWebResponse instance.

GetLifetimeService()

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(繼承來源 MarshalByRefObject)
GetObjectData(SerializationInfo, StreamingContext)

將序列化目標物件所需的資料填入 SerializationInfoPopulates a SerializationInfo with the data needed to serialize the target object.

GetResponseHeader(String)

取得隨回應傳回的標頭內容。Gets the contents of a header that was returned with the response.

GetResponseStream()

取得用來從伺服器讀取回應主體的資料流。Gets the stream that is used to read the body of the response from the server.

GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
InitializeLifetimeService()

取得存留期服務物件,以控制這個執行個體的存留期原則。Obtains a lifetime service object to control the lifetime policy for this instance.

(繼承來源 MarshalByRefObject)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。Creates a shallow copy of the current MarshalByRefObject object.

(繼承來源 MarshalByRefObject)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

明確介面實作

IDisposable.Dispose()

釋放 HttpWebResponse 所使用的所有資源。Releases all resources used by the HttpWebResponse.

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

將這個執行個體序列化為指定的 SerializationInfo 物件。Serializes this instance into the specified SerializationInfo object.

適用於

另請參閱