Common ASP. NET objects: Request and Response
In the previous article, we gave an overview of common built-in objects in ASP. NET. Now we start to understand how to use, where to use, and when to use them. First, let's start with Request and Response.
During the development process, you will inevitably encounter such a problem: transferring values from the client to the server, or transferring values from the server to the client. Here we use Request and Response.
I. Request
1. Request object: The Request object mainly allows the server to obtain some data from the client browser, including parameters, cookies, and user authentication transmitted from the HTML form using the Post or GET method.
2. The Request encapsulates the client Request information. Common Request attributes are as follows:
Attribute name |
Value Type |
Description |
ApplicationPath |
String |
Obtain the root path of the requested resource on the website |
ContentEncoding |
Encoding |
Sets the encoding of the request object. |
Cookies |
HttpCookieCollection |
Cookie set sent from the client to the server |
QueryString |
NameValueCollection |
Query string set of the current request |
UrlReferrer |
Url |
Obtains the url from which the user jumps to the current page. |
There are few Request methods, only one BinaryRead ().
3. Because the Request object is a member of the Page object, it can be directly used without any declaration in the program;
(1). QueryString: used to obtain the information in the query string attached to the url address of the client (set the submission method to get ).
stra=Request.QueryString[strUserld]
(2). Form: used to obtain information entered by the client in FORM. (The method attribute value of the form must be POST. Compared with the Get method, the Post method can send a large amount of data to the server)
stra=Request.Form[strUserld]
(3). Cookies: used to obtain the Cookie information of the client.
stra=Request.Cookies[strUserld]
(4). ServerVariables: used to obtain the header information and server environment variable information in the HTTP request information sent by the client.
Stra = Request. ServerVariables [REMOTE_ADDR] // return the Client IP Address
(5). ClientCertificate: used to obtain the authentication information of the client.
Stra = Request. ClientCertificate [VALIDFORM] // for websites requiring security verification, the effective start date is returned.
4. Request. Form is used when the Form submission method is Post, while Request. QueryString is used when the Form submission method is Get. If an error is used, no data is obtained.
Solution: Use the Request ("element name") to simplify the operation.
Ii. Response
1. Response object: Used to dynamically respond to client requests, control information sent to users, and dynamically generate responses. Output data to the client, including outputting data to the browser, redirecting the browser to another URL, or outputting a Cookie file to the browser.
2. Common Response attributes are as follows:
Attribute name |
Value Type |
Description |
Charset |
String |
Indicates the character set used by the output stream. |
ContentEncoding |
Encoding |
Sets the encoding of the output stream. |
ContentLengt |
Int |
Size of the output stream in bytes |
ContentType |
String |
Http mime type of the output stream |
Output |
TextWriter |
Character output stream of the server response object |
Cookies |
HttpCookieCollection |
Cookie set sent from the server to the client |
RedirectLocation |
String |
Redirect current request |
Common Response methods
Method Name |
Return Value Type |
Description |
AppendCookie |
Void |
Add a Cookie to the Cookie set of the response object |
Clear |
Void |
Clear all content output in the buffer |
Close |
Void |
Close the connection from the current server to the client |
End |
Void |
Terminate the response and send the output in the buffer to the client. |
Redirect |
Void |
Redirect current request |
3. Response. Write variable data or string
Response. Write (variable data or string)
Response. write (<script language = javascript> alert ('Welcome to ASP.. NET ') </script>) Response. write (<script> window. open ('webform2. aspx ') </script>)
4. The Redirect method of the Response object redirects the client browser to another URL to jump to another webpage.
Response.Redirect(http://wangjinbo0823.blog.163.com/#m=0&t=0)
5. Response. End () Terminate the running of the current page
6. Response. WriteFile (FileName)
FileName indicates the name of the file to be output to the browser.
When the web server receives an HTTP request from the client, it creates a request object representing the request and a response object for each request.
1. To obtain the data submitted by the client, you only need to find the request object.
2. to output data to the client, you only need to find the response object.