Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
GetValues Method

WebHeaderCollection.GetValues Method

Gets an array of header values stored in a header.

Overload List

Gets an array of header values stored in a header.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Overrides Public Function GetValues(String) As String()
[C#] public override string[] GetValues(string);
[C++] public: String* GetValues(String*) __gc[];
[JScript] public override function GetValues(String) : String[];

Inherited from NameValueCollection.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Function GetValues(Integer) As String()
[C#] public virtual string[] GetValues(int);
[C++] public: virtual String* GetValues(int) __gc[];
[JScript] public function GetValues(int) : String[];

Example

[Visual Basic, C#, C++] The following example uses the GetValues method to retrieve an array of values for each header in the WebHeaderCollection.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of GetValues. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
'Create a web request for "www.msn.com".
Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create("http://www.msn.com"), HttpWebRequest)
'********* the line 23 is causing problem . it is not necessary and can be deleted.
        myHttpWebRequest.Timeout = 1000
'Get the associated response for the above request.
Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)

'Get the headers associated with the response.
Dim myWebHeaderCollection As WebHeaderCollection = myHttpWebResponse.Headers

'The Snippet for 'GetValues(string)' method of 'WebHeaderCollection' class starts here---->
Dim i As Integer
For i = 0 To myWebHeaderCollection.Count - 1
    Dim header As [String] = myWebHeaderCollection.GetKey(i)
    Dim values As [String]() = myWebHeaderCollection.GetValues(header)
    If values.Length > 0 Then
        Console.WriteLine("The values of {0} header are : ", header)
        Dim j As Integer
        For j = 0 To values.Length - 1
            Console.WriteLine(ControlChars.Tab + "{0}", values(j))
        Next j
    Else
        Console.WriteLine("There is no value associated with the header")
    End If
Next i 

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

[C++] 
// Create a web request for S"www.msn.com".
HttpWebRequest* myHttpWebRequest =
   dynamic_cast<HttpWebRequest*> (WebRequest::Create(S"http://www.msn.com"));
myHttpWebRequest->Timeout = 1000;
// Get the associated response for the above request.
HttpWebResponse* myHttpWebResponse =
   dynamic_cast<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(S"The values of {0} header are : ", header);
      for (int j = 0; j < values->Length; j++)
         Console::WriteLine(S"\t {0}", values->Item[j]);
   } else
      Console::WriteLine(S"There is no value associated with the header");
}
myHttpWebResponse->Close();

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

WebHeaderCollection Class | WebHeaderCollection Members | System.Net Namespace

Show:
© 2015 Microsoft