HttpResponse对象

为了响应客户端的请求,同样定义了代表响应的类:HttpResponse类,它也定义在命名空间System.Web下,提供向客户端响应的方法和属性。

HttpResponse常用属性和方法

响应对象用于向浏览器发回服务器处理的结果,这个结果是Http协议中的响应消息。这个消息中包含:状态行,头部,主体部分。

StatusCode:用于设置状态码,例如:200,500等等

StatusDescription:状态的描述信息。

Status:可以直接设置状态行的内容,例如:200 OK

对于响应的头部信息,大多数可以通过HttpResponse的属性直接设置。对于没有对应属性的头部信息,还可以通过AddHeader方法处理。这个方法接收两个字符串参数,一个回应头的名称,一个回应头所对应的值。

ContentType:响应内容的类型,设置该属性为itexthtml,在输出到Http消息中对应的头部信息为Content-Type:texthtml.

AddHeader方法定义如下:

       //
        // Summary:
        //     Adds an HTTP header to the output stream. System.Web.HttpResponse.AddHeader(System.String,System.String)
        //     is provided for compatibility with earlier versions of ASP.
        //
        // Parameters:
        //   name:
        //     The name of the HTTP header to add value to.
        //
        //   value:
        //     The string to add to the header.
        public void AddHeader(string name, string value);

例如,在使用http下载的时候,需要设置Content-Disposition回应头,但在HttpResponse中并没有对应的属性,那么可以通过下面的方式完成:

 Response.AddHeader("Content-Disposition","attachment; filename=download.zip");

与HttpRequest类似,HttpResponse也有一个Cookies属性,这个属性是用来向浏览器响应Cookie的集合,每个加入到这个集合中的HttpCookie对象,将在回应头中生成一行如下的回应头:

如服务端创建HttpCookie对象加入到Cookies集合中,向浏览器输出

            HttpCookie n = new HttpCookie("n", "wolfy");
            HttpCookie p = new HttpCookie("p", "1234");
            Response.Cookies.Add(n);
            Response.Cookies.Add(p);

查看响应头信息

响应的内容部分是通过流来完成的,HttpResponse对象的OutputStream属性引用了输出到Http回应的输出流。

       //
        // Summary:
        //     Enables binary output to the outgoing HTTP content body.
        //
        // Returns:
        //     An IO System.IO.Stream representing the raw contents of the outgoing HTTP
        //     content body.
        //
        // Exceptions:
        //   System.Web.HttpException:
        //     OutputStream is not available.
        public Stream OutputStream { get; }

多数情况下,在web开发中,服务器的处理结果是一个文本网页,Output属性指向了一个经过包装的输出流,文本类型的输出流。这样我们可以非常简单的输出文本内容。该属性的定义如下:

       //
        // Summary:
        //     Enables output of text to the outgoing HTTP response stream.
        //
        // Returns:
        //     A System.IO.TextWriter object that enables custom output to the client.
        public TextWriter Output { get; set; }

对于文本内容,所使用的编码由属性ContentEncoding决定,该属性的定义如下:

        //
        // Summary:
        //     Gets or sets the HTTP character set of the output stream.
        //
        // Returns:
        //     A System.Text.Encoding object that contains information about the character
        //     set of the current response.
        //
        // Exceptions:
        //   System.ArgumentNullException:
        //     Attempted to set System.Web.HttpResponse.ContentEncoding to null.
        public Encoding ContentEncoding { get; set; }

注意

在Http消息中,头部必需在主体部分的前面,所以,在已经输出主体部分之后,是不能再次输出消息头的。默认情况下,HttpResponse对输出的内容进行了缓冲,所以通过HttpResponse输出的内容并没有立即输出到http中,还可以进行修改。这个时候,是可以在使用输出流之后再次设置回应头的,但,如果关闭了HttpResponse的缓冲,那么,这样的操作将会出错。HttpResponse的BufferOutput属性是一个bool类型,用于设置是否缓冲输出的内容。

HttpResponse类还提供一些特殊的方法,以以直接返回某些特定的内容,例如,请求的重定向,根据http协议,服务器可以返回iyige状态码为302的回应通知浏览器应该向另外一个地址发出请求。回应头重的Location指出请求的地址.HttpResponse的Redirect方法,可以很方便帮我们实现这个功能,我们只需将重定向的url传给这个方法,HttpResponse返回一个状态码为302的回应,通知浏览器重定向新的Url发出请求。

原文地址:https://www.cnblogs.com/wolf-sun/p/5211233.html