關於此類的介紹:查看HttpResponse
定義:
封裝來自 ASP.NET 操作的 HTTP 響應信息。
屬性:
建議使用第二個。
下面的示例將響應的 ContentType 屬性設置為 image/jpeg,調用 Clear 方法以移除可能附加到響應的其他內容,然后將 BufferOutput 屬性設置為 true,這樣就可以在將任何內容發送到請求客戶端之前先處理整個頁。
HttpCachePolicy 類
HttpCacheability 枚舉
提供用於設置的枚舉的值 Cache-Control HTTP 標頭。
MIME類型介紹:點擊查看
MIME類型的機理是告訴客戶端傳輸的各種文件:文件名的擴展具有在網絡上沒有任何意義。因此,正確設置服務器是非常重要的,因此每個文檔都會傳送正確的MIME類型。瀏覽器通常使用MIME類型來確定在獲取資源時要執行的默認操作。
常用類型:
text/plain 文本文件
image/圖片類型 一般傳輸圖片
application/octet-stream 二進制數據 (常用來下載)
<div> <asp:TextBox ID="txtTest" runat="server" TextMode="MultiLine"></asp:TextBox> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /> </div>
Server.UrlEncode(txtTest.Text,Response.Output);
這個等下看一個列子
HttpCookie MyCookie = new HttpCookie("LastVisit"); MyCookie.Value = DateTime.Now.ToString(); Response.AppendCookie(MyCookie);
備注:如果您使用 AppendHeader 方法發送緩存特定的標頭並在同一時間使用的緩存對象模型 (Cache) 來設置與緩存相關的 HTTP 響應標頭的緩存策略 (Cache-Control, ,Expires, ,Last-Modified, ,Pragma, ,和 Vary) 時使用的緩存對象模型可能會刪除。 此行為使 ASP.NET 能夠維護的限制性最強的設置。 例如,考慮一頁包含用戶控件。 如果這些控件具有沖突的緩存策略,將使用限制性最強的緩存策略。 如果一個用戶控件設置的標頭"Cache-Control: Public"和另一個用戶控件設置的限制性更強的標頭"Cache-Control: Private"通過調用 SetCacheability, ,則"Cache-Control: Private"標頭將隨響應一起發送。
Response.AppendHeader("CustomAspNetHeader", "Value1");
例如:
// Response.AppendHeader("contentType", "text/html; charset=utf-8"); Response.ContentType = "text/html;charSet=utf-8";
這兩句代碼代表一個意思。
FileStream MyFileStream; long FileSize; MyFileStream = new FileStream(Server.MapPath("../待了解.txt"), FileMode.Open); FileSize = MyFileStream.Length; byte[] Buffer = new byte[(int)FileSize]; MyFileStream.Read(Buffer, 0, (int)FileSize); MyFileStream.Close(); Response.ContentType = "application/octet-stream"; //可以控制類型 Response.ClearContent(); Response.Write("<b>File Contents: </b>"); Response.BinaryWrite(Buffer);
一運行就會下載。
char[] charArray = {'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd'}; // Write a character array to the client. Response.Write(charArray, 0, charArray.Length); // Write a single characher. Response.Write(';'); // Write a sub-section of a character array to the client. Response.Write(charArray, 0, 5); // Write an object to the client. object obj = (object)13; Response.Write(obj);
protected void Page_Load(object sender, EventArgs e) { String FileName; FileInfo MyFileInfo; long StartPos = 0, FileSize; FileName = Server.MapPath("../待了解.txt"); MyFileInfo = new FileInfo(FileName); FileSize = MyFileInfo.Length; Response.Write("Please Login: <br>"); Response.WriteFile(FileName, StartPos, FileSize); }
這個跟上面一樣的。
用這個類很常見就是生成驗證碼,和下載文件。
驗證碼:
//創建畫布 int codeW = 80; int codeH = 22; int fontSize = 16; string chkCode = string.Empty; //顏色列表,用於驗證碼、噪線、噪點 Color[] color = { Color.Black, Color.Red, Color.Blue, Color.Green, Color.Orange, Color.Brown, Color.Brown, Color.DarkBlue }; //字體列表,用於驗證碼 string[] font = { "Times New Roman", "Verdana", "Arial", "Gungsuh", "Impact" }; //驗證碼的字符集,去掉了一些容易混淆的字符 char[] character = { '2', '3', '4', '5', '6', '8', '9', 'a', 'b', 'd', 'e', 'f', 'h', 'k', 'm', 'n', 'r', 'x', 'y', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'W', 'X', 'Y' }; Random rnd = new Random(); //生成驗證碼字符串 for (int i = 0; i < 4; i++) { chkCode += character[rnd.Next(character.Length)]; } //寫入Session //創建畫布 Bitmap bmp = new Bitmap(codeW, codeH); Graphics g = Graphics.FromImage(bmp); g.Clear(Color.White); //畫噪線 for (int i = 0; i < 1; i++) { int x1 = rnd.Next(codeW); int y1 = rnd.Next(codeH); int x2 = rnd.Next(codeW); int y2 = rnd.Next(codeH); Color clr = color[rnd.Next(color.Length)]; g.DrawLine(new Pen(clr), x1, y1, x2, y2); } //畫驗證碼字符串 for (int i = 0; i < chkCode.Length; i++) { string fnt = font[rnd.Next(font.Length)]; Font ft = new Font(fnt, fontSize); Color clr = color[rnd.Next(color.Length)]; g.DrawString(chkCode[i].ToString(), ft, new SolidBrush(clr), (float)i * 18 + 2, (float)0); } //畫噪點 for (int i = 0; i < 100; i++) { int x = rnd.Next(bmp.Width); int y = rnd.Next(bmp.Height); Color clr = color[rnd.Next(color.Length)]; bmp.SetPixel(x, y, clr); } //清除緩存,設置改頁面無緩存 Response.BufferOutput = true; Response.ExpiresAbsolute = System.DateTime.Now.AddMilliseconds(0); Response.Expires = 0; Response.CacheControl = "no-cache"; Response.AppendHeader("Pragma", "No-Cache"); //第一種 Response.ContentType = "image/png"; Response.ClearContent(); bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png); bmp.Dispose(); g.Dispose(); //第二種 //MemoryStream ms = new MemoryStream(); //try //{ // bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);//把畫布以什么格式保存流 // //你要告訴客戶端是什么類型 // Response.ContentType = "image/png"; // Response.ClearContent(); // Response.BinaryWrite(ms.ToArray()); //} //finally //{ // //釋放資源 // bmp.Dispose(); // g.Dispose(); //}
下載案例:
FileStream MyFileStream; long FileSize; MyFileStream = new FileStream(Server.MapPath("../待了解.txt"), FileMode.Open); FileSize = MyFileStream.Length; byte[] Buffer = new byte[(int)FileSize]; MyFileStream.Read(Buffer, 0, (int)FileSize); MyFileStream.Close(); //一定需要設置的,附件下載的標頭 Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("測試.txt")); Response.ContentType = "application/octet-stream"; //設置了標頭這個可以不用設置 Response.ClearContent(); Response.BinaryWrite(Buffer); Response.Flush(); Response.End();