3

我正在尝试在 HTTP 响应中设置自定义 HTTP 状态标头。例如:

400 Why do you want to do that

我通过在一个中设置StatusDescription一个HttpResponseIHttpHandler

<%@ WebHandler Language="C#" Class="Foo" %>

using System;
using System.Web;

public class Foo : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest;
        context.Response.StatusDescription = "Why do you want to do that";

            //Unnecessary; the string already contains this
        context.Response.Status = "400 Why do you want to do that";

        context.Response.ContentType = "text/plain";
        context.Response.Write("Hello World");
    }

    public bool IsReusable { get { return false; } }
}

但是在来自服务器的响应标头中,状态行不包含我告诉它包含的内容,而是继续包含默认的 StatusDescription:

400 Bad Request

在此处输入图像描述

在此处输入图像描述

在通用处理程序StatusDescription中如何更改标头?HttpResponse

奖金喋喋不休

共有三个属性:

  • int StatusCode400
  • String StatusDescriptionBad Request
  • String Status400 Bad Request

设置StatusCode将改变StatusDescriptionStatus

                    StatuCode  StatusDescription  Status              
==================  =========  =================  ====================
initial             200        OK                 200 OK
StatusCode=400;
                    400        Bad Request        400 Bad Request
StatusDescription = "Too much want";
                    400        Too much want      400 Too much want

它也以另一种方式工作:

                    StatuCode  StatusDescription  Status              
==================  =========  =================  ====================
initial             200        OK                 200 OK
StatusDescription="Brilliant";
                    200        Brilliant          200 Brilliant
StatusCode=451;
                    451        Brilliant          451 Brilliant

另一种方式

                    StatuCode  StatusDescription  Status              
==================  =========  =================  ====================
initial             200        OK                 200 OK
Status="451 Brilliant"
                    451        Brilliant          451 Brilliant

奖金奖金喋喋不休

来自RFC 2616 - 超文本传输​​协议 - HTTP/1.1

6.1.1 状态码和原因短语

下面介绍了为 HTTP/1.1 定义的数字状态代码的各个值,以及一组相应的原因短语的示例。此处列出的原因短语只是建议——它们可以被本地等效替换而不影响协议。

4

0 回答 0