WebHeaderCollection.Set 메서드

정의

지정된 헤더를 지정된 값으로 설정합니다.

오버로드

Set(HttpRequestHeader, String)

지정된 헤더를 지정된 값으로 설정합니다.

Set(HttpResponseHeader, String)

지정된 헤더를 지정된 값으로 설정합니다.

Set(String, String)

지정된 헤더를 지정된 값으로 설정합니다.

Set(HttpRequestHeader, String)

Source:
WebHeaderCollection.cs
Source:
WebHeaderCollection.cs
Source:
WebHeaderCollection.cs

지정된 헤더를 지정된 값으로 설정합니다.

public:
 void Set(System::Net::HttpRequestHeader header, System::String ^ value);
public void Set (System.Net.HttpRequestHeader header, string? value);
public void Set (System.Net.HttpRequestHeader header, string value);
override this.Set : System.Net.HttpRequestHeader * string -> unit
Public Sub Set (header As HttpRequestHeader, value As String)

매개 변수

header
HttpRequestHeader

설정할 HttpRequestHeader 값입니다.

value
String

설정할 헤더의 내용입니다.

예외

.NET Framework 및 .NET Core 버전 2.0 - 3.1만 해당: value의 길이가 65,535자를 넘는 경우

WebHeaderCollection 인스턴스에서 HttpRequestHeader의 인스턴스를 사용할 수 없는 경우

설명

헤더에 지정된 헤더가 없으면 메서드는 Set 헤더 이름/값 쌍 목록에 새 헤더를 삽입합니다.

header 지정된 헤더가 이미 있는 value 경우 는 기존 값을 바꿉니다.

참고

value 길이는 .NET Framework 및 .NET Core 버전 2.0 - 3.1에서만 유효성이 검사됩니다.

적용 대상

Set(HttpResponseHeader, String)

Source:
WebHeaderCollection.cs
Source:
WebHeaderCollection.cs
Source:
WebHeaderCollection.cs

지정된 헤더를 지정된 값으로 설정합니다.

public:
 void Set(System::Net::HttpResponseHeader header, System::String ^ value);
public void Set (System.Net.HttpResponseHeader header, string? value);
public void Set (System.Net.HttpResponseHeader header, string value);
override this.Set : System.Net.HttpResponseHeader * string -> unit
Public Sub Set (header As HttpResponseHeader, value As String)

매개 변수

header
HttpResponseHeader

설정할 HttpResponseHeader 값입니다.

value
String

설정할 헤더의 내용입니다.

예외

.NET Framework 및 .NET Core 버전 2.0 - 3.1만 해당: value의 길이가 65,535자를 넘는 경우

WebHeaderCollection 인스턴스에서 HttpResponseHeader의 인스턴스를 사용할 수 없는 경우

설명

헤더에 지정된 헤더가 없으면 메서드는 Set 헤더 이름/값 쌍 목록에 새 헤더를 삽입합니다.

header 지정된 헤더가 이미 있는 value 경우 는 기존 값을 바꿉니다.

참고

value 길이는 .NET Framework 및 .NET Core 버전 2.0 - 3.1에서만 유효성이 검사됩니다.

적용 대상

Set(String, String)

Source:
WebHeaderCollection.cs
Source:
WebHeaderCollection.cs
Source:
WebHeaderCollection.cs

지정된 헤더를 지정된 값으로 설정합니다.

public:
 override void Set(System::String ^ name, System::String ^ value);
public override void Set (string name, string? value);
public override void Set (string name, string value);
override this.Set : string * string -> unit
Public Overrides Sub Set (name As String, value As String)

매개 변수

name
String

설정할 헤더입니다.

value
String

설정할 헤더의 내용입니다.

예외

namenull 또는 Empty입니다.

.NET Framework 및 .NET Core 버전 2.0 - 3.1만 해당: value의 길이가 65,535자를 넘는 경우

name이 제한된 헤더인 경우

또는

name 또는 value에 잘못된 문자가 들어 있는 경우

예제

다음 예제에서는 메서드를 Set 사용하여 기존 헤더의 값을 설정합니다.

