HttpWebClientProtocol.CookieContainer Proprietà

Definizione

Ottiene o imposta l’insieme di cookie.Gets or sets the collection of cookies.

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

Valore della proprietà

Oggetto CookieContainer che rappresenta i cookie per un client di servizi Web.A CookieContainer that represents the cookies for a Web Services client.

Esempi

L'esempio di codice seguente è un client Web Form di un servizio Web XML che usa lo stato della sessione.The following code example is a Web Forms client of an XML Web service that uses session state. Il client archivia il cookie HTTP che identifica in modo univoco la sessione archiviando il cookie nello stato sessione del client.The client stores the HTTP cookie that uniquely identifies the session by storing it in the client's session state.

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

Commenti

Se un metodo di servizio Web XML utilizza lo stato della sessione, viene passato un cookie nelle intestazioni di risposta al client del servizio Web XML che identifica in modo univoco la sessione per il client del servizio Web XML.If an XML Web service method uses session state, then a cookie is passed back in the response headers to the XML Web service client that uniquely identifies the session for that XML Web service client. Affinché un servizio Web XML mantenga lo stato della sessione per un client, il client deve archiviare il cookie.In order for an XML Web service to maintain session state for a client, the client must store the cookie. I client ricevono il cookie HTTP creando una nuova istanza di CookieContainer e assegnando tale cookie CookieContainer alla proprietà della classe proxy prima di chiamare il metodo del servizio Web XML.Clients receive the HTTP cookie by creating a new instance of CookieContainer and assigning that to the CookieContainer property of the proxy class before calling the XML Web service method. Se è necessario mantenere lo stato della sessione oltre il momento in cui l'istanza della classe proxy esce dall'ambito, il client deve archiviare il cookie HTTP tra le chiamate al servizio Web XML.If you need to maintain session state beyond when the proxy class instance goes out of scope, the client must store the HTTP cookie between calls to the XML Web service. Un client Web Form, ad esempio, può archiviare il cookie HTTP salvando CookieContainer nello stato della sessione.For instance, a Web Forms client can store the HTTP cookie by saving the CookieContainer in its own session state. Poiché non tutti i servizi Web XML utilizzano lo stato della sessione e pertanto i client non sono sempre CookieContainer necessari per utilizzare la proprietà di un proxy client, la documentazione per il servizio Web XML deve indicare se viene utilizzato lo stato della sessione.Because not all XML Web services use session state and thus clients are not always required to use the CookieContainer property of a client proxy, the documentation for the XML Web service must state whether session state is used.

Si applica a

Vedi anche