HttpWebClientProtocol.CookieContainer 属性

定义

获取或设置 Cookie 集合。

public:
 property System::Net::CookieContainer ^ CookieContainer { System::Net::CookieContainer ^ get(); void set(System::Net::CookieContainer ^ value); };
public System.Net.CookieContainer CookieContainer { get; set; }
member this.CookieContainer : System.Net.CookieContainer with get, set
Public Property CookieContainer As CookieContainer

属性值

CookieContainer

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

示例

下面的代码示例是使用会话状态的 XML Web 服务的Web Forms客户端。 客户端通过将其存储在客户端的会话状态来存储唯一标识会话的 HTTP Cookie。

<%@ 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>
<%@ 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>

注解

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

适用于

另请参阅