Use of Request, Response, and Server in ASP. NET notes

Source: Internet
Author: User

1. Request

The following is an example. You can use the Request method to determine whether the image is browsed internally, or whether the image is directly browsed by the website or used externally.

Copy codeThe Code is as follows: <% @ WebHandler Language = "C #" Class = "image_Test" %>

Using System;
Using System. Web;

Public class image_Test: IHttpHandler {

Public void ProcessRequest (HttpContext context)
{
Context. Response. ContentType = "image/JPEG ";
// If URLreferrer is directly accessed, It is null. If the request is embedded in the page
// URLreferrer is the address of the page
String picPath = HttpContext. Current. Server. MapPath ("DSCF0738.JPG ");
Using (System. Drawing. Bitmap bitmap = new System. Drawing. Bitmap (picPath )){
Using (System. Drawing. Graphics graphic = System. Drawing. Graphics. FromImage (bitmap ))
{
// However, it is still too fragile because UrlReferrer is submitted by the client
// Thunder cannot crack Pear
If (context. Request. UrlReferrer = null)
{
Graphic. Clear (System. Drawing. Color. White );
Graphic. DrawString ("prohibit direct Image Browsing", new System. Drawing. Font ("", 15 ),
System. Drawing. Brushes. Red, 0, 0 );

}
// Http: // 127.0.0.1: 32581/WebSite_zzl01/vivideo_test/request/Request. aspx

Else if (context. Request. UrlReferrer. Host! = "Localhost ")
{
Graphic. Clear (System. Drawing. Color. White );
Graphic. DrawString ("images can be viewed only within the blog Garden", new System. Drawing. Font ("", 15 ),
System. Drawing. Brushes. Red, 0, 0 );
}
// Convert to stream format output
Bitmap. Save (context. Response. OutputStream, System. Drawing. Imaging. ImageFormat. Jpeg );

}
}
}

Public bool IsReusable {
Get {
Return false;
}
}

}

Html

Lala

2. Response

(1) The returned stream is returned to the client as a stream.
* Each write operation is stored in the cache. It is not directly sent to the browser until it is full or processed.
* Flush method, which is immediately sent to the browser

(2) Anti-leeching and so on: it is better to write data in aspx without further execution.
Context. Response. End ();

(3) aspx and ashx

Like the output text, image, and finally written in ashx, while the html content is written in aspx

(4) Redirect

Flush instance:

Copy codeThe Code is as follows: <% @ WebHandler Language = "C #" Class = "response" %>

Using System;
Using System. Web;

Public class response: IHttpHandler {

Public void ProcessRequest (HttpContext context ){

// If it is plain, <br/> no effect
Context. Response. ContentType = "text/html ";
// Time-consuming operation
For (int I = 0; I <20; I ++)
{
System. Threading. Thread. Sleep (500 );
Context. Response. Write ("Step" + I + "start execution <br/> ");
// Use Flush and send it to the client immediately. The effect is obvious!
Context. Response. Flush ();
}
}

Public bool IsReusable {
Get {
Return false;
}
}

}

3. server

(1) differences between Server. Transfer and Response. Redirect

* Transfer can only access internal websites, not external websites, and redirect can

*Transfer is taken over by the website. It only executes an http request once, while Redirect executes the http request several times in the address bar.

* Transfer directly transmits various information, but Redirect does not
* Cannot be redirected to ashx transfo again.

Instance:

Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;

Public partial class vivideo_test_server_server: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
String text = Request ["id"];
If (text = "1 "){
Response. Write ("1 ");
}
Else if (text = "2 "){
// Request information is transmitted to the server
Server. Transfer ("DSCF0738.JPG ");
}
Else if (string. IsNullOrEmpty (text ))
{
Response. Write ("null ");
}
Else {
// The passed parameters are not obtained.
Response. Redirect ("Apple .jpg ");
// Unless you wear it yourself
// Response. Redirect ("aaa. aspx? Id = 1 ");

}
}
}

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.