浏览器与服务器间的交互(客服端 <---> 服务器)
浏览器与服务器间的交互(客服端 <---> 服务器)
请求--->处理--->响应
对类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
//***主要拿来生成动态图片和动态文本,比传统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];
}
}
}
提供一个验证方案
来源 : 传智博客视频教程的一些个人总结
浏览器与服务器间的交互(客服端 <---> 服务器)的更多相关文章
- java UDP 通信:服务端与客服端
import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import j ...
- 利用java在服务器和客服端建立连接,进行通讯(代码实例)
客服端代码:有注释 package javanet; import java.io.IOException; import java.io.InputStream; import java.io.Ou ...
- C# 向服务器上传文件(客服端winform、服务端web)
转载 首先写客服端,winform模拟一个post提交: /// <summary> /// 将本地文件上传到指定的服务器(HttpWebRequest方法) /// </summa ...
- Python网络编程UDP服务器与客服端简单例子
[转载] https://blog.csdn.net/hu330459076/article/details/7868028 UDP服务器代码: #!/usr/bin/env python # -*- ...
- TCP服务器端和客服端(一)
就是一个客服端(Socket)和服务器(ServerSocket)端的链接间.我的理解是一个服务端可以链接多个客服端. 在客服端有输入流outPutStream. 用于发送数据 在服务器端有输出流.i ...
- Live555 分析(三):客服端
live555的客服端流程:建立任务计划对象--建立环境对象--处理用户输入的参数(RTSP地址)--创建RTSPClient实例--发出DESCRIBE--发出SETUP--发出PLAY--进入Lo ...
- android客服端+eps8266+单片机+路由器之远程控制系统
用android客服端+eps8266+单片机+路由器做了一个远程控制的系统,因为自己是在实验室里,所以把实验室的门,灯做成了远程控制的. 控制距离有多远------只能说很远很远,只要你手机能上网的 ...
- 关于阿里云centos7安装svn,客服端无法链接的问题
阿里云的centos7的版本中,通过yum安装了subversion之后,svn客服端无法链接svn服务器. 首先确定服务器的安全组策略中的3690端口是否打开 然后确定svnserve配置是否正确, ...
- 客服端JavaScript线程模型
JavaScript语言核心并不包含任何线程机制,并且客服端JavaScript传统上没有定义任何线程机制.HTML5定义了一种作为后台线程的“WebWorker",但是客服端JavaScr ...
随机推荐
- html 表头固定
<div style="width: 100%; min-width: 300px; padding-right: 10px;"> <table style=&q ...
- jquery弹出层拖拽
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> <met ...
- springmvc附件上传核心代码
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.Comm ...
- JS常用方法函数(2)
31.判断是否Touch屏幕 function isTouchScreen(){ return (('ontouchstart' in window) || window.DocumentTouch ...
- 一键搜索之Win10锁屏壁纸
前天无意发现win10的锁屏壁纸挺漂亮的,就想着能不能保存下来?网上一搜果然有资料,当时参考的连接如下 http://jingyan.baidu.com/article/fedf07375ea2513 ...
- Spring Boot Admin Reference Guide
1. What is Spring Boot Admin? Spring Boot Admin is a simple application to manage and monitor your S ...
- vs2013 linq to mysql
安装文件 1.下载安装文件 2.创建项目 3.创建mysql数据连接 4.新建ADO.NET数据模型 项目实现代码 using (WorkerContainer db = new WorkerCont ...
- Android JNI入门第一篇——HelloJni
android支持使用NDK开发C程序,关于配置NDK环境问题应该不用再赘述了,这个网上有很多,这里通过一篇实例来讲述简单的JNI开发,大家可以参考这篇文章(Get Your Eclipse-Inte ...
- Ext JS学习第九天 Ext基础之 扩展原生的javascript对象
此文来记录学习笔记: •Ext对于原生的javascript对象进行了一系列的扩展,我们把他们掌握好,更能深刻的体会Ext的架构,从而对我们的web开发更好的服务, 源码位置,我们可以从开发包的这个位 ...
- C++学习之虚继承
http://blog.csdn.net/wangxingbao4227/article/details/6772579 C++中虚拟继承的概念 为了解决从不同途径继承来的同名的数据成员在内存中有不同 ...