. Net HttpResponse object and HttpRequest object usage

Source: Internet
Author: User
Tags current time http request httpcontext lowercase servervariables string back

In asp tutorial. net, http response information is encapsulated by the httpresponse class. This type of instance is created when the http pipeline that processes the request is created, and then the instance is linked to the httpcontext object associated with the request and exposed through its response attribute. Although the user-defined asp.net tutorial code does not need to use the httpresponse constructor, this function can help us understand the nature of this class:

Public httpresponse (textwriter writer );
It can be seen that the constructor accepts a textwriter object, which is used to store the response text. All calls to response. write are converted to calls to textwriter objects.
Httpresponse class attributes
The following table lists the attributes of the httpresponse class:
Response cache policy settings
For pages sent to the browser and cached, the response object has three control attributes. The expries and expriesabsolute attributes are used to define the relative time and absolute time respectively. At that time, the page cached on the client will expire and the browser will no longer use it to serve the customer's request. In fact, if you navigate to the current cached page, the cached version will be displayed without interacting with the server. The third attribute related to cache is cachecontrol. This attribute is used to set a special http header, namely cache-control. This header is used to control the way documents are cached across networks. These property bets are legacy asp Programming styles.
In asp.net, all cache functions are set by the httpcachepolicy class. This class plays a dual role in page caching. Some of the methods provided by this class are used to set cache-specific http headers, and some are used to control the asp.net page output cache.
To set page visibility in the client cache, you can use the setcacheability method of the httpcachepolicy class. The setexpires method is used to set the Expiration Time. This method accepts an absolute datetime object. To set the lifetime of the cache page, add the expected interval to the current time and input setexpires.
If a cache policy conflict occurs, asp.net uses the most restrictive setting. For example, if the page contains two controls and set the cache-control header to public and private, the most restrictive policy will be applied. In this example, cache-control: private is sent to the client.
Output filter settings
A response filter is an object derived from stream and associated with an httpresponse object. This object can monitor and filter page output. If you set the filter attribute to an instance derived from the stream class, all outputs to be written to the underlying http writer will pass the output filter in advance.
If a custom filter is set, it is called before the httpresponse flush method is executed and the actual text is sent to the client. The output filter is suitable for the final processing of tags and is often used to compress or correct the tags generated by the control.
Building a response filter is to create a new stream derived class and override some of the methods. This class should have a constructor that accepts stream objects. Therefore, the response filter class is not only a class inherited from the stream, but also a package class. If a class instance such as memorystream or filestream is directly assigned to response. filter, an exception occurs.
The following code demonstrates how to create a stream class that acts as a response filter. For simplicity, this class inherits from memorystream. This type can convert lowercase letters to uppercase letters.
Public class myfilterstream: memorystream
Private stream m_stream;

Public myfilterstream (stream filterstream)
M_stream = filterstream;

// The write method actually does the filtering
Public override void write (byte [] buffer, int offset, int count)
// Grab the output as a string
String buf = utf8encoding. utf8.getstring (buffer, offset, count );

// Change lowercase chars to uppercase
Buf = buf. toupper ();

// Write the resulting string back the response stream
Byte [] data = utf8encoding. utf8.getbytes (buf. tostring ());
M_stream.write (data, 0, data. length );
The following code associates the output filter with the response. filter attribute:
Void page_load (object sender, eventargs e)
Response. filter = new myfilterstream (response. filter );
Response filters help developers build powerful applications, but you must be cautious when using them. Careless use may affect the view status and internal script code (both may contain case-sensitive text), seriously endangering the page output effect.
You must also enable the filter for a single page. To filter all pages on a website, you are advised to write an http module.
Httpresponse class method
The following table lists the methods of all httpresponse classes:
Transmission of large files
There are three methods in the httpresponse class that can set large files into the output stream: binarywrite, writefile, and transmitfile. The transmitfile method is the most stable and reliable among the three methods.

When transferring large files, the writefile and binarywrite methods will put pressure on the memory of the web server. Why? Because these two methods need to load the entire data block (file or byte array content) into the memory of the web server. For large files, this causes serious memory problems and forces asp.net to recycle itself. The transmitfile method is designed to solve this problem elegantly. This method can directly send the output from the file to the asp.net isapi extension, and then download the output to the client without sending a large number of strings to the isapi extension.


The httprequest object contains all the information contained in the http data package that passes in the web request. The content of the http header, query string, input field of the form, path, and url information is organized in different sets and other special objects, which can be easily accessed by programming. When asp.net starts to process web requests, the httprequest object will be filled and exposed through the request attribute of httpcontext.

Httprequest class attributes

Httprequest attributes can be divided into three types: request type, client data, and connection.

Request information

The following table lists the attributes of the request information:

The httpbrowsercapabilities object contains a large amount of information about browser functions, including whether activex controls, scripting languages, frameworks, and cookies are supported. When the request arrives, an httpbrowsercapabilities class instance is created using the user agent information to determine the browser that sends the request, and the browser-specific information is filled in. This information is not dynamically set by the browser, but obtained offline from the server repository.

Information from the client

The following table lists the properties of the httprequest client data:

The params set combines four different but similar forms: querystring, form, servervariables, and cookies, which are consistent with the data contained in the independent set. This set is filled in the following order: querystring, form, cookies, and servervariables.

Connection information

The following table lists the attributes related to opening a connection:

The uri class provides an object representation of a uniform resource identifier.

Httprequest class method

The following table lists the main methods of the httprequest class:

Save the request to disk

The saveas method allows us to create a file that stores all the content of an http request. Note: the storage media can only be disk files, but cannot use streams and writers. Asp.net is not granted the write permission to the disk by default. Therefore, granting full access to new files (or folders) to the asp.net account is a condition for saveas to run successfully.

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.