ASP. NET study note _ 06 httphandler

Source: Internet
Author: User

1. httphandler

Instance 1: generate a dynamic image to output the Client IP address, operating system type, and browser type.

<% @ 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 ( "    " , 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 to 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.

HtmlCode: <A href = "dowload. ashx"> download </a>

<% @ 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 and set its value in the attachment dialog box. Context. response. addheader ( "  Content-Disposition  " , "  Attachment: filename =  " + Filename); context. response. writefile (  "  Apple .jpg  "  );}  Public   Bool Isreusable {  Get  {  Return   False  ;}}} 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.