HttpResponse 类

定义

封装来自 ASP.NET 操作的 HTTP 响应信息。

public ref class HttpResponse sealed
public sealed class HttpResponse
type HttpResponse = class
Public NotInheritable Class HttpResponse
继承
HttpResponse

示例

以下示例在请求页面时绘制三个重叠矩形。 代码首先将 属性设置为 ContentType image/jpeg,以便整个页面将呈现为 JPEG 图像。 然后,代码调用 Clear 方法,以确保不会随此响应一起发送无关的内容。 接下来,代码将 BufferOutput 属性设置为 true,以便在将页面发送到请求客户端之前完全处理页面。 然后创建用于绘制矩形的两个Graphics对象:和 Bitmap 对象。 在页面中创建的变量用作坐标来绘制矩形和显示在最大矩形内的字符串。

绘制三个矩形及其中显示的字符串时, Bitmap 将保存到 StreamOutputStream 属性关联的 对象,并且其格式设置为 JPEG。 代码调用 DisposeDispose 方法来释放两个绘图对象使用的资源。 最后,代码调用 Flush 方法,将缓冲的响应发送到请求客户端。

注意

在代码中,HttpResponse对象由 关键字 (keyword) Response引用。 例如, Response.Clear() 引用 HttpResponse.Clear 方法。 类 Page 具有一 Response 个名为 的属性,该属性公开 的当前实例 HttpResponse

<%@ Page Language="C#" %>
<%@ import Namespace="System.Drawing" %>
<%@ import Namespace="System.Drawing.Imaging" %>
<%@ import Namespace="System.Drawing.Drawing2D" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

    private void Page_Load(object sender, EventArgs e)
    {
// <snippet2>
        // Set the page's content type to JPEG files
        // and clears all content output from the buffer stream.
        Response.ContentType = "image/jpeg";
        Response.Clear();
    
        // Buffer response so that page is sent
        // after processing is complete.
        Response.BufferOutput = true;
// </snippet2>
    
        // Create a font style.
        Font rectangleFont = new Font(
            "Arial", 10, FontStyle.Bold);
    
        // Create integer variables.
        int height = 100;
        int width = 200;
    
        // Create a random number generator and create
        // variable values based on it.
        Random r = new Random();
        int x = r.Next(75);
        int a = r.Next(155);
        int x1 = r.Next(100);
    
        // Create a bitmap and use it to create a
        // Graphics object.
        Bitmap bmp = new Bitmap(
            width, height, PixelFormat.Format24bppRgb);
        Graphics g = Graphics.FromImage(bmp);
    
        g.SmoothingMode = SmoothingMode.AntiAlias;
        g.Clear(Color.LightGray);
    
        // Use the Graphics object to draw three rectangles.
        g.DrawRectangle(Pens.White, 1, 1, width-3, height-3);
        g.DrawRectangle(Pens.Aquamarine, 2, 2, width-3, height-3);
        g.DrawRectangle(Pens.Black, 0, 0, width, height);
    
        // Use the Graphics object to write a string
        // on the rectangles.
        g.DrawString(
            "ASP.NET Samples", rectangleFont,
            SystemBrushes.WindowText, new PointF(10, 40));
    
        // Apply color to two of the rectangles.
        g.FillRectangle(
            new SolidBrush(
                Color.FromArgb(a, 255, 128, 255)),
            x, 20, 100, 50);
    
        g.FillRectangle(
            new LinearGradientBrush(
                new Point(x, 10),
                new Point(x1 + 75, 50 + 30),
                Color.FromArgb(128, 0, 0, 128),
                Color.FromArgb(255, 255, 255, 240)),
            x1, 50, 75, 30);

// <snippet3>    
        // Save the bitmap to the response stream and
        // convert it to JPEG format.
        bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
    
        // Release memory used by the Graphics object
        // and the bitmap.
        g.Dispose();
        bmp.Dispose();
    
        // Send the output to the client.
        Response.Flush();
// </snippet3>
    }

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
    </form>
