HttpResponse类
关于此类的介绍:查看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();