HttpResponse 类
而封閉HTTP輸出信息的類型就是HttpResponse類,使用HttpResponse類可以實現三種類型的輸出,即文本,URL,二進制流.
? 實現這三類的屬性和方法分別介紹如下:
1.文本的輸出,在日常開發中,后臺中的文本可能需要輸出到瀏覽器中,讓用戶瀏覽,這就需要實現動態HTML的輸出,使用HttpResponse類的Write靜態方法可以實現,例如希望在瀏覽器上顯示一個"hello world!"的字樣時,可以在Page_load方法中增加如下代碼,就可以實現:
?
?
2.URL的輸出,程序開發經常需要根據情況將用戶瀏覽的界面重定向到其他頁面,例如,用戶在沒有登錄的狀態下查看自己的信息,系統需要首先將其轉向到登錄頁,登錄后再轉回信息瀏覽頁,實現URL的輸出可以使用HttpResponse類的redirect方法實現,代碼如下:
??
?
?
3.二進制流,有時需要將服務器上的文件提供給用戶下載,或者在瀏覽器端動態生成一幅圖片,例如,驗證的初一二進制流輸出到用戶瀏覽器中.
?
?
?
https://msdn.microsoft.com/zh-cn/library/system.web.httpresponse(v=vs.110).aspx
封裝來自 ASP.NET 操作的 HTTP 響應信息
已用到的方法:
?
?
| 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 的永久重定向。 | |
| 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) | 允許將響應替換塊插入響應,從而允許為緩存的輸出響應動態生成指定的響應區域。 |
?
代碼示例:
?
<%@ 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>?
轉載于:https://www.cnblogs.com/CandiceW/p/4936963.html
總結
以上是生活随笔為你收集整理的HttpResponse 类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: notice
- 下一篇: Struts2使用OGNL遍历各种map