Aracılığıyla paylaş


WebHeaderCollection.Add Yöntem

Tanım

Koleksiyona yeni bir üst bilgi ekler.

Aşırı Yüklemeler

Add(String)

Belirtilen üst bilgiyi koleksiyona ekler.

Add(HttpRequestHeader, String)

Belirtilen değeri içeren belirtilen üst bilgiyi koleksiyona ekler.

Add(HttpResponseHeader, String)

Belirtilen değeri içeren belirtilen üst bilgiyi koleksiyona ekler.

Add(String, String)

Koleksiyona belirtilen ad ve değere sahip bir üst bilgi ekler.

Add(String)

Kaynak:
WebHeaderCollection.cs
Kaynak:
WebHeaderCollection.cs
Kaynak:
WebHeaderCollection.cs

Belirtilen üst bilgiyi koleksiyona ekler.

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

Parametreler

header
String

Ad ve değer iki nokta üst üste ile ayrılmış olarak eklenecek üst bilgi.

Özel durumlar

headerveya Emptyşeklindedirnull.

header iki nokta üst üste (:) karakter içermez.

uzunluğu value 65535'ten büyüktür.

-veya-

öğesinin header ad bölümü veya Empty geçersiz karakterler içeriyor.

-veya-

header , bir özellik ile ayarlanması gereken kısıtlı bir üst bilgidir.

-veya-

değerinin header değer bölümü geçersiz karakterler içeriyor.

.NET Framework ve .NET Core yalnızca 2.0 - 3.1 sürümlerini içerir: İki nokta üst üste (:) 65535'ten büyük) sonraki dizenin uzunluğu.

Örnekler

Aşağıdaki örnek, Metodu kullanarak öğesine bir WebHeaderCollection ad/değer çifti Add ekler.

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;
   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 = dynamic_cast<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 : {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;

    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

Not

değerininheaderdeğer bölümünün uzunluğu, yani iki nokta üst üste (:)) sonra gelen dize yalnızca .NET Framework ve .NET Core sürüm 2.0 - 3.1'de doğrulanır.

Açıklamalar

Parametre " header name:value" biçiminde belirtilmelidir. Belirtilen üst bilgi koleksiyonda yoksa, koleksiyona yeni bir üst bilgi eklenir.

içinde header belirtilen üst bilgi koleksiyonda zaten varsa, öğesinin header değer bölümü var olan değerle birleştirilir.

Şunlara uygulanır

Add(HttpRequestHeader, String)

Kaynak:
WebHeaderCollection.cs
Kaynak:
WebHeaderCollection.cs
Kaynak:
WebHeaderCollection.cs

Belirtilen değeri içeren belirtilen üst bilgiyi koleksiyona ekler.

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)

Parametreler

header
HttpRequestHeader

Koleksiyona eklenecek üst bilgi.

value
String

Üst bilginin içeriği.

Özel durumlar

.NET Framework ve .NET Core sürümleri yalnızca 2.0 - 3.1: uzunluğu value 65535'ten büyüktür.

Açıklamalar

Belirtilen üst bilgi yoksa, Add yöntemi üst bilgi adı/değer çiftleri listesine yeni bir üst bilgi ekler.

Belirtilen üst bilgi zaten varsa, value üst bilgiyle ilişkili değerlerin virgülle ayrılmış listesine eklenir.

Not

uzunluğu value yalnızca .NET Framework ve .NET Core 2.0 - 3.1 sürümlerinde doğrulanır.

Şunlara uygulanır

Add(HttpResponseHeader, String)

Kaynak:
WebHeaderCollection.cs
Kaynak:
WebHeaderCollection.cs
Kaynak:
WebHeaderCollection.cs

Belirtilen değeri içeren belirtilen üst bilgiyi koleksiyona ekler.

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)

Parametreler

header
HttpResponseHeader

Koleksiyona eklenecek üst bilgi.

value
String

Üst bilginin içeriği.

Özel durumlar

.NET Framework ve .NET Core sürümleri yalnızca 2.0 - 3.1: uzunluğu value 65535'ten büyüktür.

Açıklamalar

Belirtilen üst bilgi yoksa, Add yöntemi üst bilgi adı/değer çiftleri listesine yeni bir üst bilgi ekler.

Belirtilen üst bilgi zaten varsa, value üst bilgiyle ilişkili değerlerin virgülle ayrılmış listesine eklenir.

Not

uzunluğu value yalnızca .NET Framework ve .NET Core 2.0 - 3.1 sürümlerinde doğrulanır.

Şunlara uygulanır

Add(String, String)

Kaynak:
WebHeaderCollection.cs
Kaynak:
WebHeaderCollection.cs
Kaynak:
WebHeaderCollection.cs

Koleksiyona belirtilen ad ve değere sahip bir üst bilgi ekler.

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)

Parametreler

name
String

Koleksiyona eklenecek üst bilgi.

value
String

Üst bilginin içeriği.

Özel durumlar

namenull, Emptyveya geçersiz karakterler içeriyor.

-veya-

name , bir özellik ayarıyla ayarlanması gereken kısıtlı bir üst bilgidir.

-veya-

value geçersiz karakterler içeriyor.

.NET Framework ve .NET Core sürümleri yalnızca 2.0 - 3.1: uzunluğu value 65535'ten büyüktür.

Örnekler

Aşağıdaki örnek, Metodu kullanarak öğesine bir WebHeaderCollection ad/değer çifti Add ekler.

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;
   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 = dynamic_cast<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 : {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;

    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

Not

uzunluğu value yalnızca .NET Framework ve .NET Core 2.0 - 3.1 sürümlerinde doğrulanır.

Açıklamalar

içinde name belirtilen üst bilgi yoksa, Add yöntemi üst bilgi adı/değer çiftleri listesine yeni bir üst bilgi ekler.

içinde name belirtilen üst bilgi zaten varsa, value ile ilişkilendirilmiş değerlerin virgülle nameayrılmış var olan listesine eklenir.

Şunlara uygulanır