CookieCollection Class
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Represents a collection of Cookie objects.
Inheritance Hierarchy
System.Object
System.Net.CookieCollection
Namespace: System.Net
Assembly: System.Net (in System.Net.dll)
Syntax
'Declaration
<DefaultMemberAttribute("Item")> _
Public NotInheritable Class CookieCollection _
Implements ICollection, IEnumerable
[DefaultMemberAttribute("Item")]
public sealed class CookieCollection : ICollection,
IEnumerable
The CookieCollection type exposes the following members.
Constructors
Name | Description | |
---|---|---|
CookieCollection | Initializes a new instance of the CookieCollection class. |
Top
Properties
Name | Description | |
---|---|---|
Count | Gets the number of cookies contained in the CookieCollection. | |
IsReadOnly | Gets a value that indicates whether the CookieCollection is read-only. | |
IsSynchronized | Gets a value that indicates whether access to the CookieCollection is thread safe. | |
Item | Gets the cookie with the specified name from the collection. | |
SyncRoot | Gets an object that you can use to synchronize access to the CookieCollection. |
Top
Methods
Name | Description | |
---|---|---|
Add(Cookie) | Adds the specified Cookie to the collection. | |
Add(CookieCollection) | Adds the contents of the specified CookieCollection to this collection. | |
CopyTo | Copies the contents of this CookieCollection to the specified array, starting at the specified index. | |
Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) | |
Finalize | Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.) | |
GetEnumerator | Gets an enumerator to iterate through the CookieCollection. | |
GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) | |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) | |
ToString | Returns a string that represents the current object. (Inherited from Object.) |
Top
Extension Methods
Name | Description | |
---|---|---|
AsQueryable | Converts an IEnumerable to an IQueryable. (Defined by Queryable.) | |
Cast<TResult> | Converts the elements of an IEnumerable to the specified type. (Defined by Enumerable.) | |
OfType<TResult> | Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.) |
Top
Remarks
This class stores a collection of cookie objects associated with a client Web request.
You should always create a CookieCollection and add it to the request if you want to retrieve Cookie objects from the response.
Examples
The following example shows how to retrieve the CookieCollection from a response and write them to an isolated storage file.
' Get the response and write cookies to isolated storage.
Private Sub ReadCallback(ByVal asynchronousResult As IAsyncResult)
Dim request As HttpWebRequest = DirectCast(asynchronousResult.AsyncState, _
HttpWebRequest)
Dim response As HttpWebResponse = _
DirectCast(request.EndGetResponse(asynchronousResult), HttpWebResponse)
Using isf As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForSite()
Using isfs As IsolatedStorageFileStream = isf.OpenFile("CookieExCookies", _
FileMode.OpenOrCreate, FileAccess.Write)
Using sw As New StreamWriter(isfs)
For Each cookieValue As Cookie In response.Cookies
sw.WriteLine("Cookie: " + cookieValue.ToString())
Next
sw.Close()
End Using
End Using
End Using
End Sub
private void ReadCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)
request.EndGetResponse(asynchronousResult);
using (IsolatedStorageFile isf =
IsolatedStorageFile.GetUserStoreForSite())
{
using (IsolatedStorageFileStream isfs = isf.OpenFile("CookieExCookies",
FileMode.OpenOrCreate, FileAccess.Write))
{
using (StreamWriter sw = new StreamWriter(isfs))
{
foreach (Cookie cookieValue in response.Cookies)
{
sw.WriteLine("Cookie: " + cookieValue.ToString());
}
sw.Close();
}
}
}
}
Version Information
Silverlight
Supported in: 5, 4, 3
Silverlight for Windows Phone
Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0
XNA Framework
Supported in: Windows Phone OS 7.0
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.