1. httphandler
Instance 1: generate a dynamic image to output the Client IP address, operating system type, and browser type.
Copy codeThe Code is 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 the canvas
Using (System. Drawing. Graphics g = System. Drawing. Graphics. FromImage (bitImage ))
{
// IP
G. drawString ("IP:" + context. request. userHostAddress, new System. drawing. font ("", 20), System. drawing. brushes. red, new System. drawing. pointF (0, 0 ));
// Operating System
G. drawString ("Operating System:" + context. request. browser. platform, new System. drawing. font ("", 20), System. drawing. brushes. red, new System. drawing. pointF (0, 50 ));
// Browser
G. drawString ("browser:" + context. request. browser. type, new System. drawing. font ("", 20), System. drawing. brushes. red, new System. drawing. pointF (0,100 ));
}
// Save it to the output stream
BitImage. Save (context. Response. OutputStream, System. Drawing. Imaging. ImageFormat. Jpeg );
}
}
Public bool IsReusable {
Get {
Return false;
}
}
}
Instance 2: Click Download to connect. The user attachment is saved.
Html code: <a href = "dowload. ashx"> download </a>
Copy codeThe Code is 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 ("Tragedy .jpg ");
Context. Response. ContentType = "image/JPEG ";
// Open the header of the message in the attachment dialog box and set its value
Context. Response. AddHeader ("Content-Disposition", "attachment: filename =" + fileName );
Context. Response. WriteFile ("Apple .jpg ");
}
Public bool IsReusable {
Get {
Return false;
}
}
}