Bagikan melalui


HttpWebClientProtocol.CookieContainer Properti

Definisi

Mendapatkan atau mengatur koleksi 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

Nilai Properti

CookieContainer yang mewakili cookie untuk klien Layanan Web.

Contoh

Contoh kode berikut adalah klien Formulir Web dari layanan Web XML yang menggunakan status sesi. Klien menyimpan cookie HTTP yang secara unik mengidentifikasi sesi dengan menyimpannya dalam status sesi klien.

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

Keterangan

Jika metode layanan Web XML menggunakan status sesi, maka cookie diteruskan kembali di header respons ke klien layanan Web XML yang secara unik mengidentifikasi sesi untuk klien layanan Web XML tersebut. Agar layanan Web XML mempertahankan status sesi untuk klien, klien harus menyimpan cookie. Klien menerima cookie HTTP dengan membuat instans CookieContainer baru dan menetapkannya ke CookieContainer properti kelas proksi sebelum memanggil metode layanan Web XML. Jika Anda perlu mempertahankan status sesi di luar ketika instans kelas proksi keluar dari cakupan, klien harus menyimpan cookie HTTP antara panggilan ke layanan Web XML. Misalnya, klien Web Forms dapat menyimpan cookie HTTP dengan menyimpan CookieContainer dalam keadaan sesinya sendiri. Karena tidak semua layanan Web XML menggunakan status sesi dan dengan demikian klien tidak selalu diharuskan untuk menggunakan CookieContainer properti proksi klien, dokumentasi untuk layanan Web XML harus menyatakan apakah status sesi digunakan.

Berlaku untuk

Lihat juga