HttpWebResponse.Headers 属性

定义

获取来自服务器的与此响应关联的标头。Gets the headers that are associated with this response from the server.

public:
 virtual property System::Net::WebHeaderCollection ^ Headers { System::Net::WebHeaderCollection ^ get(); };
public override System.Net.WebHeaderCollection Headers { get; }
member this.Headers : System.Net.WebHeaderCollection
Public Overrides ReadOnly Property Headers As WebHeaderCollection

属性值

一个 WebHeaderCollection,包含与响应一起返回的标头信息。A WebHeaderCollection that contains the header information returned with the response.

异常

已释放当前实例。The current instance has been disposed.

示例

下面的示例将所有响应标头的内容写入控制台。The following example writes the contents of all of the response headers to the console.

// Creates an HttpWebRequest for the specified URL.
HttpWebRequest^ myHttpWebRequest = dynamic_cast<HttpWebRequest^>(WebRequest::Create( url ));

// Sends the HttpWebRequest and waits for response.
HttpWebResponse^ myHttpWebResponse = dynamic_cast<HttpWebResponse^>(myHttpWebRequest->GetResponse());

// Displays all the headers present in the response received from the URI.
Console::WriteLine( "\r\nThe following headers were received in the response:" );

// Displays each header and its key associated with the response.
for ( int i = 0; i < myHttpWebResponse->Headers->Count; ++i )
   Console::WriteLine( "\nHeader Name: {0}, Value : {1}",
      myHttpWebResponse->Headers->Keys[ i ],
      myHttpWebResponse->Headers[ (System::Net::HttpRequestHeader)i ] );

// Releases the resources of the response.
myHttpWebResponse->Close();
// Creates an HttpWebRequest for the specified URL. 
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url); 
// Sends the HttpWebRequest and waits for response.
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); 
                        
// Displays all the headers present in the response received from the URI.
Console.WriteLine("\r\nThe following headers were received in the response:");
// Displays each header and it's key associated with the response.
for(int i=0; i < myHttpWebResponse.Headers.Count; ++i)  
    Console.WriteLine("\nHeader Name:{0}, Value :{1}",myHttpWebResponse.Headers.Keys[i],myHttpWebResponse.Headers[i]); 
// Releases the resources of the response.
myHttpWebResponse.Close(); 
' Creates an HttpWebRequest with the specified URL. 
Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)
' Sends the HttpWebRequest and waits for a response.
Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
' Displays all the Headers present in the response received from the URI.
Console.WriteLine(ControlChars.Lf + ControlChars.Cr + "The following headers were received in the response")
'The Headers property is a WebHeaderCollection. Use it's properties to traverse the collection and display each header.
Dim i As Integer
While i < myHttpWebResponse.Headers.Count
    Console.WriteLine(ControlChars.Cr + "Header Name:{0}, Value :{1}", myHttpWebResponse.Headers.Keys(i), myHttpWebResponse.Headers(i))
  i = i + 1
End While
myHttpWebResponse.Close()

注解

Headers 属性是名称/值对的集合,其中包含与响应一起返回的 HTTP 标头值。The Headers property is a collection of name/value pairs that contain the HTTP header values returned with the response. 从 Internet 资源返回的公共标头信息作为 HttpWebResponse 类的属性公开。Common header information returned from the Internet resource is exposed as properties of the HttpWebResponse class. 下表列出了 API 作为属性公开的常用标头。The following table lists common headers that the API exposes as properties.

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

适用于