Udostępnij za pośrednictwem


HttpWebClientProtocol.CookieContainer Właściwość

Definicja

Pobiera lub ustawia kolekcję plików 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

Wartość właściwości

CookieContainer

Element CookieContainer reprezentujący pliki cookie klienta usług sieci Web.

Przykłady

Poniższy przykład kodu to klient Web Forms usługi sieci Web XML, która używa stanu sesji. Klient przechowuje plik cookie HTTP, który jednoznacznie identyfikuje sesję, przechowując go w stanie sesji klienta.

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

Uwagi

Jeśli metoda usługi sieci Web XML używa stanu sesji, plik cookie jest przekazywany z powrotem w nagłówkach odpowiedzi do klienta usługi sieci Web XML, który jednoznacznie identyfikuje sesję dla tego klienta usługi sieci Web XML. Aby usługa sieci Web XML obsługiwała stan sesji klienta, klient musi przechowywać plik cookie. Klienci otrzymują plik cookie HTTP przez utworzenie nowego wystąpienia CookieContainer i przypisanie go do CookieContainer właściwości klasy serwera proxy przed wywołaniem metody usługi sieci Web XML. Jeśli musisz zachować stan sesji poza zakresem wystąpienia klasy proxy, klient musi przechowywać plik cookie HTTP między wywołaniami usługi sieci Web XML. Na przykład klient Web Forms może przechowywać plik cookie HTTP, zapisując plik CookieContainer cookie we własnym stanie sesji. Ponieważ nie wszystkie usługi sieci Web XML używają stanu sesji, dlatego klienci nie zawsze muszą korzystać z CookieContainer właściwości serwera proxy klienta, dokumentacja usługi sieci Web XML musi określać, czy jest używany stan sesji.

Dotyczy

Zobacz też