CookieCollection Class

CookieCollection Class

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Represents a collection of Cookie objects.


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

public class CookieCollection : ICollection, 

The CookieCollection type exposes the following members.

Public methodCookieCollectionInitializes a new instance of the CookieCollection class.

Public propertyCountGets the number of cookies contained in the CookieCollection.
Public propertyIsReadOnlyGets a value that indicates whether the CookieCollection is read-only.
Public propertyIsSynchronizedGets a value that indicates whether access to the CookieCollection is thread safe.
Public propertyItemGets the cookie with the specified name from the collection.
Public propertySyncRootGets an object that you can use to synchronize access to the CookieCollection.

Public methodAdd(Cookie)Adds the specified Cookie to the collection.
Public methodAdd(CookieCollection)Adds the contents of the specified CookieCollection to this collection.
Public methodCopyToCopies the contents of this CookieCollection to the specified array, starting at the specified index.
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetEnumeratorGets an enumerator to iterate through the CookieCollection.
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)

Public Extension MethodAsQueryableConverts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension MethodCast<TResult>Converts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension MethodOfType<TResult>Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)

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.


If you use this API in your app, you must specify the following capabilities in the app manifest. Otherwise, your app might not work correctly or it might exit unexpectedly.


Windows Phone 8, Windows Phone OS 7.1

For more info, see App capabilities and hardware requirements for Windows Phone 8.

The following example shows how to retrieve the CookieCollection from a response and write them to an isolated storage file.

private void ReadCallback(IAsyncResult asynchronousResult)
    HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
    HttpWebResponse response = (HttpWebResponse)
    using (IsolatedStorageFile isf =
        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());


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

© 2016 Microsoft