This documentation is archived and is not being maintained.

WebHeaderCollection.Clear Method

Removes all headers from the collection.

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

'Declaration
Public Overrides Sub Clear

The following code example clears the headers in a WebHeaderCollection.


If args.Length = 0 Then
    Console.WriteLine("must specify a URL!")
    Return
End If
Dim server As String = args(0)

' Create the web request 
Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(server), HttpWebRequest)
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

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)
        For j = 0 To values.Length - 1
            Console.WriteLine(vbTab & "{0}", values(j))
        Next j
    Else
        Console.WriteLine("There is no value associated" & "with the header")
    End If
Next i
Console.WriteLine("")

' Get the headers again, using new properties (Keys, 
' AllKeys, Clear) and methods (Get and GetKey)

Dim headers() As String = myWebHeaderCollection.AllKeys

' enumerate through the header collection.
For Each s In headers
    Console.WriteLine("Header {0}, value {1}", s, myWebHeaderCollection.Get(s))
Next s

Console.WriteLine("")

' show the use of Get(Int32) and GetValue(Int32)
If myWebHeaderCollection.Count > 0 Then
    ' get the name and value of the first header
    Dim index As Integer = 0
    Console.WriteLine("Header {0}: name {1}, value {2}", index, myWebHeaderCollection.GetKey(index), myWebHeaderCollection.Get(index))
End If

myWebHeaderCollection.Clear()

myHttpWebResponse.Close()


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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