zoukankan      html  css  js  c++  java
  • 今日工作心得:HttpContext.Response.Headers.Add 、HttpContext.Response.AddHeader 和HttpContext.Response.AppendHeader

    今天比较闲做ajax的练习的时候发现了一个比较奇怪的现象,就是写好的handler.ashx不能正常工作。后来查看了一下是这三个方法的区别造成的。

    第一版的程序:

    aspx:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="demoHeader.aspx.cs" Inherits="demoHeader" %>
    
    <!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 runat="server">
        <title></title>
    
        <script type="text/javascript" language="javascript">
            var r = new XMLHttpRequest;
            function add() {
                var a = document.getElementById("a").value;
                var b = document.getElementById("b").value;
                var url = "HandlerHeader.ashx";
    
                r.onreadystatechange = function() {
                    if (r.readyState == 4) {
                        if (r.status == 200) {
                            document.getElementById("result").innerHTML = r.getResponseHeader("c");
                        }
                    }
                };
                r.open("POST", url, true);
                r.setRequestHeader("a", a);
                r.setRequestHeader("b", b);
                r.send(null);
            }
        </script>
    
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <input id="a" type="text" />
            <input id="b" type="text" />
            <input id="btn" type="button" value="GetSum" onclick="add();" />
            <div id="result">
            </div>
        </div>
        </form>
    </body>
    </html>
    
    
    ashx:
    <%@ WebHandler Language="C#" Class="HandlerHeader" %>
    
    using System;
    using System.Web;
    
    public class HandlerHeader : IHttpHandler {
        
        public void ProcessRequest (HttpContext context) {
            context.Response.ContentType = "text/plain";
            int a = Convert.ToInt32(context.Request.Headers["a"]);
            int b = Convert.ToInt32(context.Request.Headers["b"]);
            int c = a + b;
            //context.Response.Write(c.ToString());
            context.Response.Headers.Add("c", c.ToString());
        }
     
        public bool IsReusable {
            get {
                return false;
            }
        }
    
    }
    

    不能正确运行,直接在浏览器中查看handler,ashx页面,提示“context.Response.Headers.Add("c", c.ToString()) ”这句代码需要在IIS集成管线中运行。从元数据查看该方法的注释说明中有这样的描述“操作要求 IIS 7.0 处于集成管线模式,并且要求 .NET Framework 至少为 3.0 版本。”。而我的iis是6.0的,估计是因为这个才不行吧,没有在IIS7.0上试是不是可以。

    后来把这一句改成了context.Response.AddHeader("c", c.ToString());和context.Response.AppendHeader("c", c.ToString());都是可以的。

    后边这两句话的注释说明中写着作用是将http头添加到输出流,不同的是前者有这样一句话“提供 System.Web.HttpResponse.AddHeader(System.String,System.String)是为了与 ASP 的先前版本保持兼容。”
    由此我推断,比较好的做法是使用后者:context.Response.AppendHeader("c", c.ToString());做http头操作比较好。

  • 相关阅读:
    第4月第1天 makefile automake
    第3月30天 UIImage imageWithContentsOfFile卡顿 Can't add self as subview MPMoviePlayerControlle rcrash
    第3月第27天 uitableviewcell复用
    learning uboot fstype command
    learning uboot part command
    linux command dialog
    linux command curl and sha256sum implement download verification package
    learning shell script prompt to run with superuser privileges (4)
    learning shell get script absolute path (3)
    learning shell args handing key=value example (2)
  • 原文地址:https://www.cnblogs.com/yuanyuan/p/1820768.html
Copyright © 2011-2022 走看看