02-一般处理程序基础

HttpHandler(ashx)

不用“网站WebSite”,用WebApplication
l 新建一个【一般处理程序】Test1.ashx, ProcessRequest中写
• context.Response.ContentType = "text/html";
• string username = context.Request["name"];
• context.Response.Write(name + "<font color='red'>你好
</font>");
l Test.ashx?name=yzk
l 每当用户请求访问ashx页面的时候,ProcessRequest方法就会被调用
,在这里通过访问context.Request获得访问者的请求参数等。然后在
ProcessRequest中通过context.Response向浏览器发回数据给浏览器。
l 调试形式启动项目,修改地址栏访问ashx。
l 练习:开发一个登录功能,登录成功则显示“欢迎光临”页面,否则显
示一个“登录失败”的图片页面。

表单提交

Html表单<form>可以自动给服务器提交参数(get是通过url,
post是通过报文体,后面会讲区别),不用用户自己拼url。
action指定把表单内容提交给谁。
l 浏览器向服务器端提交数据,被提交数据的表单(input、select
、textarea等)放到form中,form中通过action属性设定表单被
提交给哪个页面,为了在服务端取出表单项的值,需要在HTML
中为表单元素设定name属性
l 注意id是给JS操作Dom用的,name才是提交给服务器用的。id
不能重复,name可以重复。
l 服务器端用context.Request["username"]来根据表单项的name
来获得提交的属性值。
l checkbox没选中为null,选中为"on"。
l 用表单重写登录程序。method修改为get和post看报文区别。

强调

当我们点击【登录】按钮以后是浏览器将用户填写的文本框等控件中的
值“提取”出来发送给服务器,而不是服务器来读取用户填写的这个页
面。
l 哪些标签的哪些值会被提交给服务器呢?将用户填写的内容提交到服务
器有如下几个条件(使用浏览器监视网络请求验证):
• 只能为 input、textarea、select三种类型的标签。只有这些标签用户才可能
修改值,<label>、<p>、<font>等标签仅供显示用,没有提交到服务器的必
要。当input=submit的时候,只有被点击的按钮的value才会被提交。
• 只有三种标签的value属性的值才会提交给服务器input标签有title、type、
disabled等属性,但是这些属性都是供显示用的,用户并不能修改。
• 标签必须设定name属性。如果要将标签的value属性值提交到服务器,则必
须为标签设定name属性,提交到服务器的时候将会以“name=value”的键值
对的方式提交给服务器。name是给服务器用的,id是给Dom用的。对于
RadioButton,同name的为一组,选中的RadioButton的value被提交到服务
器。
• 放到form标签内。只有放到form标签内的标签才可能会被提交到服务器,
form之外的input等标签被忽略。

Get与Post

l get(默认值)是通过URL传递表单值,post传递的表单值是隐藏到http
报文中,url中看不到。
l 区别(常考):get是通过url传递表单值,post通过url看不到表单域的值;
get传递的数据量是有限的,如果要传递大数据量不能用get,比如
type=“file”上传文章、type=“password”传递密码或者<textarea>发表大
段文章,post则没有这个限制。post区别:无法通过url在其他用户中还
原(发过去的网址显示不对)
l Get方式URL数据格式。服务端文件名后跟着“?”,由于客户端可能向
服务器端提交多个键值对,键值对之间用“&”进行分割,如果URL中有
汉字、特殊符号等,则需要对URL进行编码。

http协议简介

l Http协议的几个概念:
• 长连接,短连接。
• 1.连接(Connection):浏览器和服务器之间传输数据的通道。一般
请求完毕就关闭,http不保持连接。不保持连接会降低处理速度(
因为建立连接速度很慢),保持连接的话就会降低服务器的处理的
客户端并发请求数,而不保持连接服务器可以处理更多的请求。
• 2.请求(Request):浏览器向服务器发送的“我要***”的消息,包含
请求的类型、请求的数据、浏览器的信息(语言、浏览器版本等)
• 3.响应(Response):服务器对浏览器请求的返回的数据,包含是否
成功、错误码等。

http协议报文

l 请求
• GET / HTTP/1.1表示向服务器用GET方式请求首页,使用HTTP/1.1协议
• User-Agent为浏览器的版本信息。通过这个信息可以读取浏览器是IE还是FireFox
、支持的插件、.Net版本等
• Referer:来源页面、所属页面
l 响应:
• 响应码:“200” : OK; “302” : Found 暂时转移,用于重定向,
Response.Redirect()会让浏览器再请求一次重定向的地址,重定向的请求是Get
方式; "404" : Not Found 未找到。500 服务器错误
l Content-Type: text/html; charset=utf-8 表示返回数据的类型
l 服务器通过Content-Type告诉客户端响应的数据的类型,这样浏览器就根据返回数据
的类型来进行不同的处理,如果是图片类型就显示,如果是文本类型就直接显示内容
,如果用html类型就用浏览器显示内容.常用Content-Type:text/html、image/gif、
image/jpeg、text/plain、text/javascript。这是为什么要的ashx中设置contenttype
的原因,试着改成text/plain

不简单的Redirect

l Redirect是向浏览器发回302重定向,是通知浏览器“请重新访
问url这个网址”,这个过程经历了服务器通知浏览器“请重新访
问url这个网址”和浏览器接到命令访问新网址的过程。
l 使用报文工具查看整个响应过程的Http报文。用Redirect因为是
浏览器自己去重新访问新网址的,所以在地址栏中是可以看到网
址的变化的。
l 在EditUI如何用Redirect来防止刷新浏览器时提示“重试”。

“返回”提交页面

l 1:把登录表单和登录处理放到一个页面中
2:在ashx输出提交消息可以,但是如果还想返回提交页面(提示
不能为空等错误信息等),那么需要自己绘制。
• context.Response.Write(@"
• <form action='入门1.ashx'>
• <input type='text' name='username' value="+username+@"
/><input type='text' /><input type='submit' name='sb' />
• </form>");
l 并没有真的返回提交页面,只是看起来像罢了。请求、处理、响

l 为了请求、返回的内容一样,将页面源码放到字符串常量中,模
板中有一些待填值的占位符,第一次进入页面的时候就直接访问
ashx,读取htm模板,将待填值占位符设置为空,然后输出到浏
览器。
l 根据是否有username字段来判断是第一次进入还是再次表单提
交进入,说明是提交进入的,就加载模板,并且进行占位符用计
算后的值替换,否则就将模板中的占位符清空直接输出给浏览者
l 刚进入hello2.ashx的时候是直接向浏览器输出内容,用户在输出
的内容中填入数值,再点击提交,服务器就知道“提交回来了”
(PostBack)
l 如果访问Hello2.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     /// TestHandler 的摘要说明
10     /// </summary>
11     public class TestHandler : IHttpHandler
12     {
13 
14         public void ProcessRequest(HttpContext context)
15         {
16             //context.Response.ContentType = "text/html";
17             //string action=context.Request["name"];
18             //int age = Convert.ToInt32(context.Request["age"]);
19             //string isVIP = context.Request["isVIP"];
20             //context.Response.Write("<font color='red'>Hello " + action + "</font>");
21             //context.Response.Write("<font color='green'>我今年"+age+"岁</font>");
22             //if (isVIP == "on")
23             //{
24             //    context.Response.Write("<br/>您是VIP");
25             //}
26             //else
27             //{
28             //    context.Response.Write("<br/>您是个P");
29             //}
30 
31             context.Response.ContentType = "text/plain";
32             context.Response.Write("<a href='http://www.rupeng.com'>如鹏网</a>");
33         }
34 
35         public bool IsReusable
36         {
37             get
38             {
39                 return false;
40             }
41         }
42     }
43 }
View Code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 
 6 namespace WebApplication1
 7 {
 8     /// <summary>
 9     /// VIP 的摘要说明
10     /// </summary>
11     public class VIP : IHttpHandler
12     {
13 
14         public void ProcessRequest(HttpContext context)
15         {
16             context.Response.ContentType = "text/plain";
17             string username=context.Request["username"];
18             string password = context.Request["password"];
19             if (username == "admin" && password == "123")
20             {
21                 context.Response.Write("ed2k://afdsaddddddddddddddddd");
22             }
23             else
24             {
25                 context.Response.Redirect("http://rupeng.com");
26             }
27         }
28 
29         public bool IsReusable
30         {
31             get
32             {
33                 return false;
34             }
35         }
36     }
37 }
View Code
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     您不能看,没交钱成为会员怎么能看呢?
8 </body>
9 </html>
View Code
 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="TestHandler.ashx" method="get">
 8     姓名:<input type="text" name="name" /><br />
 9     年龄:<input type="text" name="age" /><br />
10     <input type="text" name="name" disabled="disabled" />
11    <input type="checkbox" name="isVIP" /><br />
12    没有Name:<input type="text" /><br />
13    <div name="aaaaa">哈哈哈</div>
14    <textarea name="msg"></textarea>
15    <select name="prov">
16         <option value="bj">北京</option>
17        <option value="tj">天津</option>
18       <option value="sh">上海</option>
19    </select>
20     <input type="submit" />
21     <label></label>
22    <img />
23    <br />
24    性别:
25    <ul>
26     <li><input type="radio" name="gender" value="male" /></li>
27     <li><input type="radio" name="gender" value="female" /></li>
28      <li><input type="radio" name="gender" value="nhz" />女汉子</li>
29    </ul>
30    民族:
31    <ul>
32     <li><input type="radio" name="national" value="han" />汉族</li>
33     <li><input type="radio" name="national" value="man" />满族</li>
34      <li><input type="radio" name="national" value="hui" />回族</li>
35    </ul>
36 </form>
37 <input type="text" name="height" />
38 </body>
39 </html>
View Code
原文地址:https://www.cnblogs.com/liuslayer/p/4722610.html