CookieContainer Property

HttpWebClientProtocol.CookieContainer Property

Gets or sets the collection of cookies.

Namespace: System.Web.Services.Protocols
Assembly: System.Web.Services (in

public CookieContainer CookieContainer { get; set; }
/** @property */
public CookieContainer get_CookieContainer ()

/** @property */
public void set_CookieContainer (CookieContainer value)

public function get CookieContainer () : CookieContainer

public function set CookieContainer (value : CookieContainer)

Not applicable.

Property Value

A CookieContainer that represents the cookies for a Web Services client.

If an XML Web service method uses session state, then a cookie is passed back in the response headers to the XML Web service client that uniquely identifies the session for that XML Web service client. In order for an XML Web service to maintain session state for a client, the client must store the cookie. Clients receive the HTTP cookie by creating a new instance of CookieContainer and assigning that to the CookieContainer property of the proxy class before calling the XML Web service method. If you need to maintain session state beyond when the proxy class instance goes out of scope, the client must store the HTTP cookie between calls to the XML Web service. For instance, a Web Forms client can store the HTTP cookie by saving the CookieContainer in its own session state. Because not all XML Web services use session state and thus clients are not always required to use the CookieContainer property of a client proxy, the documentation for the XML Web service must state whether session state is used.

The following code example is a Web Forms client of an XML Web service that uses session state. The client stores the HTTP cookie that uniquely identifies the session by storing it in the client's session state.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>


    <script runat="server">

        void EnterBtn_Click(Object Src, EventArgs E) 
	  // Create a new instance of a proxy class for your XML Web service.
	  ServerUsage su = new ServerUsage();
          CookieContainer cookieJar;

	  // Check to see if the cookies have already been saved for this session.
	  if (Session["CookieJar"] == null) 
	    cookieJar= new CookieContainer();
	   cookieJar = (CookieContainer) Session["CookieJar"];

		// Assign the CookieContainer to the proxy class.
		su.CookieContainer = cookieJar;

	  // Invoke an XML Web service method that uses session state and thus cookies.
	  int count = su.PerSessionServiceUsage();         

	  // Store the cookies received in the session state for future retrieval by this session.
	  Session["CookieJar"] = cookieJar;

          // Populate the text box with the results from the call to the XML Web service method.
          SessionCount.Text = count.ToString();  
       <form runat=server ID="Form1">
             Click to bump up the Session Counter.
             <asp:button text="Bump Up Counter" Onclick="EnterBtn_Click" runat=server ID="Button1" NAME="Button1"/>
             <asp:label id="SessionCount"  runat=server/>

Windows 98, Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

© 2016 Microsoft