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

속성 값

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 통해 GetRequestStreamGetResponseBeginGetRequestStream요청이 시작된 후 속성을 변경하거나 BeginGetResponse 메서드가 .를 InvalidOperationExceptionthrow합니다.

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

적용 대상