try
{
   // Create a web request for S"www.msn.com".
   HttpWebRequest^ myHttpWebRequest = dynamic_cast<HttpWebRequest^>(WebRequest::Create( "http://www.msn.com" ));

   // Get the headers associated with the request.
   WebHeaderCollection^ myWebHeaderCollection = myHttpWebRequest->Headers;

   // Set the Cache-Control header in the request.
   myWebHeaderCollection->Set( "Cache-Control", "no-cache" );

   // Get the associated response for the above request.
   HttpWebResponse^ myHttpWebResponse = dynamic_cast<HttpWebResponse^>(myHttpWebRequest->GetResponse());
   Console::WriteLine( "Headers after 'Set' method is used on Cache-Control :" );

   // Print the headers for the request.
   PrintHeaders( myWebHeaderCollection );
   myHttpWebResponse->Close();
}
// Catch exception if trying to set a restricted header.
catch ( ArgumentException^ e ) 
{
   Console::WriteLine( "ArgumentException is thrown. Message is : {0}", e->Message );
}
catch ( WebException^ e ) 
{
   Console::WriteLine( "WebException is thrown. Message is : {0}", e->Message );
   if ( e->Status == WebExceptionStatus::ProtocolError )
   {
      Console::WriteLine( "Status Code : {0}", (dynamic_cast<HttpWebResponse^>(e->Response))->StatusCode );
      Console::WriteLine( "Status Description : {0}", (dynamic_cast<HttpWebResponse^>(e->Response))->StatusDescription );
      Console::WriteLine( "Server : {0}", (dynamic_cast<HttpWebResponse^>(e->Response))->Server );
   }
}
catch ( Exception^ e ) 
{
   Console::WriteLine( "Exception is thrown. Message is : {0}", e->Message );
}
try {
    // Create a web request for "www.msn.com".
    HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest.Create("http://www.msn.com");

    // Get the headers associated with the request.
    WebHeaderCollection myWebHeaderCollection = myHttpWebRequest.Headers;

    // Set the Cache-Control header in the request.
    myWebHeaderCollection.Set("Cache-Control", "no-cache");

    // Get the associated response for the above request.
    HttpWebResponse myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse();

    Console.WriteLine ("Headers after 'Set' method is used on Cache-Control :");
    // Print the headers for the request.
    PrintHeaders(myWebHeaderCollection);
    myHttpWebResponse.Close();
}
// Catch exception if trying to set a restricted header.
catch(ArgumentException e) {
    Console.WriteLine("ArgumentException is thrown. Message is :" + e.Message);
}
catch(WebException e) {
    Console.WriteLine("WebException is thrown. Message is :" + e.Message);
    if(e.Status == WebExceptionStatus.ProtocolError) {
        Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
        Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
        Console.WriteLine("Server : {0}", ((HttpWebResponse)e.Response).Server);
    }
}
catch(Exception e) {
    Console.WriteLine("Exception is thrown. Message is :" + e.Message);
}
Public Shared Sub Main()

Try
        'Create a web request for "www.msn.com".
        Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create("http://www.msn.com"), HttpWebRequest)
        
        'Get the headers associated with the request.
        Dim myWebHeaderCollection As WebHeaderCollection = myHttpWebRequest.Headers
        
        'Set the Cache-Control header in the request.
        myWebHeaderCollection.Set("Cache-Control", "no-cache")

        'Get the associated response for the above request.
        Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
        
    Console.WriteLine ("Headers after 'Set' method is used on Cache-Control :")
        'Print the headers for the request.
        PrintHeaders(myWebHeaderCollection)
        myHttpWebResponse.Close()
   'Catch exception if trying to set a restricted header.
    Catch e As ArgumentException
        Console.WriteLine(e.Message)
    Catch e As WebException
        Console.WriteLine(e.Message)
        If e.Status = WebExceptionStatus.ProtocolError Then
            Console.WriteLine("Status Code : {0}", CType(e.Response, HttpWebResponse).StatusCode)
            Console.WriteLine("Status Description : {0}", CType(e.Response, HttpWebResponse).StatusDescription)
            Console.WriteLine("Server : {0}", CType(e.Response, HttpWebResponse).Server)
        End If
    Catch e As Exception
        Console.WriteLine(e.Message)
    End Try
End Sub

참고

value 길이는 .NET Framework 및 .NET Core 버전 2.0 - 3.1에서만 유효성이 검사됩니다.

설명

헤더에 지정된 헤더가 없으면 메서드는 Set 헤더 이름/값 쌍 목록에 새 헤더를 삽입합니다.

header 지정된 헤더가 이미 있는 value 경우 는 기존 값을 바꿉니다.

적용 대상