本文分步介紹了如何使用該 System.Net.CookieContainer 類,應用程式中的 Web 服務使用會話或 Cookie 時。
儘管 Web 服務是本質上是無狀態,您可以使用 Session 對象維護用戶端應用程式和伺服器應用程式之間的有狀態通訊。 若要啟用 網頁用戶端和 Web 服務之間的有狀態通訊,您可能會從用戶端應用程式發送到 Web 服務的每個郵件使用 CookieContainer 對象。 您可能會佔用狀態啟用用戶端應用程式中有狀態的 Web 服務。
建立 Web 服務應用程式
- 運行 Microsoft Visual Studio.NET。 建立新的 ASP.NET Web 服務項目,使用 Visual C#.NET。
情況預設,建立 Service 1.asmx。
- 將該項目命名 WebService1 。
- 在 產生 菜單上單擊 產生解決方案 。
啟用伺服器上的會話支援
預設,處於關閉狀態為每個 Web 服務方法的 ASP.NET 會話支援。 必須顯式啟用需要工作階段狀態的每個 Web 服務方法的會話支援。 若要啟用該會話支援,請將 EnableSession 屬性添加到 WebMethod 屬性。 要這樣做,請按下列步驟操作:
- 在方案總管按右鍵 Service 1.asmx ,然後將現有代碼替換為下面的代碼:
using System;using System.ComponentModel;using System.Web;using System.Web.Services;namespace WebService1{/// <summary>/// Summary description for Service1./// </summary>public class Service1 : System.Web.Services.WebService{public Service1(){//CODEGEN: Call required by ASP.NET Web Services Designer.InitializeComponent();}#region Component Designer generated codeprivate void InitializeComponent(){}#endregion [WebMethod(EnableSession=true)] public string SetTime(string CurrentTime) { Session.Add("Time", CurrentTime); return ((string) Session["Time"] ); } [WebMethod(EnableSession=true)] public string GetTime() { return ((string) Session["Time"] ); }}}
您可能注意到 [WebMethod(EnableSession=true)] 屬性添加要啟用會話支援兩個 Web 方法的。
- 在 產生 菜單上單擊 產生解決方案 。
建立 ASP.NET 用戶端應用程式
當 Web 服務方法使用工作階段狀態時,則會將 Cookie 傳遞迴給 Web 服務用戶端響應標題中。 該 Cookie 唯一標識的該 Web 服務用戶端的會話。 為 Web 服務用戶端接收的 Cookie, CookieContainer 的新執行個體必須建立並調用 Web 服務方法之前然後分配給 CookieContainer 屬性。 這將確保正確中後續請求中包含該 Cookie。 您必須這樣做,因為您必須儲存在以後檢索此會話的工作階段狀態中收到的 Cookie。 要這樣做,請按下列步驟操作:
- 建立新的 ASP.NET Web 應用程式使用 Visual C#.NET。 將該項目 CookieContainerApp 命名。
情況預設,建立 WebForm 1.aspx。
- 在 設計 視圖中按右鍵 WebForm 1 ,然後單擊 查看 HTML 源 。
- Replace the existing code with the following code:
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="CookieContainerApp.WebForm1" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ><HTML> <HEAD> <title>WebForm1</title> <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 270px; POSITION: absolute; TOP: 143px" runat="server" Text="SetTimeInSession" Width="187px"></asp:Button> <asp:Button id="Button2" style="Z-INDEX: 102; LEFT: 269px; POSITION: absolute; TOP: 203px" runat="server" Text="GetTimeFromSession"></asp:Button> <asp:Label id="Label1" style="Z-INDEX: 103; LEFT: 565px; POSITION: absolute; TOP: 150px" runat="server"></asp:Label> <asp:Label id="Label2" style="Z-INDEX: 104; LEFT: 565px; POSITION: absolute; TOP: 211px" runat="server"></asp:Label> </form> </body></HTML>
- 在方案總管按右鍵 引用 ,然後單擊 添加 Web 參考 。
- 在 地址 文字框中鍵入的 WebService1 以下 URL:http://localhost/WebService1/Service1.asmx
- 單擊 定位 ,然後單擊 添加引用 。
- 在方案總管按右鍵 WebForm 1.aspx ,然後單擊 查看代碼 。
- 替換為下面的代碼的 WebForm 1 中的現有代碼:
using System;using System.Web.UI.WebControls;namespace CookieContainerApp{/// <summary>/// Summary description for WebForm1./// </summary>public class WebForm1 : System.Web.UI.Page{ protected System.Web.UI.WebControls.Button Button1; protected System.Web.UI.WebControls.Button Button2; protected System.Web.UI.WebControls.Label Label1; protected System.Web.UI.WebControls.Label Label2; // Create a new instance of a proxy class for your Web service. private localhost.Service1 objWSFunc = new localhost.Service1();private void Page_Load(object sender, System.EventArgs e){// Put user code to initialize the page here.}#region Web Form Designer generated codeoverride protected void OnInit(EventArgs e){//// CODEGEN: Call required by ASP.NET Web Form Designer.//InitializeComponent();base.OnInit(e);}/// <summary>/// Required method for Designer support. Do not modify./// The contents of this method with the code editor./// </summary>private void InitializeComponent(){ this.Button1.Click += new System.EventHandler(this.Button1_Click); this.Button2.Click += new System.EventHandler(this.Button2_Click); this.Load += new System.EventHandler(this.Page_Load); }#endregion private void Button1_Click(object sender, System.EventArgs e) { System.Net.CookieContainer cookieJar = new System.Net.CookieContainer(); // Assign the CookieContainer to the proxy class. objWSFunc.CookieContainer = cookieJar; // Get CurrentTime. DateTime dt = DateTime.Now; string CurrentTime = dt.ToString("s"); // Invoke a Web service method that uses session state and therefore cookies. objWSFunc.SetTime(CurrentTime); // Store the cookies received in the session state for future retrieval by this session. Session.Add("Time", cookieJar); Label1.Text="Time set in Session : " +CurrentTime ; Label2.Visible=false; } private void Button2_Click(object sender, System.EventArgs e) { // Get the SessionObject. objWSFunc.CookieContainer = (System.Net.CookieContainer) Session["Time"]; Label2.Visible=true; // Call the WebService method to access the session state. Label2.Text = "Time Get from Session : "+ objWSFunc.GetTime(); } }}
- 在 產生 菜單上單擊 產生解決方案 。
使用 CookieContainer 將內容添加到會話對象
- 在 調試 菜單中上, 單擊 開始 ,以產生並運行應用程式。
- 單擊 SetTimeInSession 。
當前的時間值儲存在會話對象中,目前時間顯示。
在按鈕單擊事件, CookieContainer 對象建立,並再分配給 Web 服務代理 CookieContainer 屬性。 然後 Web 服務方法 SetTime() 調用來更新會話對象。
內容從擷取會話對象使用 CookieContainer
單擊 GetTimeFromSession 。 您可能發現的對象顯示在調用 Web 會話中儲存的值服務方法 GetTime() 時間。