浏览器与服务器间的交互(客服端 <---> 服务器)

                                                        浏览器与服务器间的交互(客服端 <---> 服务器)

 

     请求--->处理--->响应  

    对类HttpContext 内部成员的使用 例如 :Request 、Response 、 Cookie 、 Session 、GetSection  . . .

        

 

/////~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

* * * 然而  一般处理程序 既为 中间阶段的 处理 层面

 

1 public void ProcessRequest(HttpContext context)
2 
3         {
4 
5             context.Response.ContentType = "text/plain";
6 
7             context.Response.Write("<a href='http://www.rupeng.com'>如鹏网</a>");
8 
9         }

* * *  上面的代码就是对于请求的内容做出处理。

/////~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

 

 ContentType: http://baike.baidu.com/view/1547292.htm?wtp=tt#5

http://zhidao.baidu.com/link?url=P-4kstF4EZV9ZcBctA8WJ6pBvMoeU-3PTu2mAT_ZQ_0GPfCKtz7e4d_iDOfomOCMq9KcUZxbFwNN9NlROF-Fl_

//***主要拿来生成动态图片和动态文本,比传统aspx文件效率高,因为它不包含控件解析和页面处理的过程..楼上说用于ajax,其实就是用它来产生动态文本,
你也可以拿来做验证码图片等..不过它的作用应该不止于此吧,我看了下资料说它完全可以自定义Http请求,这些当然是属于比较高深的内容了

 

/////~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

HttpContext.Request :

http://msdn.microsoft.com/zh-cn/library/system.web.httprequest(v=vs.110).aspx

 

 添加  //string action=context.Request["name"];

            //int age = Convert.ToInt32(context.Request["age"]);

            //string action=context.Request["name"]

            //context.Response.Write("<font color='red'>Hello " + action + "</font>");

            //context.Response.Write("<font color='green'>我今年"+age+"</font>");

之后  在地址栏里 传递 参数 . . . . 

       Request :请求   //从地址栏获得

       Response :响应  //返回给页面

 

 

 

<form action="TestHandler.ashx" method="get">

 

姓名:<input type="text" name="name" /><br />

 

年龄:<input type="text" name="age" /><br />

 

<input type="submit" />

</from>

 

 

将表单里的东西发送到地址栏,地址栏向服务器请求,然后服务器响应传回所请求的东西到地址栏,然后页面获取地址栏里相应的值。

 

   //  将表单里的内容提交(action)给服务器端(url)的程序(TestHandler.ashx)

      name里的值是指定具体对应的值(传参用的)

    上面的 //string action=context.Request["name"];

            //int age = Convert.ToInt32(context.Request["age"]);

     请求的参数就是 name 标签里的具体值。 

 

/////~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

封装模版引擎NVelocity.dll   

使用到了匿名函数 

 

            //把类的定义和对象的声明初始化放到一起

            //匿名类

            var news = new { Title = "特大喜讯",Author="杨中科",PostDate="2013-11-08",Msg="今天晚上会公布喜讯细节!" };

            string s = news.PostDate;           

 

 1  public class CommonHelper
 2     {
 3         public static string RenderHtml(string name, object data)
 4         {
 5              VelocityEngine vltEngine = new VelocityEngine();
 6             vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
 7             vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/templates"));//模板文件所在的文件夹
 8             vltEngine.Init();
 9 
10             VelocityContext vltContext = new VelocityContext();
11             vltContext.Put("Model", data);//设置参数,在模板中可以通过$data来引用
12 
13             Template vltTemplate = vltEngine.GetTemplate(name);
14             System.IO.StringWriter vltWriter = new System.IO.StringWriter();
15             vltTemplate.Merge(vltContext, vltWriter);
16             return vltWriter.GetStringBuilder().ToString();
17         }
18     }

结合一般处理程序 使用情况

 

 

 

 

                                         http 无状态保持(05-状态的传递和保持)

 

 

 怎么记住提交的值呢 ? 

   利用隐藏字段来实现 【相当于看病的病历本】

Html中:

 

Ashx中:

 

缺点  : 无法自由的存取数据 。 

 

下面介绍 Cookie

    

 

 

 

                                                 Cookie

 

 

 

Cookie的生命周期虽浏览器的关闭而自动删除

 

那么改如何,解决方法如下:

 

 

 

然而客户端的Cookie 任然还原被用户更改

解决法子  : Guid ( 全球唯一标识符 )

(病历本入手)

从而引发出  Session 

 

例子 :

