WebClient.Headers 속성

정의

요청과 관련된 헤더 이름/값 쌍의 컬렉션을 가져오거나 설정합니다.

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

속성 값

이 요청과 관련된 헤더 이름/값 쌍이 들어 있는 WebHeaderCollection입니다.

예제

다음 코드 예제에서는 컬렉션을 사용하여 Headers HTTP Content-Type 헤더를 로 application/x-www-form-urlencoded, 설정하여 양식 데이터가 게시물에 연결되어 있음을 서버에 알립니다.

String^ uriString;
Console::Write( "\nPlease enter the URI to post data to {for example, http://www.contoso.com}: " );
uriString = Console::ReadLine();

// Create a new WebClient instance.
WebClient^ myWebClient = gcnew WebClient;
Console::WriteLine( "\nPlease enter the data to be posted to the URI {0}:", uriString );
String^ postData = Console::ReadLine();
myWebClient->Headers->Add( "Content-Type", "application/x-www-form-urlencoded" );

// Displays the headers in the request
Console::Write( "Resulting Request Headers: ");
Console::WriteLine(myWebClient->Headers);

// Apply ASCII Encoding to obtain the String^ as a Byte array.
array<Byte>^ byteArray = Encoding::ASCII->GetBytes( postData );
Console::WriteLine( "Uploading to {0} ...", uriString );
// Upload the input String* using the HTTP 1.0 POST method.
array<Byte>^responseArray = myWebClient->UploadData( uriString, "POST", byteArray );
// Decode and display the response.
Console::WriteLine( "\nResponse received was {0}",
   Encoding::ASCII->GetString( responseArray ) );
   string uriString;
       Console.Write("\nPlease enter the URI to post data to {for example, http://www.contoso.com} : ");
       uriString = Console.ReadLine();

       // Create a new WebClient instance.
       WebClient myWebClient = new WebClient();
       Console.WriteLine("\nPlease enter the data to be posted to the URI {0}:",uriString);
       string postData = Console.ReadLine();
       myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded");

 // Display the headers in the request
       Console.Write("Resulting Request Headers: ");
       Console.WriteLine(myWebClient.Headers.ToString());
       
       // Apply ASCII Encoding to obtain the string as a byte array.

       byte[] byteArray = Encoding.ASCII.GetBytes(postData);
       Console.WriteLine("Uploading to {0} ...",  uriString);						
       // Upload the input string using the HTTP 1.0 POST method.
       byte[] responseArray = myWebClient.UploadData(uriString,"POST",byteArray);
       
       // Decode and display the response.
       Console.WriteLine("\nResponse received was {0}",
       Encoding.ASCII.GetString(responseArray));
                 
Dim uriString As String
Console.Write(ControlChars.Cr + "Please enter the URI to post data to{for example, http://www.contoso.com} : ")
uriString = Console.ReadLine()

' Create a new WebClient instance.
Dim myWebClient As New WebClient()
Console.WriteLine(ControlChars.Cr + "Please enter the data to be posted to the URI {0}:", uriString)
Dim postData As String = Console.ReadLine()
myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded")

' Display the headers in the request
Console.Write("Resulting Request Headers: ")
Console.Writeline(myWebClient.Headers.ToString())

' Apply ASCII Encoding to obtain the string as a byte array.
Dim byteArray As Byte() = Encoding.ASCII.GetBytes(postData)
Console.WriteLine("Uploading to {0} ...", uriString)
' Upload the input string using the HTTP 1.0 POST method.
Dim responseArray As Byte() = myWebClient.UploadData(uriString, "POST", byteArray)
' Decode and display the response.
Console.WriteLine(ControlChars.Cr + "Response received was :{0}", Encoding.ASCII.GetString(responseArray))

설명

속성에는 Headers 요청과 WebHeaderCollection 함께 보내는 프로토콜 헤더가 WebClient 포함된 instance 포함되어 있습니다.

일부 일반적인 헤더는 제한된 것으로 간주되며 시스템에서 보호되며 개체에서 WebHeaderCollection 설정하거나 변경할 수 없습니다. 개체와 WebClient 연결된 개체에서 WebHeaderCollection 이러한 제한된 헤더 중 하나를 설정하려고 하면 나중에 요청을 보내려고 할 때 예외가 WebClient throw됩니다.

시스템에 의해 보호되는 제한된 헤더는 포함되지만 다음으로 제한되지는 않습니다.

  • 날짜

  • 호스트

또한 개체를 사용할 WebClient 때 다른 헤더도 제한됩니다. 이러한 제한된 헤더에는 다음이 포함되지만 이에 국한되지는 않습니다.

  • 동의함

  • 연결

  • Content-Length

  • 예상(값이 "100-continue"로 설정된 경우)

  • If-Modified-Since

  • 범위

  • Transfer-Encoding

클래스에는 HttpWebRequest 위의 헤더 중 일부를 설정하기 위한 속성이 있습니다. 이러한 헤더를 설정 하는 애플리케이션에 대 한 중요 한 경우 해당 HttpWebRequest 클래스를 대신 사용 해야는 WebRequest 클래스입니다.

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

적용 대상

추가 정보