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,表示 Web 服務用戶端的 Cookie。

範例

下列程式碼範例是 XML Web 服務的Web Form用戶端,其使用會話狀態。 用戶端會儲存可唯一識別會話的 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。 用戶端會先建立 的新實例,並將該實例 CookieContainer 指派給 CookieContainer Proxy 類別的 屬性,再呼叫 XML Web 服務方法,以接收 HTTP Cookie。 如果您需要在 Proxy 類別實例超出範圍時維護超過會話狀態,用戶端必須在對 XML Web 服務的呼叫之間儲存 HTTP Cookie。 例如,Web Form用戶端可以儲存自己的會話狀態來 CookieContainer 儲存 HTTP Cookie。 由於並非所有 XML Web 服務都使用會話狀態,因此用戶端不一定需要使用 CookieContainer 用戶端 Proxy 的 屬性,因此 XML Web 服務的檔必須指出是否使用會話狀態。

適用於

另請參閱