HttpWebRequest.Headers 속성

정의

HTTP 헤더를 구성하는 이름/값 쌍의 컬렉션을 지정합니다.

public:
 virtual property System::Net::WebHeaderCollection ^ Headers { System::Net::WebHeaderCollection ^ get(); void set(System::Net::WebHeaderCollection ^ value); };
public override System.Net.WebHeaderCollection Headers { get; set; }
member this.Headers : System.Net.WebHeaderCollection with get, set
Public Overrides Property Headers As WebHeaderCollection

속성 값

HTTP 요청의 헤더를 구성하는 이름/값 쌍이 들어 있는 WebHeaderCollection입니다.

예외

예제

다음 코드 예제를 사용 하 Headers 여는 HTTP 헤더 이름/값 쌍을 콘솔에 인쇄 하는 속성입니다.

// Create a new 'HttpWebRequest' Object to the mentioned URL.
HttpWebRequest^ myHttpWebRequest = (HttpWebRequest^)( WebRequest::Create( "http://www.contoso.com" ) );
// Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
HttpWebResponse^ myHttpWebResponse = (HttpWebResponse^)( myHttpWebRequest->GetResponse() );
Console::WriteLine( "\nThe HttpHeaders are \n\n\tName\t\tValue\n {0}", myHttpWebRequest->Headers );
// Print the HTML contents of the page to the console.
Stream^ streamResponse = myHttpWebResponse->GetResponseStream();
StreamReader^ streamRead = gcnew StreamReader( streamResponse );
array<Char>^ readBuff = gcnew array<Char>(256);
int count = streamRead->Read( readBuff, 0, 256 );
Console::WriteLine( "\nThe HTML contents of page the are  : \n\n " );
while ( count > 0 )
{
   String^ outputData = gcnew String( readBuff,0,count );
   Console::Write( outputData );
   count = streamRead->Read( readBuff, 0, 256 );
}
streamResponse->Close();
streamRead->Close();
// Release the HttpWebResponse Resource.
myHttpWebResponse->Close();
// Create a new 'HttpWebRequest' Object to the mentioned URL.
HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create("http://www.contoso.com");
// Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
Console.WriteLine("\nThe HttpHeaders are \n\n\tName\t\tValue\n{0}",myHttpWebRequest.Headers);
// Print the HTML contents of the page to the console.
Stream streamResponse=myHttpWebResponse.GetResponseStream();
StreamReader streamRead = new StreamReader( streamResponse );
Char[] readBuff = new Char[256];
int count = streamRead.Read( readBuff, 0, 256 );
Console.WriteLine("\nThe HTML contents of page the are  : \n\n ");	
while (count > 0)
{
    String outputData = new String(readBuff, 0, count);
    Console.Write(outputData);
    count = streamRead.Read(readBuff, 0, 256);
}
// Close the Stream object.
streamResponse.Close();
streamRead.Close();
// Release the HttpWebResponse Resource.
myHttpWebResponse.Close();
     ' Create a new 'HttpWebRequest' Object to the mentioned URL.
     Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create("http://www.contoso.com"), HttpWebRequest)
     ' Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
     Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
     Console.WriteLine(ControlChars.Cr + "The HttpHeaders are " + ControlChars.Cr + ControlChars.Cr + ControlChars.Tab + "Name" + ControlChars.Tab + ControlChars.Tab + "Value" + ControlChars.Cr + "{0}", myHttpWebRequest.Headers)

     ' Print the HTML contents of the page to the console. 
     Dim streamResponse As Stream = myHttpWebResponse.GetResponseStream()
     Dim streamRead As New StreamReader(streamResponse)
     Dim readBuff(256) As [Char]
     Dim count As Integer = streamRead.Read(readBuff, 0, 256)
     Console.WriteLine(ControlChars.Cr + "The HTML contents of page the are  : " + ControlChars.Cr + ControlChars.Cr + " ")
     While count > 0
         Dim outputData As New [String](readBuff, 0, count)
         Console.Write(outputData)
         count = streamRead.Read(readBuff, 0, 256)
     End While
' Close the Stream object.
streamResponse.Close()
streamRead.Close()
' Release the HttpWebResponse Resource.
 myHttpWebResponse.Close()

설명

컬렉션에는 Headers 요청과 연결된 프로토콜 헤더가 포함되어 있습니다. 다음 표에서는 컬렉션에 Headers 저장되지 않지만 시스템에 의해 설정되거나 속성 또는 메서드로 설정된 HTTP 헤더를 나열합니다.

헤더 설정 기준
동의함 속성으로 Accept 설정합니다.
연결 속성 및 KeepAlive 속성으로 Connection 설정합니다.
Content-Length 속성으로 ContentLength 설정합니다.
콘텐츠 형식 속성으로 ContentType 설정합니다.
Expect 속성으로 Expect 설정합니다.
날짜 속성으로 Date 설정합니다.
호스트 속성으로 Host 설정합니다.
If-Modified-Since 속성으로 IfModifiedSince 설정합니다.
범위 메서드에 의해 설정됩니다 AddRange .
Referer 속성으로 Referer 설정합니다.
Transfer-Encoding 속성으로 TransferEncoding 설정합니다(속성은 SendChunked true여야 합니다).
User-Agent 속성으로 UserAgent 설정합니다.

Add 이러한 보호된 헤더 중 하나를 설정하려고 하면 메서드가 을 throw ArgumentException 합니다.

Headers, , BeginGetRequestStreamGetResponse또는 BeginGetResponse 메서드를 호출GetRequestStream하여 요청이 시작된 후 속성을 변경하면 가 throw됩니다InvalidOperationException.

웹 서버와 캐시가 웹 요청에 헤더를 변경하거나 추가할 수 있으므로 헤더 값이 변경되지 않은 상태로 유지된다고 가정해서는 안 됩니다.

적용 대상