一般处理程序HttpHandler的应用
ashx
一般处理程序(HttpHandler)是·NET众多web组件的一种,ashx是其扩展名。一个httpHandler接受并处理一个http请求,类比于Java中的servlet。类比于在Java中需要继承HttpServlet类。在net中需要实现IHttpHandler接口,这个接口有一个IsReusable成员,一个待实现的方法ProcessRequest(HttpContextctx) 。程序在processRequest方法中处理接受到的Http请求。成员IsReusable指定此IhttpHandler的实例是否可以被用来处理多个请求。
.ashx程序适合产生供浏览器处理的、不需要回发处理的数据格式,例如用于生成动态图片、动态文本等内容。
例子:
1 前台Html:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ABC</title>
<link rel="stylesheet" type="text/css" href="../easyui/themes/default/easyui.css" />
<link rel="stylesheet" type="text/css" href="../easyui/themes/icon.css" />
<script type="text/javascript" src="../easyui/jquery.min.js"></script>
<script type="text/javascript" src="../easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="../easyui/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="index.js"></script> <style type="text/css">
.code
{
background-image:url(/Images/verifyCode.png);
font-family:Arial;
font-style:italic;
color:Red;
border:0;
padding:2px 3px;
letter-spacing:3px;
font-weight:bolder;
text-align:center;
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
-khtml-user-select: none;
user-select: none;
}
</style>
</head>
<body>
<div style="margin:0 auto;position:absolute;top:50%;margin-top:-115px;left:50%;margin-left:-200px">
<form id="fm" method="post" novalidate>
<div id="login" class="easyui-panel" title="登录" style="width:400px;padding:30px 70px 20px 70px;">
<div style="margin-bottom:10px">
<input id="userName1" name="userName" class="easyui-textbox" style="width:100%;height:40px;padding:12px" data-options="prompt:'用户名',iconCls:'icon-man',iconWidth:38">
</div>
<div style="margin-bottom:20px">
<input id="password1" name="password" class="easyui-textbox" type="password" style="width:100%;height:40px;padding:12px" data-options="prompt:'Password',iconCls:'icon-lock',iconWidth:38">
</div>
<div style="margin-bottom:20px; display:inline">
<input id="verifyCode" name="verifyCode" type="text" style="width:150px; height:12px; padding:12px; border: 1px solid #95BBE7 " />
<input id="checkCode" name="checkCode" value="XH59" class="code" type="text" onclick="createCode()" style="width:60px" readonly />
</div>
<div style="margin-top:20px; margin-bottom:20px">
<input id="remember1" name="remember" type="checkbox">
<span>记住密码</span>
</div>
<div>
<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" onclick="Login()" style="padding:5px 0px;width:100%;">
<span style="font-size:14px;">登录</span>
</a>
</div>
</div>
</form>
</div>
</body>
2 JQuery实现前台html中的Login():
function Login() {
$('#fm').form('submit', {
url: "../HttpHandler/LoginHandler.ashx?action=Login",
onSubmit: function () {
if (validate()) {
return $(this).form('validate');
}
else {
return false;
}
//return $(this).form('validate');
},
error: function () {
$.messager.alert('错误', '操作失败!', 'error');
},
success: function (result) {
var result = eval('(' + result + ')');
if (result.success) {
location.href = "../Pages/MainPage.htm";
} else {
$.messager.alert('提示', result.msg, 'warning');
}
}
});
}
3 后台一般程序处理:LoginHandler.ashx:
namespace Web.HttpHandler
{
public class LoginHandler : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string sReturnJson = string.Empty;
string action = ParamsofEasyUI.RequstString("action");
switch (action)
{
case "Login":
sReturnJson = Login();
break;
case "CheckLogin":
sReturnJson = CheckLogin();
break;
case "GetUserInfo":
sReturnJson = GetUserInfo();
break;
default:
break;
}
context.Response.Write(sReturnJson);
context.Response.End();
} private string GetUserInfo()
{
string result = string.Empty;
//读取保存的Cookie信息
HttpCookie cookies = HttpContext.Current.Request.Cookies["USER_COOKIE"];
if (cookies != null)
{
result += "{UserName:'";
result += cookies["UserName"];
result += "',UserPassword:'";
result += cookies["UserPassword"];
result += "',Checked:true}";
}
else
{
result += "{UserName:'";
result += "',UserPassword:'";
result += "',Checked:false}";
}
return result;
} private string CheckLogin()
{
if (HttpContext.Current.Session.Keys.Count == )
{
return "{success:false}";
} string curUser = HttpContext.Current.Session["userName"].ToString(); if (curUser == null)
{
return "{success:false}";
} if (HttpContext.Current.Application.AllKeys.Contains(curUser))
{
return "{success:true}";
}
else
{
return "{success:false}";
}
} private string Login()
{
string userName = ParamsofEasyUI.RequstForm("userName");
string password = ParamsofEasyUI.RequstForm("password");
bool blChecked = ParamsofEasyUI.RequstBool("remember"); string localPsw = Encoding.Default.GetString(Convert.FromBase64String(ConfigTools.Get(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory.ToString(), ConfigurationManager.AppSettings["ConfigFile"]), "CODE")));
if (userName.ToLower() == "admin" && password == localPsw)
{
if (blChecked)
{
HttpCookie cookie = new HttpCookie("USER_COOKIE");
//所有的验证信息检测之后,如果用户选择的记住密码,则将用户名和密码写入Cookie里面保存起来。
cookie.Values.Add("UserName", userName);
cookie.Values.Add("UserPassword", password);
//这里是设置Cookie的过期时间,这里设置一个星期的时间,过了一个星期之后状态保持自动清空。
cookie.Expires = System.DateTime.Now.AddDays(7.0);
HttpContext.Current.Response.Cookies.Add(cookie);
}
else
{
HttpCookie cookie = HttpContext.Current.Request.Cookies["USER_COOKIE"];
if (cookie != null)
{
//如果用户没有选择记住密码,那么立即将Cookie里面的信息清空,并且设置状态保持立即过期。
HttpContext.Current.Response.Cookies["USER_COOKIE"].Expires = DateTime.Now;
}
} string newGuid = Guid.NewGuid().ToString();
HttpContext.Current.Application.Lock();
HttpContext.Current.Application["admin"] = newGuid;
HttpContext.Current.Application.UnLock();
HttpContext.Current.Session[HttpContext.Current.Session.SessionID] = newGuid;
HttpContext.Current.Session.Add("UserName", userName);
return "{success:true}";
}
else
{
return "{success:false,msg:'用户名或密码错误!'}";
}
} public bool IsReusable
{
get
{
return false;
}
}
}
}
namespace Web.HttpHandler
{
public class ParamsofEasyUI
{
public static string RequstForm(string name)
{
return (HttpContext.Current.Request.Form[name] == null ? string.Empty : HttpContext.Current.Request.Form[name].ToString().Trim());
} public static string RequstString(string sParam)
{
return (HttpContext.Current.Request[sParam] == null ? string.Empty : HttpContext.Current.Request[sParam].ToString().Trim());
}
}
}
一般处理程序HttpHandler的应用的更多相关文章
- 002-一般处理程序(HttpHandler)
一般处理程序(HttpHandler):是一个实现System.Web.IHttpHandler接口的特殊类.任何一个实现了IHttpHandler接口的类,是作为一个外部请求的目标程序的前提.(凡是 ...
- 使用一般处理程序HTTPHandler下载文件
一般来说我们可以用HTTPHandler来处理一些简单的逻辑,比如验证码.下载文件等. 以下载word文档为例讲解一下如何在HHTPHandler中下载文件,不限于word文档,如果下载其他文件,需要 ...
- EasyUI - 使用一般处理程序 HttpHandler (.ashx)
以easyui中的panel中,使用url加载数据为列. 效果: html代码: <div id="p" style="padding: 10px;"&g ...
- 【IHttpHandler】HttpModule,HttpHandler,HttpHandlerFactory简单使用
这三个对象我们在开发Asp.net程序时经常会用到,似乎很熟悉,但有时候又不太确定.本文通过一个简单的例子来直观的比较一下这三个对象的使用. HttpModule:Http模块,可以在页面处理前后.应 ...
- HttpModule,HttpHandler,HttpHandlerFactory
HttpModule:Http模块,可以在页面处理前后.应用程序初始化.出错等时候加入自己的事件处理程序. HttpHandler:Http处理程序,处理页面请求 HttpHandlerFactory ...
- .net-一般处理程序及生命周期
IsReusable属性用来表示在IHttpHandlerFactory对象创建IHttpHandler的时候是否能够将这个Handler存入池中以便重用. 一般处理程序(HttpHandler):是 ...
- 【.net项目中。。】.net一般处理程序使用方法
1.基本原理图 IsReusable属性用来表示在IHttpHandlerFactory对象创建IHttpHandler的时候是否能够将这个Handler存入池中以便重用. 一般处理程序(HttpHa ...
- C#强化系列:HttpModule,HttpHandler,HttpHandlerFactory简单使用
这三个对象我们在开发Asp.net程序时经常会用到,似乎很熟悉,但有时候又不太确定.本文通过一个简单的例子来直观的比较一下这三个对象的使用.HttpModule:Http模块,可以在页面处理前后.应用 ...
- asp.net页面生命周期
Asp.Net页面生命周期 本文转载自:http://www.cnblogs.com/xhwy/archive/2012/05/20/2510178.html 一.什么是Asp.Net页面生命周期 当 ...
随机推荐
- Mysql 锁机制和事务
InnoDB 锁机制 InnoDB存储引擎支持行级锁 其大类可以细分为共享锁和排它锁两类 共享锁(S):允许拥有共享锁的事务读取该行数据.当一个事务拥有一行的共享锁时,另外的事务可以在同一行数据也获得 ...
- SpringBoot入门 (十四) Security安全控制
本文记录在SpringBoot使用SpringSecurity进行安全访问控制. 一 什么是Security Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访 ...
- 快速搭建一个“微视”类短视频 App
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯云视频发表于云+社区专栏 关注公众号"腾讯云视频",一键获取 技术干货 | 优惠活动 | 视频方案 " ...
- <Think Python>中斐波那契使用memo实现的章节
If you played with the fibonacci function from Section 6.7, you might have noticed thatthe bigger th ...
- Linux Tomcat日志查看实用命令
实用命令: 查看tomcat运行日志 tail -f catalina.out 通过关键字搜索查看日志 cat jeewx-2015-09-20.log | grep 验证码 查看固定时间日志 cat ...
- 第一次项目上Linux服务器(三:安装Tomcat及相关命令)
一.下载Tomcat 去官网:http://tomcat.apache.org/ 找到要下载的Tomcat 本人下载的是apache-tomcat-8.5.29.tar.gz,百度云资源链接:链接:h ...
- 关于《阿里巴巴Java开发规约》插件的安装与使用
一.安装 二.idea插件的安装与使用 https://github.com/alibaba/p3c/tree/master/idea-plugin#run-plugin Idea Plugin Pr ...
- Java - 生产者消费者问题
Java多线程系列--“基础篇”11之 生产消费者问题 概要 本章,会对“生产/消费者问题”进行讨论.涉及到的内容包括:1. 生产/消费者模型2. 生产/消费者实现 转载请注明出处:http://ww ...
- 微信小程序 引入公共页面的几种情况
1.不带参数 首先在pages文件夹中新建一个template文件夹,文件夹中新建一个template.wxml文件,代码如下 <!--template.wxml--> <templ ...
- 使用ThinkPHP实现生成/校验验证码功能
首先了解父类Verity.class.php(ThinkPHP/Library/Think/Verity.class.php)中的一些函数 1:check() 校验验证码是否正确 2:entry()输 ...