HttpWebClientProtocol.CookieContainer Tulajdonság
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Lekéri vagy beállítja a cookie-k gyűjtését.
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
Tulajdonság értéke
Egy CookieContainer Web Services-ügyfél cookie-jának felel meg.
Példák
Az alábbi példakód egy munkamenet-állapotot használó XML-webszolgáltatás Web Forms-ügyfele. Az ügyfél tárolja a munkamenetet egyedileg azonosító HTTP-cookie-t úgy, hogy az ügyfél munkamenet-állapotában tárolja.
<%@ 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>
Megjegyzések
Ha egy XML webszolgáltatás-metódus munkamenet-állapotot használ, a rendszer a válaszfejlécekben egy cookie-t ad vissza az XML Web Service-ügyfélnek, amely egyedileg azonosítja az adott XML-webszolgáltatás-ügyfél munkamenetét. Ahhoz, hogy egy XML-webszolgáltatás megőrizze az ügyfél munkamenet-állapotát, az ügyfélnek el kell tárolnia a cookie-t. Az ügyfelek úgy kapják meg a HTTP-cookie-t, hogy létrehoznak egy új példányt CookieContainer , és hozzárendelik azt a CookieContainer proxyosztály tulajdonságához, mielőtt meghívják az XML Web Service metódust. Ha a proxyosztály-példány hatókörén kívülre kell tartania a munkamenet-állapotot, az ügyfélnek el kell tárolnia a HTTP-cookie-t az XML-webszolgáltatás felé irányuló hívások között. Egy Web Forms-ügyfél például a SAJÁT munkamenet-állapotában mentve tárolhatja a CookieContainer HTTP-cookie-t. Mivel nem minden XML-webszolgáltatás használ munkamenet-állapotot, ezért az ügyfeleknek nem mindig kell használniuk az CookieContainer ügyfélproxy tulajdonságát, az XML-webszolgáltatás dokumentációjának meg kell állapítania, hogy a munkamenet állapota használatban van-e.