System.Net.WebHeaderCollection Class

public class WebHeaderCollection : NameValueCollection

Base Types

Object
  NameValueCollection
    WebHeaderCollection

This type implements ICollection and IEnumerable.

Assembly

System

Library

Networking

Summary

Contains protocol headers associated with a WebRequest or WebResponse instance.

Description

This class is generally accessed through System.Net.WebRequest.Headers or System.Net.WebResponse.Headers .

Certain protocol headers are protected and cannot be set directly in a WebHeaderCollection instance. These headers can only be set through provided property accessors or by the system. The protected headers are:

Attributes

DefaultMemberAttribute("Item")

See Also

System.Net Namespace

Members

WebHeaderCollection Constructors

WebHeaderCollection Constructor

WebHeaderCollection Methods

WebHeaderCollection.Add(System.String, System.String) Method
WebHeaderCollection.Add(System.String) Method
WebHeaderCollection.AddWithoutValidate Method
WebHeaderCollection.GetValues Method
WebHeaderCollection.IsRestricted Method
WebHeaderCollection.Remove Method
WebHeaderCollection.Set Method


WebHeaderCollection Constructor

public WebHeaderCollection();

Summary

Constructs a new instance of the WebHeaderCollection class.

See Also

System.Net.WebHeaderCollection Class, System.Net Namespace

WebHeaderCollection.Add(System.String, System.String) Method

public override void Add(string name, string value);

Summary

Inserts a new header with the specified name and value into the collection.

Parameters

name
A String that contains the name of the header to add to the collection.
value
A String that contains the content of the header.

Exceptions

Exception TypeCondition
ArgumentExceptionname is null or System.String.Empty, or contains invalid characters.

-or-

name is a protected header that can only be set with a property accessor or by the system.

-or-

value contains invalid characters.

Description

This method inserts a new header into the list of header name/value pairs.

If the header specified in name is already present, value is concatenated with the existing value.

[Note: This method overrides System.Collections.Specialized.NameValueCollection.Add(System.Collections.Specialized.NameValueCollection).]

See Also

System.Net.WebHeaderCollection Class, System.Net Namespace

WebHeaderCollection.Add(System.String) Method

public void Add(string header);

Summary

Inserts the specified header into the collection.

Parameters

header
A String containing the header to add, with the name and value separated by a colon.

Exceptions

Exception TypeCondition
ArgumentNullExceptionheader is null or System.String.Empty.
ArgumentExceptionheader does not contain a colon (:) character.

-or-

name is System.String.Empty, or contains invalid characters.

-or-

header is a protected header that can only be set with a property accessor or by the system.

-or-

value contains invalid characters.

Description

This method inserts a new header into the list of header name/value pairs. header is required to be specified in the format name:value.

If the header specified in name is already present in the collection, value is concatenated with the existing value.

See Also

System.Net.WebHeaderCollection Class, System.Net Namespace

WebHeaderCollection.AddWithoutValidate Method

protected void AddWithoutValidate(string headerName, string headerValue);

Summary

Inserts a header into the current instance without checking whether the header is on the restricted header list.

Parameters

headerName
A String that contains the name of the header to add to the collection.
headerValue
A String that contains the content of the header.

Exceptions

Exception TypeCondition
ArgumentExceptionheaderName is null or System.String.Empty, or contains invalid characters.

-or-

headerValue contains invalid characters.

Description

This method adds a header to the collection without checking whether the header is on the restricted header list.

[Note: When subclassing WebHeaderCollection, use the System.Net.WebHeaderCollection.AddWithoutValidate(System.String,System.String) method to add headers that are normally exposed through property accessors. ]

See Also

System.Net.WebHeaderCollection Class, System.Net Namespace

WebHeaderCollection.GetValues Method

public override string[] GetValues(string header);

Summary

Returns the values stored in the specified protocol header.

Parameters

header
A String containing the protocol header name whose values are returned.

Return Value

An array of String objects that contain the values of the protocol header named header in the current instance. If header is not found in the current instance, returns null .

Description

[Note: This method overrides System.Collections.Specialized.NameValueCollection.GetValues(System.String).]

Example

This example demonstrates the System.Net.WebHeaderCollection.GetValues(System.String) method.

using System;
using System.Net;

class GetValuesExample
{
   public static void Main()
   {
      Uri contosoUri = new Uri("http://www.contoso.com");
      HttpWebRequest httpContoso = 
         (HttpWebRequest)WebRequest.Create(contosoUri);

      httpContoso.SendChunked=true;
      httpContoso.TransferEncoding="compress";
      httpContoso.TransferEncoding="gzip";
    
      WebHeaderCollection webColl = httpContoso.Headers;
      String[] sAry = webColl.GetValues("Transfer-Encoding");

      Console.WriteLine("Transfer-Encoding:");
      foreach(string s in sAry)
         Console.WriteLine("{0}", s);
   }
}
The output is

Transfer-Encoding:

compress

gzip

See Also

System.Net.WebHeaderCollection Class, System.Net Namespace

WebHeaderCollection.IsRestricted Method

public static bool IsRestricted(string headerName);

Summary

Returns a Boolean value that indicates whether the specified HTTP header can be set.

Parameters

headerName
A String containing the header to test.

Return Value

true if the header is protected; otherwise false .

Exceptions

Exception TypeCondition
ArgumentNullExceptionheaderName is null or System.String.Empty.
ArgumentExceptionheaderName contains invalid characters.

Description

This method returns true to indicate that a header is protected. Protected headers can only be set through provided property accessors or by the system. They cannot be set directly in the current instance.

The protected headers are:

See Also

System.Net.WebHeaderCollection Class, System.Net Namespace

WebHeaderCollection.Remove Method

public override void Remove(string name);

Summary

Removes the specified header from the current instance.

Parameters

name
A String that contains the name of the header to remove from the current instance.

Exceptions

Exception TypeCondition
ArgumentNullExceptionname is null or System.String.Empty.
ArgumentExceptionname contains invalid characters.

-or-

name is a protected header that can only be set with a property accessor or by the system.

Description

This method deletes the specified header from the current instance. If multiple values were added to the same header using System.Net.WebHeaderCollection.Add(System.String,System.String), a single call to System.Net.WebHeaderCollection.Remove(System.String) deletes all of the values.

[Note: This method overrides System.Collections.Specialized.NameValueCollection.Remove(System.String).]

See Also

System.Net.WebHeaderCollection Class, System.Net Namespace

WebHeaderCollection.Set Method

public override void Set(string name, string value);

Summary

Sets the specified header to the specified value.

Parameters

name
A String that contains the name of the header to set.
value
A String that contains the content of the header to set.

Exceptions

Exception TypeCondition
ArgumentNullExceptionname is null or System.String.Empty.
ArgumentExceptionname contains invalid characters.

-or-

name is a protected header that can only be set with a property accessor or by the system.

-or-

value contains invalid characters.

Description

The System.Net.WebHeaderCollection.Set(System.String,System.String) method inserts a new header into the list of header name/value pairs.

If the header specified in name is already present, value replaces the existing value.

[Note: This method overrides System.Collections.Specialized.NameValueCollection.Set(System.String,System.String).]

See Also

System.Net.WebHeaderCollection Class, System.Net Namespace