</body>
</html>
<%@ Page Language="VB" %>
<%@ import Namespace="System.Drawing" %>
<%@ import Namespace="System.Drawing.Imaging" %>
<%@ import Namespace="System.Drawing.Drawing2D" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

   Private Sub Page_Load(sender As Object, e As EventArgs)
' <snippet2>
      ' Set the page's content type to JPEG files
      ' and clears all content output from the buffer stream.
      Response.ContentType = "image/jpeg"
      Response.Clear()
      
      ' Buffer response so that page is sent
      ' after processing is complete.
      Response.BufferOutput = True
' </snippet2>
      
      ' Create a font style.
      Dim rectangleFont As New Font( _
          "Arial", 10, FontStyle.Bold)
      
      ' Create integer variables.
      Dim height As Integer = 100
      Dim width As Integer = 200
      
      ' Create a random number generator and create
      ' variable values based on it.
      Dim r As New Random()
      Dim x As Integer = r.Next(75)
      Dim a As Integer = r.Next(155)
      Dim x1 As Integer = r.Next(100)
      
      ' Create a bitmap and use it to create a
      ' Graphics object.
      Dim bmp As New Bitmap( _
          width, height, PixelFormat.Format24bppRgb)
      Dim g As Graphics = Graphics.FromImage(bmp)
      
      g.SmoothingMode = SmoothingMode.AntiAlias
      g.Clear(Color.LightGray)
      
      ' Use the Graphics object to draw three rectangles.
      g.DrawRectangle(Pens.White, 1, 1, width - 3, height - 3)
      g.DrawRectangle(Pens.Aquamarine, 2, 2, width - 3, height - 3)
      g.DrawRectangle(Pens.Black, 0, 0, width, height)
      
      ' Use the Graphics object to write a string
      ' on the rectangles.
      g.DrawString("ASP.NET Samples", rectangleFont, SystemBrushes.WindowText, New PointF(10, 40))
      
      ' Apply color to two of the rectangles.
      g.FillRectangle( _
          New SolidBrush( _
              Color.FromArgb(a, 255, 128, 255)), _
          x, 20, 100, 50)
      
      g.FillRectangle( _
          New LinearGradientBrush( _
              New Point(x, 10), _
              New Point(x1 + 75, 50 + 30), _
              Color.FromArgb(128, 0, 0, 128), _
              Color.FromArgb(255, 255, 255, 240)), _
          x1, 50, 75, 30)

' <snippet3>      
      ' Save the bitmap to the response stream and
      ' convert it to JPEG format.
      bmp.Save(Response.OutputStream, ImageFormat.Jpeg)
      
      ' Release memory used by the Graphics object
      ' and the bitmap.
      g.Dispose()
      bmp.Dispose()
      
      ' Send the output to the client.
      Response.Flush()
' </snippet3>
   End Sub 'Page_Load

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
    </form>
</body>
</html>

注解

类的方法和属性HttpResponse通过 ResponseHttpContextPageUserControl 类的 HttpApplication属性公开。

类的 HttpResponse 以下方法仅在回发方案中受支持,在异步回发方案中不受支持:

使用 UpdatePanel 控件更新页面的选定区域而不是使用回发更新整个页面时,将启用部分页面更新。 有关详细信息,请参阅 UpdatePanel 控件概述分页呈现概述

构造函数

HttpResponse(TextWriter)

初始化 HttpResponse 类的新实例。

属性

Buffer

获取或设置一个值,该值指示是否缓冲输出并在处理完整个响应之后发送它。

BufferOutput

获取或设置一个值,该值指示是否缓冲输出并在处理完整个页之后发送它。

Cache

获取网页的缓存策略(例如,过期时间、保密性设置和变化条款)。

CacheControl

获取或设置与 HttpCacheability 枚举值之一匹配的 Cache-Control HTTP 标头。

Charset

获取或设置输出流的 HTTP 字符集。

ClientDisconnectedToken

获取客户端断开连接时触发的 CancellationToken 对象。

ContentEncoding

获取或设置输出流的 HTTP 字符集。

ContentType

获取或设置输出流的 HTTP MIME 类型。

Cookies

获取响应 cookie 集合。

Expires

