WebHeaderCollection.GetValues Method (String)

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Gets an array of header values stored in a header.

Namespace:   System.Net
Assembly:  System (in System.dll)

public override string[] GetValues(
	string header
)

Parameters

header
Type: System.String

The header to return.

Return Value

Type: System.String[]

An array of header strings.

GetValues returns the contents of the specified header as an array.

The following example uses the GetValues method to retrieve an array of values for each header in the WebHeaderCollection.

// Create a web request for "www.msn.com".
 HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest.Create("http://www.msn.com");
myHttpWebRequest.Timeout = 1000;
// Get the associated response for the above request.
 HttpWebResponse myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse();

// Get the headers associated with the response.
WebHeaderCollection myWebHeaderCollection = myHttpWebResponse.Headers;

for(int i = 0; i < myWebHeaderCollection.Count; i++) {
    String header = myWebHeaderCollection.GetKey(i);
    String[] values = myWebHeaderCollection.GetValues(header);
    if(values.Length > 0) {
        Console.WriteLine("The values of {0} header are : ", header);
        for(int j = 0; j < values.Length; j++) 
            Console.WriteLine("\t{0}", values[j]);
    }
    else
        Console.WriteLine("There is no value associated with the header");
}
myHttpWebResponse.Close();

.NET Framework
Available since 1.1
Return to top
Show: