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

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

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

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

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

 public void ProcessRequest(HttpContext context)

         {

             context.Response.ContentType = "text/plain";

             context.Response.Write("<a href='http://www.rupeng.com'>如鹏网</a>");

         }

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

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

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;

  public class CommonHelper
{
public static string RenderHtml(string name, object data)
{
VelocityEngine vltEngine = new VelocityEngine();
vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/templates"));//模板文件所在的文件夹
vltEngine.Init(); VelocityContext vltContext = new VelocityContext();
vltContext.Put("Model", data);//设置参数,在模板中可以通过$data来引用 Template vltTemplate = vltEngine.GetTemplate(name);
System.IO.StringWriter vltWriter = new System.IO.StringWriter();
vltTemplate.Merge(vltContext, vltWriter);
return vltWriter.GetStringBuilder().ToString();
}
}

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

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

怎么记住提交的值呢 ?

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

Html中:

Ashx中:

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

下面介绍 Cookie

           Cookie

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

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

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

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

(病历本入手)

从而引发出  Session

例子 :

html:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<form action="BinLiBen.ashx" method="post">
<input type="text" name="username" />
<input type="password" name="password" />
<input type="submit" name="login" value="登录" />
</form>
</body>
</html>

html

text.ashx:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace WebApplication1
{
/// <summary>
/// text 的摘要说明
/// </summary>
public class text : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html"; HttpCookie cookie = context.Request.Cookies["ZhangBenId"]; //context.Response.SetCookie(new HttpCookie("ZhangBenId", id.ToString()));//把id写入病人的病历本 、、 所以当病人把病历本拿给医生看的时候 , 医生就知道id了
if (cookie == null)
{
context.Response.Redirect("Login.ashx");
}
else
{
Guid id = new Guid(cookie.Value);//获得病历本中的id
if (SessionMgr.IsJiZhang(id))
{
string value = SessionMgr.Get(id);
context.Response.Write(value);
}
else
{
context.Response.Redirect("Login.ashx");
}
}
} public bool IsReusable
{
get
{
return false;
}
}
}
}

一般处理程序

BinLiBen.ashx:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace WebApplication1
{
/// <summary>
/// BinLiBen 的摘要说明
/// </summary>
public class BinLiBen : IHttpHandler //模仿 Session
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
string login = context.Request["login"];
if (string.IsNullOrEmpty(login))
{
string html = CommonHelper.RenderHtml("Login.htm", null);
context.Response.Write(html);
}
else
{
string username = context.Request["username"];
string password = context.Request["password"];
if (password == "")
{
//context.Response.SetCookie(new HttpCookie("YongHuMing",username));
Guid id = Guid.NewGuid();//生成一个医生分配的用户编号
SessionMgr.JiZhang(id, username);//计入账本 //相等于 医生端的病历本 //把医生分配的病人编号写入病历本
context.Response.SetCookie(new HttpCookie("ZhangBenId", id.ToString()));//把id写入病人的病历本
context.Response.Redirect("text.ashx");
}
} context.Response.Write("Hello World");
} public bool IsReusable
{
get
{
return false;
}
}
}
}

一般处理程序

   public static string RenderHtml(string name, object data)
{
VelocityEngine vltEngine = new VelocityEngine();
vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/templates"));//模板文件所在的文件夹
vltEngine.Init(); VelocityContext vltContext = new VelocityContext();
vltContext.Put("Model", data);//设置参数,在模板中可以通过$data来引用 Template vltTemplate = vltEngine.GetTemplate(name);
System.IO.StringWriter vltWriter = new System.IO.StringWriter();
vltTemplate.Merge(vltContext, vltWriter);
return vltWriter.GetStringBuilder().ToString();
}

CommonHelper 封装的模版 NVelocity

SessionMgr:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace WebApplication1
{
public class SessionMgr
{
//static在.net framework运行的时候一直存在!这样就可以在服务器端保存(医生的账本)
private static Dictionary<Guid, string> zhangben = new Dictionary<Guid, string>();
public static void JiZhang(Guid id, string value)
{
zhangben[id] = value;
} public static bool IsJiZhang(Guid id)
{
return zhangben.Keys.Contains(id);
} public static string Get(Guid id)
{
return zhangben[id];
}
}
}

提供一个验证方案

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

浏览器与服务器间的交互(客服端 <---> 服务器)的更多相关文章

  1. java UDP 通信:服务端与客服端

    import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import j ...

  2. 利用java在服务器和客服端建立连接,进行通讯(代码实例)

    客服端代码:有注释 package javanet; import java.io.IOException; import java.io.InputStream; import java.io.Ou ...

  3. C# 向服务器上传文件(客服端winform、服务端web)

    转载 首先写客服端,winform模拟一个post提交: /// <summary> /// 将本地文件上传到指定的服务器(HttpWebRequest方法) /// </summa ...

  4. Python网络编程UDP服务器与客服端简单例子

    [转载] https://blog.csdn.net/hu330459076/article/details/7868028 UDP服务器代码: #!/usr/bin/env python # -*- ...

  5. TCP服务器端和客服端(一)

    就是一个客服端(Socket)和服务器(ServerSocket)端的链接间.我的理解是一个服务端可以链接多个客服端. 在客服端有输入流outPutStream. 用于发送数据 在服务器端有输出流.i ...

  6. Live555 分析(三):客服端

    live555的客服端流程:建立任务计划对象--建立环境对象--处理用户输入的参数(RTSP地址)--创建RTSPClient实例--发出DESCRIBE--发出SETUP--发出PLAY--进入Lo ...

  7. android客服端+eps8266+单片机+路由器之远程控制系统

    用android客服端+eps8266+单片机+路由器做了一个远程控制的系统,因为自己是在实验室里,所以把实验室的门,灯做成了远程控制的. 控制距离有多远------只能说很远很远,只要你手机能上网的 ...

  8. 关于阿里云centos7安装svn,客服端无法链接的问题

    阿里云的centos7的版本中,通过yum安装了subversion之后,svn客服端无法链接svn服务器. 首先确定服务器的安全组策略中的3690端口是否打开 然后确定svnserve配置是否正确, ...

  9. 客服端JavaScript线程模型

    JavaScript语言核心并不包含任何线程机制,并且客服端JavaScript传统上没有定义任何线程机制.HTML5定义了一种作为后台线程的“WebWorker",但是客服端JavaScr ...

随机推荐

  1. 关于codestyle

    如果你的代码易于阅读,那么代码中bug也将会很少,因为一些bug可以很容被调试,并且,其他开发者参与你项目时的门槛也会比较低.因此,如果项目中有多人参与,采取一个有共识的编码风格约定非常有必要. 以t ...

  2. IPv6地址的ping、telnet等操作

    最近在研究https协议是如何传输数据的,用wireshark抓包分析,发现客户机和google网站在传输数据时使用了IPv6地址,于是相对ipv6地址测试下基本的功能. ping功能,直接使用pin ...

  3. ASP.NET jQuery 随笔 使用allValidator插件简单实现客户端验证功能

    首先放出该插件的下载地址:http://pan.baidu.com/s/1Aa3yD,里面有帮助文档,详细了解可以自行下载学习,本章只讲解一些基本的验证功能,页面代码如下: <%@ Page L ...

  4. QTableView另类打印解决方案(复用render函数去解决print问题)

    Qt QTableView另类打印解决方案     上回书说道Qt的model/view,我就做了个demo用于显示数据库中的内容.没想到tableview的打印竟然成了问题.我困惑了,难道Qt不应该 ...

  5. FMX相当于在界面上自己又做了一个小操作系统

    FMX的自画界面我也不看好,比如复制粘贴,太丑了,系统做得很好很精细的复制粘贴界面,就是无法调出,比如MIUI,复制粘贴还能有个放大镜,可以选择到屏幕边缘的文字,可以选择剪贴板内多个可粘贴的文字:还有 ...

  6. 基于Visual C++2013拆解世界五百强面试题--题6-double类型逆序

    请设计一个函数,不许用到字符串函数,用数学运算,将double类型数据转换,例如123.456转换成654.321 首先想到依次提取他的每一个位数,然后进行运算,移动每一位数到相应位置,结果相加就能逆 ...

  7. Dancing Stars on Me(判断正多边形)

    Dancing Stars on Me Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Ot ...

  8. 定制ToolChain for ARM

    **************************************************************************编写:王卫无,北京讯业互联科技有限公司版本号:V1. ...

  9. hadoop高速扫盲帖,从零了解hadoop

    1.MapReduce理论简单介绍 1.1 MapReduce编程模型 MapReduce採用"分而治之"的思想,把对大规模数据集的操作,分发给一个主节点管理下的各个分节点共同完毕 ...

  10. c 判断水仙花数,质数(素数)

    #include<stdio.h> #include<stdbool.h> //水仙花数--各位立方和等于本身 void sXh() { int x,y,z; printf(& ...