?? ??? ?? ??? ???? ????? ?? ?? ?? ????? ???? ????
System.Net.CookieContainer???? ?? ????? ???? ?????? ?? ?????? ???? ????????? ??? ??? ???? ?? ????
??? ?????? inherently stateless ???, ?????? ?? ???? ?????????? ???? ??????? ????????? ?? ?? ????? ????????? ?? ??? stateful ????? ?? ???? ???? ?? ??? ????? ?? ???? ??? ??? ??? ??????? ?? ??? ???? ?? ??? stateful ????? ?? ????? ???? ?? ??? ?? ????? ?? ???? ??
CookieContainer??????? ????????? ?? ??? ???? ?? ???? ???? ?? ?? ???????? ????? ?? ??? ???????? ??? ?? ???? ?????? ????? ??????? ????????? ??? stateful ??? ???? ??? ?? ???? ?? ???? ???
??? ??? ???? ????????? ?????
- Microsoft Visual Studio .NET ?????? ??? ??? asp.NET ??? ????? ????? C# .NET ?? ????? ?? ???? ??????????
???????? ??? ??, Service1.asmx ?? ???? ??? - ????????? ?? ???WebService1.
- ????? ????????????? ??,??????? ??????.
????? ?? ???? ?????? ????? ????
???????? ??? ??, ASP.NET ???? ?????? ??? ???? ?? ???????? ???? ?? ??? ??? ?? ???? ??? ?? ???? ???? ?????? ?? ???????? ?? ?? ???????? ??? ???? ???? ?? ??? ?????? ?? ???? ?????? ??? ?? ????? ???? ?????? ??? ???? ?????? ?? ????? ???? ?? ??? ??????
EnableSession???? ?? ??? ???
WebMethod??????? ??? ??? ???? ?? ???, ????? ????? ?? ???? ????::
- ?????? Explorer ???, ???? ?????Service1.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"] );
}
}
}
?? ?? ????? ?? ???? ??[WebMethod(EnableSession=true)]??????? ?? ???? ?????? ?? ????? ???? ?? ??? ??? ??????? ????? ?? ??? ????? ??? ??? - ????? ????????????? ??,??????? ??????.
???? asp.NET ??????? ????????? ?????
??? ???? ?????? ?? ???? ?????? ?? ????? ???? ??, ?? ???? ???? ???????? ????? ??? ??? ??? ???? ??????? ?? ???? ??? ??? ?? ???? ?? ??? ???? ??????? ?? ??? ?? ???? ???????? ??? ???? ??????? ?? ???, ???? ?? ?? ?????? ?? ???? ?? ??????? ???? ?? ???
CookieContainer?????? ????? ?? ??? ?? ????? ???? ???
CookieContainer??? ???? ??? ???? ?????? ?? ??? ???? ???? ??? ?? ????????? ???? ?? ???? ??? ??? ??? ????? ?? ?????? ?????? ???? ??? ???? ????? ??? ??????? ?? ???? ?? ?????? ???? ?? ??? ???? ?????? ??? ??????? ?? ?? ?????? ?? ???????? ???? ?????? ??? ???? ?? ???, ????? ????? ?? ???? ????::
- ????? C# ?? ????? ?? ?? ??? asp.NET ??? ????????? ?? ??? .NET. ????????? ?? ???CookieContainerApp.
???????? ??? ??, WebForm1.aspx ?? ???? ??? - ??????????????, ????-????? ????WebForm1?? ????-????? ????, ?? ???? ???HTML ????? ?????.
- ?????? ??? ?? ????? ??? ?? ???????????? ????:
<%@ 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>
- ?????? Explorer ???, ???? ????????????? ????-????? ????, ?? ???? ?????? ?????? ??????.
- ??????????? ?????, WebService1 ?? ??? ????? URL ???? ????:
HTTP://localhost/WebService1/Service1.asmx
- ????? ????,?? ?????? ????-????? ????, ?? ???? ????????? ??????.
- ?????? Explorer ???, ???? ?????WebForm1.aspx?? ????-????? ????, ?? ???? ???????? ???.
- ?????? ??? ??? ?????WebForm1????? ???: ?? ???
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 ?? ????? ?? ??????
- ????? ???????? ???????? ??,???????????? ?? ??? ?? ????????? ?? ????? ?? ????
- ????? ????,SetTimeInSession.
??????? ??? ??? ???? ???????? ??? ???????? ??, ?? ??????? ??? ????? ???? ???
??? ??? ?????, ????? ????.CookieContainer???????? ????? ??? ?? ?? ???? ??? ?? ????? ???? ?? ??? ??? ???? ????????CookieContainer???? ???? ??? ??? ???? ????SetTime()???? ???????? ?? ?????? ???? ?? ??? ??? ???
??????? ?? ??????? ???? ???????? CookieContainer ?? ????? ??
????? ????,
GetTimeFromSession. ?? ????? ?? ???? ?? ?? ?? ?? ??? ??? ???? ???????? ??? ???????? ??? ??? ????? ???? ????
GetTime().
???? ??????? ?? ??? ???? ??? ???? ??? asp.NET ???? ?????? ?? ????? ???? ?? ???? ??? ?? CookieContainer ???? ?? ???? ???, ????? Microsoft ??? ???? ?? ????:
(vs.71) http://msdn2.Microsoft.com/en-us/library/SYSTEM.NET.cookiecontainer .aspx
(http://msdn2.microsoft.com/en-us/library/system.net.cookiecontainer(vs.71).aspx)
HTTP://msdn2.Microsoft.com/en-us/library/aa480509.aspx
(http://msdn2.microsoft.com/en-us/library/aa480509.aspx)