Preface:
The Request object is mainly used to obtain data from the client, such as the data filled in by the user and the Cookie stored on the client.
1. Request object Overview
1. Main attributes
ApplicationPath |
Obtain the root path of the virtual application of the asp.net application on the server. |
Browser |
Obtain information about the browser function of the Client Requesting. The property value is: HttpBrowserCapabilities |
ContentEncoding |
Obtains or sets the character set of the entity. This property value indicates the character set Encoding object of the client. |
ContentLength |
Length of the content sent by the client, in bytes |
ContentType |
Gets or sets the MIME content type of the incoming request. |
Cookies |
Obtains the Cookie set sent by the client. The property value is the HttpCookieCollection object that represents the Cookie variable of the client. |
CurrentExecutionFilePath |
Obtain the virtual path of the current request |
FilePath |
Obtain the virtual path of the current request |
Files |
Obtain the file set uploaded by the client. The property value is an HttpFileCollection object, indicating the file set uploaded by the client. |
Form |
Get the form variable set |
HttpMethod |
Obtain the HTTP data transmission method (such as get, post, or head) used by the client) |
Item |
Obtains the specified objects in the Cookies, forms, QueryString, or ServerVariables set. |
Params |
Obtains a combination of Cookies, forms, QueryString, and ServerVariables. |
Path |
Obtain the virtual path of the current request |
PathInfo |
Obtain the additional path information of a resource with a URL extension. |
PhysicalApplicationPath |
Obtain the physical file system path of the root directory of the currently running server application |
PhysicalPath |
Obtain the physical file path corresponding to the requested URL |
QueryString |
Obtains a set of HTTP query string variables. The property value is a NameValueCollection object that contains a collection of query string variables sent by the client. |
RequestType |
Obtain or set the HTTP data transmission method (get or post) for the client) |
ServerVariables |
Get a set of Web server Variables |
TotalBytes |
Obtains the number of bytes of the current input stream. |
Url |
Obtain information about the current request URL |
UserHostAddress |
Obtain the IP address of the remote client host. |
|
|
2. Main Methods
(1) MapPath (VirtualPath): maps the virtual path virtualPath in the current request URL to the physical path on the server. The virtualPath parameter specifies the virtual path of the current request, which can be an absolute or relative path. The return value of this method is the physical path of the server specified by virtualPath.
(2) SaveAs (Filename, includeHeaders): saves the http request to the disk. The filename parameter specifies the physical drive path. includeHeaders is a Boolean value that specifies whether the HTTP header should be saved to the disk.
II. Application of the Request object
1. Read form variables in four ways:
(1). Use the Request. Form attribute to read Form variables
(2). Use the Request. QueryString attribute to read form variables
(3). Use the Request. Params attribute to read form variables
(4). Directly read form variables through the properties of the server control
(1). Use the Request. Form attribute to read Form variables
The default value of the Method attribute of the HtmlForm control is post. In this case, when a user submits a webpage, the form data is sent to the server in the form of an HTTP header. In this case, you can use the Form attribute of the Request object to read Form variables. For example, the text box controls of txtUserName and txtPassword can read their values in the following Form: Request. Form ["txtUserName"]; Request. Form ["txtPassword"]
(2) read form variables using the Request. QueryString attribute
If you set the Method attribute of the HtmlForm control to get, when you submit a webpage, the form data will be appended to the URL and sent to the server. In this case, you can use the QueryString attribute of the Request object to read the form variable. Request. QueryString ["txtUserName"]; Request. QueryString ["txtPassword"]
(3) read form variables using the Request. Params attribute
Regardless of the value of the Method property of the HtmlForm control, you can use the Params attribute of the Request object to read the content of the form variable, such as Request. params ["txtPassword"] or Request. ["txtPassword"],GET the data submitted in GET mode first,It searches in the order of QueryString, Form, and ServerVariable.
Request: contains the preceding two methods (obtain the data submitted by the GET method first), which are searched in order in QueryString, Form, and ServerVariable. Request. params is a set of all post and get values. params is actually a set, which in turn includes request. queryString, request. form, request. cookies and requests. serverVariable.
Note: When Request. Params is used, it is best not to have the same name among these set items. If you only need a piece of data in Form, but use Request instead of Request. Form, the program will also search for it in QueryString and ServerVariable. If exactly QueryString or ServerVariable has an item of the same name, it is not the expected value.
(4) directly read form variables through the properties of the server control
In addition to the preceding three methods, you can also directly read form variables by using the properties of the server control. This is the most common and simple way to obtain form data. Example: txtUserName. Text
2. Read the query string variable
When browsing the Web page, I often see "xxx. aspx?" displayed in the address bar of the browser? Id = 8018 "and other URLs, where xxx. aspx indicates the. aspx webpage to be accessed, question mark (?) The following content is the query string, which is used to transfer the variable name and value to the ASP. NET file for processing. Query string variables can be generated in the following ways.
(1) If you set the Method attribute of the HtmlForm control to get, when you submit a webpage, the form data is attached to the URL as a query string variable and sent to the server.
(2). Use <a>... </A> when creating a hypertext link with a tag or HyperLink control, you can place the query string behind the target URL and use the question mark "?" To separate URLs from query strings.
(3) When the Response. Redirect method is called, if a variable name/value pair is attached to the URL parameter, the variable values are attached to the URL and sent to the server when the target webpage is opened.
(4) when entering the request URL in the browser address bar, enter the question mark "?" After the URL And query string. For example: http ://... /T. aspx? Id = 8018
In the preceding cases, you can use the Request. QueryString attribute to retrieve and query string variables.
The following code:
Copy codeThe Code is as follows:
// On the login page
Protected void button#click (object sender, EventArgs e)
{
// Login
// If (txtUserName. Text = "admin" & txtPwd. Text = "123 ")
//{
// Session ["Info"] = "any value ";
// Response. Redirect ("Request2_test.aspx? Info = "+ txtUserName. Text );
//}
// Else
//{
// Response. Redirect ("Request2_test.aspx? Error = login failed! ");
//}
/*********************************** Method 2 ** **************************************/
// Or
If (txtUserName. Text = "admin" & txtPwd. Text = "123 ")
{
Response. Redirect ("Request2_test.aspx? Info = "+ txtUserName. Text +" & check = 1 ");
}
Else
{
Response. Redirect ("Request2_test.aspx? Error = login failed! ");
}
}
On the verification page
Copy codeThe Code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
// Verification page
// If (Session ["Info"]! = Null & Session ["Info"]. ToString () = "any value ")
//{
// Response. Write ("Login successful! <Br> "+ Request. QueryString [" Info "] +", welcome to this site ");
//// Response. Write ("Login successful! <Br> "+ Request [" Info "] +", welcome to this site ");
//// Response. Write ("Logon successful! <Br> "+ Request. Form [" username "] +", welcome to this site ");
//}
// Else
//{
// Response. Write ("Logon Failed ");
//}
/*************************************** Method 2 **********************************/
If (Convert. ToInt32 (Request ["check"]) = 1)
{
Response. Write ("Login successful! <Br> "+ Request. QueryString [" Info "] +", welcome to this site ");
}
Else
{
Response. Write ("Logon Failed ");
}
}
3. Obtain the system information of the Web server.
The Request object uses the ServerVariables collection object to save the server-side system information. These information variables are included in the HTTP header and transmitted along with the HTTP Request.The syntax for getting environment variables using the ServerVariables collection object of the Request object is as follows: Request. ServerVariables [environment variable name]
Common Information variables stored in the ServerVariables collection object are as follows:
Copy codeThe Code is as follows:
Response. Write (Request. ServerVariables ["LOCAL_ADDR"]); // address of the remote server
Response. Write ("<br> ");
Response. Write (Request. ServerVariables ["Remote_ADDR"]); // ip address of the host where the browser is located
Response. Write ("<br> ");
Response. Write (Request. Browser. Type. ToString (); // Browser Type
Response. Write ("<br> ");
Response. Write (Request. Browser. Platform. ToString (); // the Platform where the Browser is located
Response. Write ("<br> ");
Response. Write (Request. ServerVariables ["url"]);
4. Obtain client browser Information
The result is obtained through the Browser attribute of the Request object. You need to use the Browser property to generate an HttpBrowserCapabilities object instance. The HttpBrowserCapabilities class has the following common attributes:
Copy codeThe Code is as follows:
Response. Write ("the Browser type is:" + Request. Browser. Browser. ToString () + "<br> ");
Response. Write ("the Browser Version is:" + Request. Browser. Version. ToString () + "<br> ");
Response. Write ("the Platform of the Browser is:" + Request. Browser. Platform. ToString () + "<br> ");
Response. Write ("does the Browser support the framework?" + Request. Browser. Frames. ToString () + "<br> ");
Response. Write ("does the Browser support Cookies:" + Request. Browser. Cookies. ToString () + "<br> ");
Response. Write ("does the Browser support Javascript:" + Request. Browser. JavaScript. ToString () + "<br> ");
5. Read client cookies
Cookie is a way for servers or scripts under the HTTP protocol to maintain information on the customer's workstation. A Cookie is a small text file stored by the Web server in the user's browser. It can contain information about the user, which is stored in the text file in the form of name/value pairs. At any time, the Web site can access Cookie information as long as the user connects to the server. The Cookie is stored in the user's Cookie file. You can still call the Cookie when the user returns it.
Cookies are composed of Cookie objects. The class of the Cookie object is HttpCookie. The main attributes of the HttpCookie class are as follows:
Note the following when using cookies:
[1]. When you use cookies to save the request information sent from the client browser to the server page, the length of time it takes depends on the Expires attribute of the Cookie object and can be set as needed. If no Cookie expiration date is set, they are saved only until the browser is closed. If you set the Expires attribute of the Cookie object to DateTime. MaxValue, the Cookie will never expire.
[2]. The amount of data stored in cookies is limited. Most browsers support a maximum capacity of 4096 bytes. Therefore, do not use cookies to store large amounts of data.
[3]. Not all browsers support cookies and data is stored in plain text on the client computer. Therefore, it is best not to use cookies to store sensitive unencrypted data.
[4]. in ASP. NET has two Cookies, namely, the Cookie set of the Response object and the Cookie set of the Request object. However, the two have different functions. The former can write the Cookie to the client, the latter can be used to read the Cookies stored on the client.
Follow these steps:
Example:
Copy codeThe Code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
HttpCookie c1 = Request. Cookies ["UserName"];
HttpCookie c2 = Request. Cookies ["Password"];
If (c1! = Null | c2! = Null)
{
// After the Cookie is saved (that is, "Save or permanently save"), this will be output. When the website is opened in a unified browser for the second time, it will be output
Response. Write (c1.Value + "welcome ");
}
}
Protected void button#click (object sender, EventArgs e)
{
// Submit
If (TextBox1.Text = "admin" & TextBox2.Text = "123 ")
{
Response. Write ("welcome" + TextBox1.Text );
Response. Cookies ["UserName"]. Value = TextBox1.Text;
Response. Cookies ["Password"]. Value = TextBox2.Text;
If (DropDownList1.SelectedItem. Text = "permanent save ")
{
// The default cookie expiration time is until the browser is closed
// The Cookie is permanently saved.
Response. Cookies ["UserName"]. Expires = DateTime. MaxValue;
Response. Cookies ["Password"]. Expires = DateTime. MaxValue;
}
Else
{
// Cookies are never saved
Response. Cookies ["UserName"]. Expires = DateTime. Now;
Response. Cookies ["Password"]. Expires = DateTime. Now;
}
}
}
When we open the website again (in the same browser), "admin welcome" will pop up"