html: 

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4     <title></title>
 5 </head>
 6 <body>
 7    <form action="BinLiBen.ashx" method="post">
 8         <input type="text" name="username" />
 9         <input type="password" name="password" />
10         <input type="submit" name="login" value="登录" />
11    </form>
12 </body>
13 </html>
html

text.ashx:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 
 6 namespace WebApplication1
 7 {
 8     /// <summary>
 9     /// text 的摘要说明
10     /// </summary>
11     public class text : IHttpHandler
12     {
13 
14         public void ProcessRequest(HttpContext context)
15         {
16             context.Response.ContentType = "text/html";
17            
18 
19             HttpCookie cookie = context.Request.Cookies["ZhangBenId"];  //context.Response.SetCookie(new HttpCookie("ZhangBenId", id.ToString()));//把id写入病人的病历本     、、 所以当病人把病历本拿给医生看的时候 , 医生就知道id了
20             if (cookie == null)
21             {
22                 context.Response.Redirect("Login.ashx");
23             }
24             else
25             {
26                 Guid id = new Guid(cookie.Value);//获得病历本中的id
27                 if (SessionMgr.IsJiZhang(id))
28                 {
29                     string value = SessionMgr.Get(id);
30                     context.Response.Write(value);
31                 }
32                 else
33                 {
34                     context.Response.Redirect("Login.ashx");
35                 }
36             }
37         }
38 
39         public bool IsReusable
40         {
41             get
42             {
43                 return false;
44             }
45         }
46     }
47 }
一般处理程序

BinLiBen.ashx:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 
 6 namespace WebApplication1
 7 {
 8     /// <summary>
 9     /// BinLiBen 的摘要说明
10     /// </summary>
11     public class BinLiBen : IHttpHandler //模仿  Session 
12     {
13 
14         public void ProcessRequest(HttpContext context)
15         {
16             context.Response.ContentType = "text/html";
17             string login = context.Request["login"];
18             if (string.IsNullOrEmpty(login))
19             {
20                 string html = CommonHelper.RenderHtml("Login.htm", null);
21                 context.Response.Write(html);
22             }
23             else
24             {
25                 string username = context.Request["username"];
26                 string password = context.Request["password"];
27                 if (password == "123456")
28                 {
29                     //context.Response.SetCookie(new HttpCookie("YongHuMing",username));
30                     Guid id = Guid.NewGuid();//生成一个医生分配的用户编号
31                     SessionMgr.JiZhang(id, username);//计入账本   //相等于 医生端的病历本 
32 
33                     //把医生分配的病人编号写入病历本
34                     context.Response.SetCookie(new HttpCookie("ZhangBenId", id.ToString()));//把id写入病人的病历本
35                     context.Response.Redirect("text.ashx");
36                 }
37             }
38 
39 
40 
41 
42             context.Response.Write("Hello World");
43         }
44 
45         public bool IsReusable
46         {
47             get
48             {
49                 return false;
50             }
51         }
52     }
53 }
一般处理程序
 1   public static string RenderHtml(string name, object data)
 2         {
 3              VelocityEngine vltEngine = new VelocityEngine();
 4             vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
 5             vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/templates"));//模板文件所在的文件夹
 6             vltEngine.Init();
 7 
 8             VelocityContext vltContext = new VelocityContext();
 9             vltContext.Put("Model", data);//设置参数,在模板中可以通过$data来引用
10 
11             Template vltTemplate = vltEngine.GetTemplate(name);
12             System.IO.StringWriter vltWriter = new System.IO.StringWriter();
13             vltTemplate.Merge(vltContext, vltWriter);
14             return vltWriter.GetStringBuilder().ToString();
15         }
CommonHelper 封装的模版 NVelocity

SessionMgr:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 
 6 namespace WebApplication1
 7 {
 8     public class SessionMgr
 9     {
10         //static在.net framework运行的时候一直存在!这样就可以在服务器端保存(医生的账本)
11         private static Dictionary<Guid, string> zhangben = new Dictionary<Guid, string>();
12         public static void JiZhang(Guid id, string value)
13         {
14             zhangben[id] = value;
15         }
16 
17         public static bool IsJiZhang(Guid id)
18         {
19             return zhangben.Keys.Contains(id);
20         }
21 
22         public static string Get(Guid id)
23         {
24             return zhangben[id];
25         }
26     }
27 }
提供一个验证方案

来源 : 传智博客视频教程的一些个人总结 

 

 

 

 

 

        

原文地址:https://www.cnblogs.com/izhiniao/p/3746789.html