1, HttpHandler
Example 1: By generating a dynamic picture output the client's IP address, operating system type, browser type
Copy Code code as follows:
<%@ WebHandler language= "C #" class= "Visitor"%>
Using System;
Using System.Web;
public class Visitor:ihttphandler {
public void ProcessRequest (HttpContext context)
{
Context. Response.ContentType = "Image/jpeg";
using (System.Drawing.Bitmap bitimage = new System.Drawing.Bitmap (330, 300))
{
Set Canvas
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage (Bitimage))
{
Ip
g.DrawString ("IP:" + context.) Request.userhostaddress, New System.Drawing.Font ("XXFarEastFont-Arial"), System.Drawing.Brushes.Red, new System.Drawing.PointF (0, 0));
Operating system
g.DrawString ("Operating system:" + context.) Request.Browser.Platform, New System.Drawing.Font ("XXFarEastFont-Arial"), System.Drawing.Brushes.Red, new System.Drawing.PointF (0 , 50));
Browser
g.DrawString ("Browser:" + context.) Request.Browser.Type, New System.Drawing.Font ("XXFarEastFont-Arial"), System.Drawing.Brushes.Red, new System.Drawing.PointF (0, 100 ));
}
Save to the output stream
Bitimage.save (context. Response.outputstream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
public bool IsReusable {
get {
return false;
}
}
}
Example 2: Through the "Download" Connection, pop-up user attachment save
HTML code: <a href= "dowload.ashx" > Download </a>
Copy Code code as follows:
<%@ WebHandler language= "C #" class= "Dowload"%>
Using System;
Using System.Web;
public class Dowload:ihttphandler {
public void ProcessRequest (HttpContext context)
{
String fileName = Httputility.urlencode ("tragic. jpg");
Context. Response.ContentType = "Image/jpeg";
Open the Attachment dialog box header header and set its value
Context. Response.AddHeader ("Content-disposition", "attachment:filename=" + filename);
Context. Response.WriteFile ("apple. jpg");
}
public bool IsReusable {
get {
return false;
}
}
}