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 应用程序中使用并且其方法和属性通过 ASP 公开的 HttpResponse 类混淆。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.GetResponse返回的实例。Instead, 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.

从 Internet 资源返回的公共标头信息作为类的属性公开。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.

HeaderHeader 属性Property
Content-EncodingContent-Encoding ContentEncoding
Content-lengthContent-Length ContentLength
Content-TypeContent-Type ContentType
上次修改时间Last-Modified LastModified
服务器Server Server

来自 Internet 资源的响应内容将作为 Stream 通过调用 GetResponseStream 方法返回。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)

根据指定的 SerializationInfoStreamingContext 实例初始化 HttpWebResponse 类的新实例。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

获取响应请求的 Internet 资源的 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)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(继承自 MarshalByRefObject)
Dispose()

释放 WebResponse 对象使用的非托管资源。Releases the unmanaged resources used by the WebResponse object.

(继承自 WebResponse)
Dispose(Boolean)

释放由 HttpWebResponse 使用的非托管资源,并可根据需要释放托管资源。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()

检索控制此实例的生存期策略的当前生存期服务对象。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.

适用于

另请参阅