1、在 Web Service 里添加 Session
[WebMethod(EnableSession = true)] public void CreateSession() { Session["Ikesy"] = "Hello world."; }
ASP.NET 页面使用
SessionService.SessionService sessionService = new SessionService.SessionService(); CookieContainer container = new CookieContainer(); //创建 cookie 容器,用于标记 Web 服务的Session sessionService.CookieContainer = container; //将 cookie 容器与 Web 服务对应 sessionService.CreateSession(); Session["Ikesy"] = container; //在 Session 中保存 cookie 容器
2、读取 Web Service 里的 Session
[WebMethod(EnableSession=true)] public string GetSession() { if (Session["Ikesy"] != null) { return Session["Ikesy"].ToString(); } else { return "Cannot create session."; } }
SessionService.SessionService sessionService = new SessionService.SessionService(); CookieContainer container = Session["Ikesy"] as CookieContainer; //获取 Session 中保存的 cookie 容器 sessionService.CookieContainer = container; //标记 Web 服务中对应的 cookie 容器 message.InnerText = sessionService.GetSession();