다음을 통해 공유


WebHeaderCollection.Add 메서드

정의

컬렉션에 새 헤더를 삽입합니다.

오버로드

Name Description
Add(String)

지정된 헤더를 컬렉션에 삽입합니다.

Add(HttpRequestHeader, String)

지정된 값이 있는 지정된 헤더를 컬렉션에 삽입합니다.

Add(HttpResponseHeader, String)

지정된 값이 있는 지정된 헤더를 컬렉션에 삽입합니다.

Add(String, String)

지정된 이름과 값을 가진 헤더를 컬렉션에 삽입합니다.

Add(String)

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

지정된 헤더를 컬렉션에 삽입합니다.

public:
 void Add(System::String ^ header);
public void Add(string header);
override this.Add : string -> unit
Public Sub Add (header As String)

매개 변수

header
String

콜론으로 구분된 이름 및 값을 사용하여 추가할 헤더입니다.

예외

header is null 또는 Empty.

header 에는 콜론(:) 문자가 포함되어 있지 않습니다.

-또는-

값 부분 header 의 길이가 65535보다 큽다.

-또는-

이름 부분이 headerEmpty 잘못되었거나 잘못된 문자가 포함되어 있습니다.

-또는-

header 는 속성으로 설정해야 하는 제한된 헤더입니다.

-또는-

값 부분에 header 잘못된 문자가 포함되어 있습니다.

.NET Framework 및 .NET Core 버전 2.0 - 3.1만 해당: 콜론(:) 이후의 문자열 길이가 65535보다 큽 수 있습니다.

예제

다음 예제에서는 메서드를 사용하여 이름/값 쌍을 WebHeaderCollectionAdd 추가합니다.

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;

    Console.WriteLine("Configuring Webrequest to accept Danish and English language using 'Add' method");

    //Add the Accept-Language header (for Danish) in the request.
    myWebHeaderCollection.Add("Accept-Language:da");

    //Include English in the Accept-Langauge header. 
    myWebHeaderCollection.Add("Accept-Language","en;q=0.8");

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

    //Print the headers for the request.
    printHeaders(myWebHeaderCollection);
    myHttpWebResponse.Close();
}
//Catch exception if trying to add a restricted header.
catch(ArgumentException e) {
    Console.WriteLine(e.Message);
}
catch(WebException e) {
    Console.WriteLine("\nWebException is thrown. \nMessage 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
        
    Console.WriteLine("Configuring Webrequest to accept Danish and English language using 'Add' method")
        
    'Add the Accept-Language header (for Danish) in the request.
        myWebHeaderCollection.Add("Accept-Language:da")
        
        'Include English in the Accept-Langauge header. 
        myWebHeaderCollection.Add("Accept-Language","en;q" + ChrW(61) + "0.8")
        
        'Get the associated response for the above request.
        Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
        
        'Print the headers for the request.
        printHeaders(myWebHeaderCollection)
        myHttpWebResponse.Close()
    'Catch exception if trying to add 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

메모

콜론(:) 뒤의 문자열인 부분의 header길이는 .NET Framework 및 .NET Core 버전 2.0 - 3.1에서만 유효성을 검사합니다.

설명

매개 변수는 header "name:value" 형식으로 지정해야 합니다. 지정된 헤더가 컬렉션에 없으면 컬렉션에 새 헤더가 추가됩니다.

지정된 header 헤더가 컬렉션에 이미 있는 경우 값 부분은 header 기존 값과 연결됩니다.

적용 대상

Add(HttpRequestHeader, String)

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

지정된 값이 있는 지정된 헤더를 컬렉션에 삽입합니다.

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

매개 변수

header
HttpRequestHeader

컬렉션에 추가할 헤더입니다.

value
String

헤더의 내용입니다.

예외

.NET Framework 및 .NET Core 버전 2.0 - 3.1만 해당: 길이 value 가 65535보다 큽다.

WebHeaderCollection 인스턴스는 .의 HttpRequestHeader인스턴스를 허용하지 않습니다.

설명

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

지정된 헤더가 이미 있는 경우 헤더 value 와 연결된 값의 쉼표로 구분된 목록에 추가됩니다.

메모

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

적용 대상

Add(HttpResponseHeader, String)

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

지정된 값이 있는 지정된 헤더를 컬렉션에 삽입합니다.

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

매개 변수

header
HttpResponseHeader

컬렉션에 추가할 헤더입니다.

value
String

헤더의 내용입니다.

예외

.NET Framework 및 .NET Core 버전 2.0 - 3.1만 해당: 길이 value 가 65535보다 큽다.

WebHeaderCollection 인스턴스는 .의 HttpResponseHeader인스턴스를 허용하지 않습니다.

설명

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

지정된 헤더가 이미 있는 경우 헤더 value 와 연결된 값의 쉼표로 구분된 목록에 추가됩니다.

메모

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

적용 대상

Add(String, String)

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

지정된 이름과 값을 가진 헤더를 컬렉션에 삽입합니다.

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

매개 변수

name
String

컬렉션에 추가할 헤더입니다.

value
String

헤더의 내용입니다.

예외

namenull있거나 Empty잘못된 문자를 포함합니다.

-또는-

name 는 속성 설정으로 설정해야 하는 제한된 헤더입니다.

-또는-

value 에 잘못된 문자가 포함되어 있습니다.

.NET Framework 및 .NET Core 버전 2.0 - 3.1만 해당: 길이 value 가 65535보다 큽다.

예제

다음 예제에서는 메서드를 사용하여 이름/값 쌍을 WebHeaderCollectionAdd 추가합니다.

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;

    Console.WriteLine("Configuring Webrequest to accept Danish and English language using 'Add' method");

    //Add the Accept-Language header (for Danish) in the request.
    myWebHeaderCollection.Add("Accept-Language:da");

    //Include English in the Accept-Langauge header. 
    myWebHeaderCollection.Add("Accept-Language","en;q=0.8");

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

    //Print the headers for the request.
    printHeaders(myWebHeaderCollection);
    myHttpWebResponse.Close();
}
//Catch exception if trying to add a restricted header.
catch(ArgumentException e) {
    Console.WriteLine(e.Message);
}
catch(WebException e) {
    Console.WriteLine("\nWebException is thrown. \nMessage 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
        
    Console.WriteLine("Configuring Webrequest to accept Danish and English language using 'Add' method")
        
    'Add the Accept-Language header (for Danish) in the request.
        myWebHeaderCollection.Add("Accept-Language:da")
        
        'Include English in the Accept-Langauge header. 
        myWebHeaderCollection.Add("Accept-Language","en;q" + ChrW(61) + "0.8")
        
        'Get the associated response for the above request.
        Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
        
        'Print the headers for the request.
        printHeaders(myWebHeaderCollection)
        myHttpWebResponse.Close()
    'Catch exception if trying to add 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에서만 유효성을 검사합니다.

설명

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

지정된 name 헤더가 이미 있는 value 경우 연결된 값의 기존 쉼표로 구분된 목록에 추가됩니다 name.

적용 대상