获取或设置在浏览器上缓存的页过期之前的分钟数。 如果用户在页面过期之前返回到该页,则显示缓存的版本。 提供 Expires,以便兼容 ASP 的早期版本。

ExpiresAbsolute

获取或设置从缓存中移删除缓存信息的绝对日期和时间。 提供 ExpiresAbsolute,以便兼容 ASP 的早期版本。

Filter

获取或设置一个包装筛选器对象,该对象用于在传输之前修改 HTTP 实体主体。

HeaderEncoding

获取或设置一个 Encoding 对象,该对象表示当前标头输出流的编码。

Headers

获取响应标头的集合。

HeadersWritten

获取指示是否已写入响应标头的值。

IsClientConnected

获取一个值,通过该值指示客户端是否仍连接在服务器上。

IsRequestBeingRedirected

获取指示客户端是否正在被传输到新位置的布尔值。

Output

实现到传出 HTTP 响应流的文本输出。

OutputStream

实现到传出 HTTP 内容主体的二进制输出。

RedirectLocation

获取或设置 HTTP Location 标头的值。

Status

设置返回到客户端的 Status 栏。

StatusCode

获取或设置返回给客户端的输出的 HTTP 状态代码。

StatusDescription

获取或设置返回给客户端的输出的 HTTP 状态字符串。

SubStatusCode

获取或设置一个限定响应的状态代码的值。

SupportsAsyncFlush

获取一个值,该值指示集合是否支持异步刷新操作。

SuppressContent

获取或设置一个值,该值指示是否将 HTTP 内容发送到客户端。

SuppressDefaultCacheControlHeader

获取或设置一个值,该值指示是否取消当前 HTTP 响应的默认 Cache Control: private 标头。

SuppressFormsAuthenticationRedirect

获取或设置指定重定向至登录页的 forms 身份验证是否应取消的值。

TrySkipIisCustomErrors

获取或设置一个值,该值指定是否禁用 IIS 7.0 自定义错误。

方法

AddCacheDependency(CacheDependency[])

将一组缓存依赖项与响应关联,这样,如果响应存储在输出缓存中并且指定的依赖项发生变化,就可以使该响应失效。

AddCacheItemDependencies(ArrayList)

使缓存响应的有效性依赖于缓存中的其他项。

AddCacheItemDependencies(String[])

使缓存项的有效性依赖于缓存中的另一项。

AddCacheItemDependency(String)

使缓存响应的有效性依赖于缓存中的其他项。

AddFileDependencies(ArrayList)

将一组文件名添加到文件名集合中,当前响应依赖于该集合。

AddFileDependencies(String[])

将文件名数组添加到当前响应依赖的文件名集合中。

AddFileDependency(String)

将单个文件名添加到文件名集合中,当前响应依赖于该集合。

AddHeader(String, String)

将 HTTP 头添加到输出流。 提供 AddHeader(String, String),以便兼容 ASP 的早期版本。

AddOnSendingHeaders(Action<HttpContext>)

注册一个回调,在即将为此请求发送响应标头之前,ASP.NET 运行时将会调用此回调。

AppendCookie(HttpCookie)

将一个 HTTP Cookie 添加到内部 Cookie 集合。

AppendHeader(String, String)

将 HTTP 头添加到输出流。

AppendToLog(String)

将自定义日志信息添加到 Internet Information Services (IIS)日志文件。

ApplyAppPathModifier(String)

如果会话使用 Cookieless 会话状态,则将该会话 ID 添加到虚拟路径中,并返回组合路径。 如果不使用 Cookieless 会话状态,则 ApplyAppPathModifier(String) 返回原始的虚拟路径。

BeginFlush(AsyncCallback, Object)

向客户端发送当前所有缓冲的响应。

BinaryWrite(Byte[])

将二进制字符串写入 HTTP 输出流。

Clear()

清除缓冲区流中的所有内容输出。

ClearContent()

清除缓冲区流中的所有内容输出。

ClearHeaders()

清除缓冲区流中的所有头。

Close()

关闭到客户端的套接字连接。

DisableKernelCache()

禁用当前响应的内核缓存。

DisableUserCache()

禁用 IIS 用户-方式来缓存反映。

End()

