Http Handler Simple Demo

D:\visual studio 2010\Projects\WebAppControlTest\WebAppControlTest\Web.config

      <httpHandlers>
        <add verb="POST" path="*MyHandlerTest.ashx*" type="WebAppControlTest.MyHandlerTest,WebAppControlTest"/>
      </httpHandlers>
  
    
  </system.web>

D:\visual studio 2010\Projects\WebAppControlTest\WebAppControlTest\ASHXTest.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ASHXTest.aspx.cs"  %>

<!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 id="Head1" runat="server">
    <title>mytest</title>
    <script type="text/javascript">
        function $(s) { if (document.getElementById) { return eval('document.getElementById("' + s + '")'); } else { return eval('document.all.' + s); } }

        function createXMLHTTP() {
            var xmlHttp = false;
            var arrSignatures = ["MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0",
                         "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP",
                         "Microsoft.XMLHTTP"];
            for (var i = 0; i < arrSignatures.length; i++) {
                try {
                    xmlHttp = new ActiveXObject(arrSignatures[i]);
                    return xmlHttp;
                }
                catch (oError) {
                    xmlHttp = false; //ignore
                }
            }
            // throw new Error("MSXML is not installed on your system."); 
            if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
                xmlHttp = new XMLHttpRequest();
            }
            return xmlHttp;
        }

        var xmlReq = createXMLHTTP();

        // 发送ajax处理请求(这里简单验证旧密码的有效性)
        function validateOldPwd(oTxt) {
            var url = "/MyHandlerTest.ashx?action=modifyPwd&pwd=" + escape(oTxt.value); //.ashx文件
            xmlReq.open("get", url, true);
            xmlReq.setRequestHeader("If-Modified-Since", "0");
            xmlReq.onreadystatechange = callBack;
            xmlReq.send(url); // 发送文本
        }

        function callBack() {
            if (xmlReq.readyState == 4) {
                if (xmlReq.status == 200) {
                    alert(xmlReq.responseText); // 接收文本
                }
                else if (xmlReq.status == 404) {
                    alert("Requested URL is not found.");
                } else if (xmlReq.status == 403) {
                    alert("Access denied.");
                } else
                    alert("status is " + xmlReq.status);
            }
        }

    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input id="txtOldPwd" type="text" onblur="validateOldPwd(this)" />
    </div>
    </form>
</body>
</html>

D:\visual studio 2010\Projects\WebAppControlTest\WebAppControlTest\MyHandlerTest.ashx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
//未能从程序集“WebAppControlTest”中加载类型“WebAppControlTest.MyHandlerTest”。
namespace WebAppControlTest
{
    /// <summary>
    /// Summary description for MyHandlerTest
    /// </summary>
    public class MyHandlerTest : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ClearContent();
            context.Response.ContentType = "text/plain";
            context.Response.Cache.SetCacheability(HttpCacheability.NoCache); //无缓存

            string action = context.Request.Params["action"]; //外部请求
            if (action == "modifyPwd") //用户改密码
            {
                string oldPwd = context.Request.Params["pwd"];
                context.Response.Write(oldPwd);
                /*
                //在ashx文件用使用Session必须实现IRequiresSessionState接口
                //Session["LogedUser"]是登录用户的会话,用户名和密码都是test
                if (oldPwd.ToUpper() != ((context.Session["LogedUser"]) as Customer).Password.ToUpper()) //用户输入的旧密码和当前登录用户的不相同
                {
                    context.Response.Write("旧密码输入错误!");
                }
                else
                {
                    context.Response.Write("旧密码输入正确!");
                }
                */

            }


            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return true;
            }
        }

    }
}

 

posted @ 2018-02-06 16:27  sky20080101  阅读(85)  评论(0)    收藏  举报