本文分步介紹瞭如何使用該 System.Net.CookieContainer 類,應用程序中的 Web 服務使用會話或 Cookie 時。
儘管 Web 服務是本質上是無狀態,您可以使用 Session 對象維護客戶端應用程序和服務器應用程序之間的有狀態通信。 若要啓用 Web 客戶端和 Web 服務之間的有狀態通信,您可能會從客戶端應用程序發送到 Web 服務的每個郵件使用 CookieContainer 對象。 您可能會佔用狀態啓用客戶端應用程序中有狀態的 Web 服務。
創建 Web 服務應用程序
<script type="text/javascript"></script>
- 運行 Microsoft Visual Studio.NET。 創建新的 ASP.NET Web 服務項目,使用 Visual C#.NET。
情況默認,創建 Service 1.asmx。 - 將該項目命名 WebService1 。
- 在 生成 菜單上單擊 生成解決方案 。
啓用服務器上的會話支持
<script type="text/javascript"></script> 默認,處於關閉狀態爲每個 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 code private 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"] ); } } }
- 在 生成 菜單上單擊 生成解決方案 。
創建 ASP.NET 客戶端應用程序
<script type="text/javascript"></script> 當 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 code override 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 將內容添加到會話對象
<script type="text/javascript"></script>
- 在 調試 菜單中上, 單擊 開始 ,以生成並運行應用程序。
- 單擊 SetTimeInSession 。
當前的時間值存儲在會話對象中,當前時間顯示。
在按鈕單擊事件, CookieContainer 對象創建,並再分配給 Web 服務代理 CookieContainer 屬性。 然後 Web 服務方法 SetTime() 調用來更新會話對象。
內容從獲取會話對象使用 CookieContainer
<script type="text/javascript"></script> 單擊 GetTimeFromSession 。 您可能發現的對象顯示在調用 Web 會話中存儲的值服務方法 GetTime() 時間。