将当前所有缓冲的输出发送到客户端,停止该页的执行,并引发 EndRequest 事件。

EndFlush(IAsyncResult)

完成异步刷新操作。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
Flush()

向客户端发送当前所有缓冲的输出。

FlushAsync()

向客户端异步发送当前缓冲的所有输出。

GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
Pics(String)

将 HTTP PICS-Label 标头追加到输出流。

PushPromise(String)

支持将推送承诺发送到 HTTP 2.0 客户端的应用程序。 有关详细信息,请参阅 HTTP/2 规范 8.2 节:服务器推送

PushPromise(String, String, NameValueCollection)

支持将推送承诺发送到 HTTP 2.0 客户端的应用程序。 有关详细信息,请参阅 HTTP/2 规范 8.2 节:服务器推送

Redirect(String)

将请求重定向到新 URL 并指定该新 URL。

Redirect(String, Boolean)

将客户端重定向到新的 URL。 指定新的 URL 并指定当前页的执行是否应终止。

RedirectPermanent(String)

执行从所请求 URL 到所指定 URL 的永久重定向。

RedirectPermanent(String, Boolean)

执行从所请求 URL 到所指定 URL 的永久重定向,并提供用于完成响应的选项。

RedirectToRoute(Object)

使用路由参数值将请求重定向到新 URL。

RedirectToRoute(RouteValueDictionary)

使用路由参数值将请求重定向到新 URL。

RedirectToRoute(String)

使用路由名称将请求重定向到新 URL。

RedirectToRoute(String, Object)

使用路由参数值和路由名称将请求重定向到新 URL。

RedirectToRoute(String, RouteValueDictionary)

使用路由参数值和路由名称将请求重定向到新 URL。

RedirectToRoutePermanent(Object)

使用路由参数值执行从所请求 URL 到新 URL 的永久重定向。

RedirectToRoutePermanent(RouteValueDictionary)

使用路由参数值执行从所请求 URL 到新 URL 的永久重定向。

RedirectToRoutePermanent(String)

使用路由名称执行从所请求 URL 到新 URL 的永久重定向。

RedirectToRoutePermanent(String, Object)

使用路由参数值以及与新 URL 对应的路由的名称执行从所请求 URL 到新 URL 的永久重定向。

RedirectToRoutePermanent(String, RouteValueDictionary)

使用路由参数值和路由名称执行从所请求 URL 到新 URL 的永久重定向。

RemoveOutputCacheItem(String)

从缓存中移除与默认输出缓存提供程序关联的所有缓存项。 此方法是静态的。

RemoveOutputCacheItem(String, String)

使用指定的输出缓存提供程序删除所有与指定路径关联的输出缓存项。

SetCookie(HttpCookie)

因为 HttpResponse.SetCookie 方法仅供内部使用,所以不应在代码中调用该方法。 可以改为调用 HttpResponse.Cookies.Set 方法,如以下示例所示。
更新 cookie 集合中的现有 cookie。

ToString()

返回表示当前对象的字符串。

(继承自 Object)
TransmitFile(String)

将指定的文件直接写入 HTTP 响应输出流,而不在内存中缓冲该文件。

TransmitFile(String, Int64, Int64)

将文件的指定部分直接写入 HTTP 响应输出流,而不在内存中缓冲它。

Write(Char)

将一个字符写入 HTTP 响应输出流。

Write(Char[], Int32, Int32)

将字符数组写入 HTTP 响应输出流。

Write(Object)

Object 写入 HTTP 响应流。

Write(String)

将字符串写入 HTTP 响应输出流。

WriteFile(IntPtr, Int64, Int64)

将指定的文件直接写入 HTTP 响应输出流。

WriteFile(String)

将指定文件的内容作为文件块直接写入 HTTP 响应输出流。

WriteFile(String, Boolean)

将指定文件的内容作为内存块直接写入 HTTP 响应输出流。

WriteFile(String, Int64, Int64)

将指定的文件直接写入 HTTP 响应输出流。

WriteSubstitution(HttpResponseSubstitutionCallback)

允许将响应替换块插入响应,从而允许为缓存的输出响应动态生成指定的响应区域。

适用于