HttpResponse 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
封装来自 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,以便页面在发送到请求客户端之前完全进行处理。 然后创建用于绘制矩形的两个对象:a Bitmap 和一个 Graphics 对象。 在页面中创建的变量用作坐标来绘制矩形和显示在最大矩形内的字符串。
当绘制三个矩形及其内部显示的字符串时,该 Bitmap 矩形将保存到 Stream 与 OutputStream 属性关联的对象,其格式设置为 JPEG。 代码调用 Dispose 和 Dispose 方法释放两个绘图对象使用的资源。 最后,代码调用 Flush 该方法以将缓冲响应发送到请求客户端。
注释
在代码中,该 HttpResponse 对象由关键字 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通过ResponseHttpApplication类HttpContextPageUserControl的属性公开。
类的 HttpResponse 以下方法仅在回发方案中受支持,而不支持在异步回发方案中:
- BinaryWrite
- Clear
- ClearContent
- ClearHeaders
- Close
- End
- Flush
- TransmitFile
- Write
- WriteFile
- WriteSubstitution
当使用 UpdatePanel 控件更新页面的选定区域,而不是使用回发更新整个页面时,将启用部分页面更新。 有关详细信息,请参阅 UpdatePanel 控件概述 和 Partial-Page 呈现概述。
构造函数
| 名称 | 说明 |
|---|---|
| HttpResponse(TextWriter) |
初始化 HttpResponse 类的新实例。 |
属性
| 名称 | 说明 |
|---|---|
| Buffer |
获取或设置一个值,该值指示是否在完成响应处理后缓冲输出并发送输出。 |
| BufferOutput |
获取或设置一个值,该值指示是否在完成页面处理后缓冲输出并发送输出。 |
| Cache |
获取网页的缓存策略(例如过期时间、隐私设置和不同子句)。 |
| CacheControl |
获取或设置 |
| 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 |
| Status |
|
| StatusCode |
获取或设置返回到客户端的输出的 HTTP 状态代码。 |
| StatusDescription |
获取或设置返回到客户端的输出的 HTTP 状态字符串。 |
| SubStatusCode |
获取或设置一个值,该值限定响应的状态代码。 |
| SupportsAsyncFlush |
获取一个值,该值指示连接是否支持异步刷新操作。 |
| SuppressContent |
获取或设置一个值,该值指示是否将 HTTP 内容发送到客户端。 |
| SuppressDefaultCacheControlHeader |
获取或设置一个值,该值指示是否禁止显示当前 HTTP 响应的默认 |
| SuppressFormsAuthenticationRedirect |
获取或设置一个值,该值指定是否应禁止表单身份验证重定向到登录页。 |
| 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 |
| PushPromise(String, String, NameValueCollection) |
支持向 HTTP 2.0 客户端发送推送承诺的应用程序。 有关详细信息,请参阅 HTTP/2 规范第 8.2 节:服务器推送。 |
| PushPromise(String) |
支持向 HTTP 2.0 客户端发送推送承诺的应用程序。 有关详细信息,请参阅 HTTP/2 规范第 8.2 节:服务器推送。 |
| Redirect(String, Boolean) |
将客户端重定向到新的 URL。 指定新 URL 以及当前页的执行是否应终止。 |
| Redirect(String) |
将请求重定向到新的 URL 并指定新 URL。 |
| RedirectPermanent(String, Boolean) |
执行从请求的 URL 到指定 URL 的永久重定向,并提供完成响应的选项。 |
| RedirectPermanent(String) |
执行从请求的 URL 到指定 URL 的永久重定向。 |
| RedirectToRoute(Object) |
使用路由参数值将请求重定向到新 URL。 |
| RedirectToRoute(RouteValueDictionary) |
使用路由参数值将请求重定向到新 URL。 |
| RedirectToRoute(String, Object) |
使用路由参数值和路由名称将请求重定向到新的 URL。 |
| RedirectToRoute(String, RouteValueDictionary) |
使用路由参数值和路由名称将请求重定向到新的 URL。 |
| RedirectToRoute(String) |
使用路由名称将请求重定向到新的 URL。 |
| RedirectToRoutePermanent(Object) |
使用路由参数值执行从请求的 URL 到新 URL 的永久重定向。 |
| RedirectToRoutePermanent(RouteValueDictionary) |
使用路由参数值执行从请求的 URL 到新 URL 的永久重定向。 |
| RedirectToRoutePermanent(String, Object) |
使用路由参数值和与新 URL 对应的路由名称,执行从请求的 URL 到新 URL 的永久重定向。 |
| RedirectToRoutePermanent(String, RouteValueDictionary) |
使用路由参数值和路由名称,执行从请求的 URL 到新 URL 的永久重定向。 |
| RedirectToRoutePermanent(String) |
使用路由名称执行从请求的 URL 到新 URL 的永久重定向。 |
| RemoveOutputCacheItem(String, String) |
使用指定的输出缓存提供程序删除与指定路径关联的所有输出缓存项。 |
| RemoveOutputCacheItem(String) |
从缓存中删除与默认输出缓存提供程序关联的所有缓存项。 此方法是静态的。 |
| SetCookie(HttpCookie) |
由于 HttpResponse.SetCookie 方法仅供内部使用,因此不应在代码中调用它。 相反,可以调用 HttpResponse.Cookies.Set 方法,如以下示例所示。 |
| ToString() |
返回一个表示当前对象的字符串。 (继承自 Object) |
| TransmitFile(String, Int64, Int64) |
将文件的指定部分直接写入 HTTP 响应输出流,而无需将其缓冲到内存中。 |
| TransmitFile(String) |
将指定的文件直接写入 HTTP 响应输出流,而无需将其缓冲到内存中。 |
| Write(Char) |
将字符写入 HTTP 响应输出流。 |
| Write(Char[], Int32, Int32) |
将字符数组写入 HTTP 响应输出流。 |
| Write(Object) |
将一个 Object 写入 HTTP 响应流。 |
| Write(String) |
将字符串写入 HTTP 响应输出流。 |
| WriteFile(IntPtr, Int64, Int64) |
将指定的文件直接写入 HTTP 响应输出流。 |
| WriteFile(String, Boolean) |
将指定文件的内容作为内存块直接写入 HTTP 响应输出流。 |
| WriteFile(String, Int64, Int64) |
将指定的文件直接写入 HTTP 响应输出流。 |
| WriteFile(String) |
将指定文件的内容作为文件块直接写入 HTTP 响应输出流。 |
| WriteSubstitution(HttpResponseSubstitutionCallback) |
允许将响应替换块插入到响应中,这允许动态生成输出缓存响应的指定响应区域。 |