public class WebHeaderCollection : NameValueCollection
Object
NameValueCollection
WebHeaderCollectionThis type implements ICollection and IEnumerable.
System
Networking
Contains protocol headers associated with a WebRequest or WebResponse instance.
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:
- Accept
- Connection
- Content-Length
- Content-Type
- Date
- Expect
- Host
- If-Modified-Since
- Range
- Referer
- Transfer-Encoding
- UserAgent
DefaultMemberAttribute("Item")
System.Net Namespace
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
public WebHeaderCollection();
Constructs a new instance of the WebHeaderCollection class.
System.Net.WebHeaderCollection Class, System.Net Namespace
public override void Add(string name, string value);
Inserts a new header with the specified name and value into the collection.
- 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.
Exception Type Condition ArgumentException name 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.
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).]
System.Net.WebHeaderCollection Class, System.Net Namespace
public void Add(string header);
Inserts the specified header into the collection.
- header
- A String containing the header to add, with the name and value separated by a colon.
Exception Type Condition ArgumentNullException header is null
or System.String.Empty.ArgumentException header 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.
This method inserts a new header into the list of header name/value pairs. header is required to be specified in the formatname:value
.If the header specified in
name
is already present in the collection,value
is concatenated with the existing value.
System.Net.WebHeaderCollection Class, System.Net Namespace
protected void AddWithoutValidate(string headerName, string headerValue);
Inserts a header into the current instance without checking whether the header is on the restricted header list.
- 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.
Exception Type Condition ArgumentException headerName is null
or System.String.Empty, or contains invalid characters.-or-
headerValue contains invalid characters.
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. ]
System.Net.WebHeaderCollection Class, System.Net Namespace
public override string[] GetValues(string header);
Returns the values stored in the specified protocol header.
- header
- A String containing the protocol header name whose values are returned.
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, returnsnull
.
[Note: This method overrides System.Collections.Specialized.NameValueCollection.GetValues(System.String).]
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 isTransfer-Encoding:
compress
gzip
System.Net.WebHeaderCollection Class, System.Net Namespace
public static bool IsRestricted(string headerName);
Returns a Boolean value that indicates whether the specified HTTP header can be set.
- headerName
- A String containing the header to test.
true
if the header is protected; otherwisefalse
.
Exception Type Condition ArgumentNullException headerName is null
or System.String.Empty.ArgumentException headerName contains invalid characters.
This method returnstrue
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:
- Accept
- Connection
- Content-Length
- Content-Type
- Date
- Expect
- Host
- If-Modified-Since
- Range
- Referer
- Transfer-Encoding
- UserAgent
System.Net.WebHeaderCollection Class, System.Net Namespace
public override void Remove(string name);
Removes the specified header from the current instance.
- name
- A String that contains the name of the header to remove from the current instance.
Exception Type Condition ArgumentNullException name is null
or System.String.Empty.ArgumentException name contains invalid characters. -or-
name is a protected header that can only be set with a property accessor or by the system.
This method deletes the specified header from the current instance. If the same header was added multiple times with System.Net.WebHeaderCollection.Add(System.String,System.String), a single call to System.Net.WebHeaderCollection.Remove(System.String) deletes all of the headers.[Note: This method overrides System.Collections.Specialized.NameValueCollection.Remove(System.String).]
System.Net.WebHeaderCollection Class, System.Net Namespace
public override void Set(string name, string value);
Sets the specified header to the specified value.
- name
- A String that contains the name of the header to set.
- value
- A String that contains the content of the header to set.
Exception Type Condition ArgumentNullException name is null
or System.String.Empty.ArgumentException name 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.
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).]
System.Net.WebHeaderCollection Class, System.Net Namespace