HttpWebClientProtocol.CookieContainer 属性

获取或设置 Cookie 集合。

**命名空间:**System.Web.Services.Protocols
**程序集:**System.Web.Services(在 system.web.services.dll 中)

语法

声明
Public Property CookieContainer As CookieContainer
用法
Dim instance As HttpWebClientProtocol
Dim value As CookieContainer

value = instance.CookieContainer

instance.CookieContainer = value
public CookieContainer CookieContainer { get; set; }
public:
property CookieContainer^ CookieContainer {
    CookieContainer^ get ();
    void set (CookieContainer^ value);
}
/** @property */
public CookieContainer get_CookieContainer ()

/** @property */
public void set_CookieContainer (CookieContainer value)
public function get CookieContainer () : CookieContainer

public function set CookieContainer (value : CookieContainer)

属性值

CookieContainer,它表示 Web 服务客户端的 Cookie。

备注

如果 XML Web services 方法使用会话状态,则将在响应标头中将一个为 XML Web services 客户端唯一标识会话的 Cookie 传递回该 XML Web services 客户端。为了使 XML Web services 维护客户端的会话状态,客户端必须存储 Cookie。客户端通过在调用 XML Web services 之前创建 CookieContainer 的新实例并将其分配给代理类的 CookieContainer 属性来接收 HTTP Cookie。如果需要在代理类实例超出范围时维护会话状态,客户端必须在 XML Web services 调用之间存储 HTTP Cookie。例如,Web 窗体客户端可以通过在自己的会话状态中保存 CookieContainer 来存储 HTTP Cookie。因为并非所有 XML Web services 都使用会话状态,因而并不总是要求客户端使用客户端代理的 CookieContainer 属性,所以 XML Web services 文档必须说明是否使用会话状态。

示例

下面的代码示例是使用会话状态的 XML Web services 的 Web 窗体客户端。该客户端通过将唯一标识会话的 HTTP Cookie 存储在客户端的会话状态中来存储它。

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

<html>

    <script runat=server>

        Public Sub EnterBtn_Click(src As Object, E As EventArgs) 

      ' Create a new instance of a proxy class for your XML Web service.
      Dim su As ServerUsage = new ServerUsage()
          Dim cookieJar As CookieContainer

      ' Check to see if the cookies have already been saved for this session.
      If (Session("CookieJar") Is Nothing) 
        cookieJar= new CookieContainer()
          Else
       cookieJar = Session("CookieJar")
      End If
   

        ' Assign the CookieContainer to the proxy class.
        su.CookieContainer = cookieJar

      ' Invoke an XML Web service method that uses session state and thus cookies.
      Dim count As Integer = 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()  
    End Sub
         
    </script>
    <body>
       <form runat=server ID="Form1">
           
             Click to bump up the Session Counter.
             <p>
             <asp:button text="Bump Up Counter" Onclick="EnterBtn_Click" runat=server ID="Button1" NAME="Button1"/>
             <p>
             <asp:label id="SessionCount"  runat=server/>
          
       </form>
    </body>
</html>
<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>

<html>

    <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();
          else
       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();  
        }
         
    </script>
    <body>
       <form runat=server ID="Form1">
           
             Click to bump up the Session Counter.
             <p>
             <asp:button text="Bump Up Counter" Onclick="EnterBtn_Click" runat=server ID="Button1" NAME="Button1"/>
             <p>
             <asp:label id="SessionCount"  runat=server/>
          
       </form>
    </body>
</html>

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

HttpWebClientProtocol 类
HttpWebClientProtocol 成员
System.Web.Services.Protocols 命名空间
CookieContainer
WebMethodAttribute.EnableSession 属性