http的内置对象 Session Application Global.aspx文件
2015-09-12 14:58
344 查看
http请求和响应是无状态的。 客户端向服务器发出请求,对于服务器来讲, 代表客户端的请求对象是HttpRequest.. //HttpRequest对象表示的是客户端对于服务器的请求,那么把客户端的请求封装在HttpRequest对象中。 HttpRequest request = this.Request; //HttpResponse表示的是服务器对于客户端的响应。 //HttpResponse response = this.response; Response.Write("客户端的请求的虚拟路径:"+this.Request.FilePath); Response.Write("客户端的请求的物理路径:"+this.Request.PhysicalPath); Response.Write("请求的参数值:"+Request.QueryString["code"]); 内置对象: 1.HttpRequest对象 2.HttpResponse对象 3.HttpCookie对象 由服务器端发送给客户端的信息, 但是这个信息保留在客户端的电脑中。 关键点:由服务器产生,由客户端保存。 不同浏览器的Cookie是独立的。 protected void Page_Load(object sender,EventArgs e) { if(!this.IsPostBack)//表示不是会发的数据。 { if(Request.Cookies["username"] !=null) { string username = Request.Cookies["username"].Value; this.TextBox1.Text = username; } } } protected void Button_Click(object sender,EventArgs e) { if(this.CheckBox1.Checked)//选中的话就表示的是记住用户名 { HttpCookie cookie1 = new HttpCookie("username",this.TextBox1.Text); //Cookie有一个过期时间 cookie1.Expires.AddMinutes(1);//AddMinutes表示的是过期的时间是一分钟。 //Cookie对象要发送给客户端 Response.Cookies.Add(cookie1); } //Session是保存在服务器端的,默认的会话时间是20分钟。 Session["user"]=this.TextBox1.Text; //跳转到另外一个页面 Response.Redirect("chat.aspx"); } 4.Session -位置:保存在服务器端,安全性高 -类型:任意类型 -Session保存的信息不与其他用户共享 -在用户会话期间可以记录和监视其他用户信息 -当回话过期或终止时服务器会清楚Session对象 protected void Page_Load(object sender,EventArgs e) { if(Session["user"] !=null) { this.Label1.Text = Session["user"].ToString(); this.Label2.Text = Application["userVisit"].ToString(); this.Label3.Text = Application["count"].ToString(); } } 5.Application 所有的用户都可以访问和设置 而Session表示的是单个的用户访问和设置 Global.aspx文件 -处理应用程序级时间的可选文件 -在应用程序的根目录下 Application_Start 接受第一个请求是触发 Application_End 应用程序结束时触发 Session_Start 某用户第一次访问时触发 Session_End 某用户退出应用程序时触发 protected void Application_Start(object sender,EventArgs e) { Application.Lock(); Application["UserVisit"]=0; //初始化 Application["count"] = 0; Application.UnLock(); } protected void Session_Start(object sender,EventArgs e) { Application.Lock(); Application["UserVisit"]=(int)Application["UserVisit"]+1; Application["count"] = (int)Application["count"]+1; Application.UnLock(); } protected void Session_End(object sender,EventArgs e) { Application.Lock(); Application["count"] = (int)Application["count"]-1; Application.UnLock(); }
相关文章推荐
- (5.2.2)TCP和UDP的区别(转)
- TCP三次握手、四次挥手
- 网络管理技术(二)
- ios网络编程(二)之网络连接
- linux c 检测网络状态
- HttpRequest信息内容介绍
- HTTP协议中POST、GET、HEAD、PUT等请求方法以及一些常见错误(转载)
- 关于《JavaScript DOM 编程艺术》一书中getHTTPObject.js 的一点看法
- IOS 获取网络地址的html字符串 本地获取网络图片
- HttpClient使用HttpGet进行json数据传输
- Linux下常用命令-网络命令
- 网络电视精灵2
- VMware虚拟机上网络连接(network type)的三种模式--bridged、host-only、NAT
- http://www.cnblogs.com/xia520pi/archive/2012/05/16/2504205.html
- web service中配置tcp/ip监视器
- 网络请求之——————post
- ASP.NET-自定义HttpModule与HttpHandler介绍
- php读取网络文件 curl, fsockopen ,file_get_contents 几个方法的效率对比
- ios网络编程(一)之网络请求
- Qt5.5中QTcpserver的注意细节(默认监听与IPv6格式等)