Export (0) Print
Expand All

WebHeaderCollection.GetValues Method (String)

Gets an array of header values stored in a header.

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

public override string[] GetValues (
	string header
)
public String[] GetValues (
	String header
)
public override function GetValues (
	header : String
) : String[]

Parameters

header

The header to return.

Return Value

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();

// Create a web request for "www.msn.com".
HttpWebRequest myHttpWebRequest = (HttpWebRequest)(
    WebRequest.Create("http://www.msn.com"));
myHttpWebRequest.set_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.
    get_Headers();

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

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft