WebHeaderCollection.GetValues Method

Definition

Gets an array of header values stored in a header.

Overloads

GetValues(String)

Gets an array of header values stored in a header.

GetValues(Int32)

Gets an array of header values stored in the index position of the header collection.

GetValues(String)

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

Gets an array of header values stored in a header.

C#
public override string[]? GetValues(string header);
C#
public override string[] GetValues(string header);

Parameters

header
String

The header to return.

Returns

String[]

An array of header strings.

Examples

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

C#
// 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();

Remarks

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

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

GetValues(Int32)

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

Gets an array of header values stored in the index position of the header collection.

C#
public override string[]? GetValues(int index);
C#
public override string[] GetValues(int index);

Parameters

index
Int32

The header index to return.

Returns

String[]

An array of header strings.

Remarks

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

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1