A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© iamheima 中级黑马   /  2012-9-16 13:57  /  1365 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 iamheima 于 2012-9-16 13:59 编辑

HttpResponse中的End()方法和Flush()的区别?
我的理解是:End()方法不但将输出缓存中的数据输出到浏览器中了,还停止了服务器端处理程序的执行,而Flush()则只是输出缓冲中的数据,并没有停止程序的执行。。对吗?

1 个回复

倒序浏览
给你我的理解吧:
HttpResponse.Flush()方法
将当前缓冲的所有输出强制发送到客户端。 在请求处理期间,可以多次调用 Flush 方法。实例:
下面的示例调用 Save 方法将一个 Bitmap 对象保存到 OutputStream 属性中,并将图像转换为 JPEG 格式。然后,代码调用 Bitmap 对象和 Graphics 对象的 Dispose 方法,释放它们正在使用的资源。最后,该代码调用 Flush 方法将响应的内容发送到请求客户端。
  1. <%@ Page Language="C#" %>
  2. <%@ import Namespace="System.Drawing" %>
  3. <%@ import Namespace="System.Drawing.Imaging" %>
  4. <%@ import Namespace="System.Drawing.Drawing2D" %>

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

  8.     private void Page_Load(object sender, EventArgs e)
  9.     {
  10.         // Set the page's content type to JPEG files
  11.         // and clear all response headers.
  12.         Response.ContentType = "image/jpeg";
  13.         Response.Clear();

  14.         // Buffer response so that page is sent
  15.         // after processing is complete.
  16.         Response.BufferOutput = true;

  17.         // Create a font style.
  18.         Font rectangleFont = new Font(
  19.             "Arial", 10, FontStyle.Bold);

  20.         // Create integer variables.
  21.         int height = 100;
  22.         int width = 200;

  23.         // Create a random number generator and create
  24.         // variable values based on it.
  25.         Random r = new Random();
  26.         int x = r.Next(75);
  27.         int a = r.Next(155);
  28.         int x1 = r.Next(100);

  29.         // Create a bitmap and use it to create a
  30.         // Graphics object.
  31.         Bitmap bmp = new Bitmap(
  32.             width, height, PixelFormat.Format24bppRgb);
  33.         Graphics g = Graphics.FromImage(bmp);

  34.         g.SmoothingMode = SmoothingMode.AntiAlias;
  35.         g.Clear(Color.LightGray);

  36.         // Use the Graphics object to draw three rectangles.
  37.         g.DrawRectangle(Pens.White, 1, 1, width-3, height-3);
  38.         g.DrawRectangle(Pens.Aquamarine, 2, 2, width-3, height-3);
  39.         g.DrawRectangle(Pens.Black, 0, 0, width, height);

  40.         // Use the Graphics object to write a string
  41.         // on the rectangles.
  42.         g.DrawString(
  43.             "ASP.NET Samples", rectangleFont,
  44.             SystemBrushes.WindowText, new PointF(10, 40));

  45.         // Apply color to two of the rectangles.
  46.         g.FillRectangle(
  47.             new SolidBrush(
  48.                 Color.FromArgb(a, 255, 128, 255)),
  49.             x, 20, 100, 50);

  50.         g.FillRectangle(
  51.             new LinearGradientBrush(
  52.                 new Point(x, 10),
  53.                 new Point(x1 + 75, 50 + 30),
  54.                 Color.FromArgb(128, 0, 0, 128),
  55.                 Color.FromArgb(255, 255, 255, 240)),
  56.             x1, 50, 75, 30);

  57.         // Save the bitmap to the response stream and
  58.         // convert it to JPEG format.
  59.         bmp.Save(Response.OutputStream, ImageFormat.Jpeg);

  60.         // Release memory used by the Graphics object
  61.         // and the bitmap.
  62.         g.Dispose();
  63.         bmp.Dispose();

  64.         // Send the output to the client.
  65.         Response.Flush();
  66.     }

  67. </script>
  68. <html xmlns="http://www.w3.org/1999/xhtml" >
  69. <head>
  70.     <title>ASP.NET Example</title>
  71. </head>
  72. <body>
  73.     <form id="form1" runat="server">
  74.     </form>
  75. </body>
  76. </html>
复制代码
HttpResponse中的End()方法
將目前所有受緩衝的輸出傳送到用戶端、停止網頁的執行,並引發 EndRequest 事件。下面的示例使用 IsClientConnected 属性来检查请求页的客户端是否仍与服务器连接。如果 IsClientConnected 为 true,代码将调用 Redirect 方法,因此客户端将可以查看另一页。如果 IsClientConnected 为 false,代码将调用 End 方法,并且所有页处理都将终止。
  1. [code]<%@ Page Language="C#" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <script runat="server">

  5. private void Page_Load(object sender, EventArgs e)
  6. {
  7. // Check whether the browser remains
  8. // connected to the server.
  9. if (Response.IsClientConnected)
  10. {
  11. // If still connected, redirect
  12. // to another page.
  13. Response.Redirect("Page2CS.aspx", false);
  14. }
  15. else
  16. {
  17. // If the browser is not connected
  18. // stop all response processing.
  19. Response.End();
  20. }
  21. }

  22. </script>
  23. <html xmlns="http://www.w3.org/1999/xhtml" >
  24. <head>
  25. <title>ASP.NET Example</title>
  26. </head>
  27. <body>
  28. <form id="form1" runat="server">
  29. </form>
  30. </body>
  31. </html>
复制代码
[/code]




回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马