Objective:
The request object is used primarily to obtain data from the client, such as the user fills in the form's data, the cookie saved on the client, and so on.
I. Overview of the Request object
1. Main properties
Applicationpath |
To get the virtual application root path of the ASP.net application on the server |
Browser |
Gets information about the browser capabilities of the client being requested, which is the value: HttpBrowserCapabilities object |
ContentEncoding |
Gets or sets the character set of the entity body. The property value is the character set encoding object that represents the client |
ContentLength |
Specifies the length of the content sent by the client, in bytes |
ContentType |
Gets or sets the MIME content type of the incoming request. |
Cookies |
Gets the collection of cookies sent by the client, which is the HttpCookieCollection object that represents the cookie variable for the client |
Currentexecutionfilepath |
Gets the virtual path of the current request |
FilePath |
Gets the virtual path of the current request |
Files |
Gets the collection of files uploaded by the client. The property value is a Httpfilecollection object that represents the collection of files uploaded by the client |
Form |
Get form Variable Collection |
HttpMethod |
Gets the HTTP data transfer method used by the client (for example: Get, post, or head) |
Item |
Gets the object specified in the cookie, Form, QueryString, or ServerVariables collection |
Params |
Get a combination set of cookies, Form, querystring, or ServerVariables items |
Path |
Gets the virtual path of the current request |
PathInfo |
Get additional path information for a resource with a URL extension |
Physicalapplicationpath |
Gets the physical file system path of the root directory of the currently executing server application |
PhysicalPath |
Gets the physical file path corresponding to the requested URL |
QueryString |
Gets the collection of HTTP query string variables. The value of this property is: The NameValueCollection object, which contains a collection of query string variables sent by the client |
RequestType |
Gets or sets how the client uses HTTP data transfer (GET or POST) |
ServerVariables |
Get a collection of Web server variables |
TotalBytes |
Gets the number of bytes in the current input stream |
Url |
Get information about the current request URL |
UserHostAddress |
To obtain the IP host address of a remote client |
|
|
2. Main methods
(1) MapPath (virtualpath): Maps the virtual path virtualpath in the URL of the current request to a physical path on the server. Parameter virtualpath Specifies the virtual path of the current request, either an absolute or a relative path. The return value of this method is the server physical path specified by virtualpath.
(2) SaveAs (filename,includeheaders): Saves HTTP requests to disk. Parameter filename Specifies the physical drive path, and Includeheaders is a Boolean value that specifies whether HTTP headers should be saved to disk.
II. Application of Request object
1. Read form variables four ways :
(1). Reading form variables using the Request.Form property
(2). Reading form variables using the Request.QueryString property
(3). Reading form variables using the Request.params property
(4). Read form variables directly from the properties of the server control
(1). Reading form variables using the Request.Form property
The default value for the method property of the HtmlForm control is post. In this case, when the user submits the page, the form data is sent to the server side as an HTTP header. At this point, you can use the form property of the Request object to read the forms variable. For example: txtUserName and Txtpassword text box controls, you can read their values in the following form: request.form["txtUserName"]; request.form["Txtpassword"]
(2) Reading form variables using the Request.QueryString property
If you set the method property of the HtmlForm control to get, the form data is appended to the server side after the Web address when the user submits the page. In this case, you can use the QueryString property of the Request object to read the form variable. request.querystring["txtUserName"]; request.querystring["Txtpassword"]
(3) Reading form variables using the Request.params property
Regardless of the value of the method property of the HtmlForm control, you can use the Params property of the Request object to read the contents of the form variable, such as request.params["Txtpassword"] or request. ["Txtpassword"], which takes precedence over the data submitted by Get method, is searched sequentially in querystring, Form, and servervariable.
Request: Contains the above two methods (get the data submitted by priority), it will be in the QueryString, Form, servervariable in order to search in sequence. Request.params is a collection of all post and get-pass values, Request.params is actually a collection, which in turn includes Request.QueryString, request. Form, Request.Cookies and request.servervariable.
Note: When using Request.params, it is best not to have an item with the same name in these collection items. If you only need one of the data in a Form, but you use the request instead of the Request.Form, then the program will also be searched in QueryString, servervariable. If just querystring or servervariable have the same name, then the resulting value is not what you want.
(4) Directly read form variables through the properties of the server control
In addition to the above 3 ways, you can also read form variables directly from the properties of the server control, which is the most common and easiest way to get form data. For example: txtUsername.Text
2. Reading query string variables
When browsing a Web page, you often see URLs such as "xxx.aspx?id=8018" displayed in the browser's address bar, where xxx.aspx represents the. aspx Web page you want to access, followed by a question mark (?) that follows the query string. The function is to pass the name and value of the variable to the ASP.net file for processing. Query string variables can be generated in several ways.
(1). If you set the method property of the HtmlForm control to get, the form data will be attached to the server side after the URL as a query string variable when the user submits the page.
(2). When you create a hypertext link using the <a>...</a> tag or hyperlink control, you can place the query string behind the target URL and use the question mark "?". To separate the URL from the query string
(3). When the Response.Redirect method is invoked, if a variable name/value pair is appended to the URL parameter, the values attached to the destination Web page are appended to the server side after the URL is opened.
(4). When you enter the request URL in the browser address bar, enter the question mark "?" After the URL. and query strings. For example: Http://.../t.aspx? id=8018
In these situations, query string variables can be retrieved by Request.QueryString property.
The following code:
Copy Code code as follows:
On the landing page
protected void Button1_Click (object sender, EventArgs e)
{
Landing
if (txtUsername.Text = "admin" && txtpwd.text = = "123")
//{
session["Info"] = "random value";
Response.Redirect ("Request2_test.aspx?") info= "+ txtusername.text);
//}
Else
//{
Response.Redirect ("Request2_test.aspx?error= Landing 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= Landing failed!") ");
}
}
On the validation page
Copy Code code as follows:
protected void Page_Load (object sender, EventArgs e)
{
Validate page
if (session["info"]!= null && session["info"]. ToString () = = "Random value")
//{
Response.Write ("Landing success!") <br> "+ request.querystring[" Info "+", welcome to visit this site ");
Response.Write ("Landing success!") <br> "+ request[" Info "+", welcome to visit this site ");
Response.Write ("Login Success!<br>" +request.form["username"]+ ", welcome to visit this site");
//}
Else
//{
Response.Write ("Landing failure");
//}
/*************************************** Method 2**********************************/
if (Convert.ToInt32 (request["Check"]) = = 1)
{
Response.Write ("Landing success!") <br> "+ request.querystring[" Info "+", welcome to visit this site ");
}
Else
{
Response.Write ("Landing failure");
}
}
3. Obtaining system information on the Web server side
The request object uses the ServerVariables collection object to hold server-side system information, which is contained in the HTTP header and is routed along with the HTTP request. The syntax for obtaining environment variables using the ServerVariables collection object of the Request object is as follows: request.servervariables[environment variable name]
The common information variables saved in the ServerVariables collection object are as follows:
Copy Code code as follows:
Response.Write (request.servervariables["LOCAL_ADDR"])//remote server address
Response.Write ("<br>");
Response.Write (request.servervariables["REMOTE_ADDR");//The IP address of the host on which the browser resides
Response.Write ("<br>");
Response.Write (Request.Browser.Type.ToString ());//Type of browser
Response.Write ("<br>");
Response.Write (Request.Browser.Platform.ToString ());//the platform where the browser resides
Response.Write ("<br>");
Response.Write (request.servervariables["url"]);
4. Obtaining client browser Information
Obtained by the browser property of the Request object. You need to use the browser property to generate an object instance of HttpBrowserCapabilities type. The common properties that the HttpBrowserCapabilities class has are as follows:
Copy Code code as follows:
Response.Write ("Browser type is:" + Request.Browser.Browser.ToString () + "<br>");
Response.Write ("Browser version is:" + Request.Browser.Version.ToString () + "<br>");
Response.Write ("Browser's platform is:" + Request.Browser.Platform.ToString () + "<br>");
Response.Write ("Browser support frame:" + Request.Browser.Frames.ToString () + "<br>");
Response.Write ("Browser support Cookies:" + Request.Browser.Cookies.ToString () + "<br>");
Response.Write ("Browser support JavaScript:" + Request.Browser.JavaScript.ToString () + "<br>");
5. Read Client Cookies
Cookies are a way for servers or scripts to maintain information on client workstations under the HTTP protocol. A cookie is a small text file saved by a Web server in a user's browser, which can contain information about the user, which is stored in a text file as a name/value pair. Whenever a user connects to a server, the Web site can access cookie information. The cookie is saved in the user's cookie file and can still be invoked the next time the user returns.
A collection of cookies is made up of some cookie objects. The class name of the cookie object is HttpCookie. The main properties of the HttpCookie class are as follows:
When using cookies, be aware of the following points
[1]. When you use cookies to save request information for a client browser requesting a server page, the length of time saved depends on the Expires property of the cookie object and can be set as needed. If the expiration date of the cookie is not set, they are saved only until the browser is closed. Setting the Expires property of a cookie object to DateTime.MaxValue means that the cookie will never expire.
[2]. Cookies store a limited amount of data, and most browsers support a maximum capacity of 4096 bytes, so do not use cookies to hold large amounts of data.
[3]. Not all browsers support cookies, and the data is stored in clear text on the client computer, so it is best not to use cookies to store sensitive unencrypted data.
[4]. There are two sets of cookies in the ASP.net, namely: The Response object's cookie collection and the Request object's cookies collection, but the function is different, through the former can write the cookie to the client, through the latter can read the cookie stored in the client.
Follow these steps:
Examples are as follows:
Copy Code code 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)
{
This can be output when the cookie is saved (that is, "save or permanently saved") and output when the Web site is opened for the second time in a unified browser
Response.Write (C1. Value + "Welcome");
}
}
protected void Button1_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")
{
Default cookie expiration time is until the browser is closed
Cookie Save Permanent
response.cookies["UserName"]. Expires = DateTime.MaxValue;
response.cookies["Password"]. Expires = DateTime.MaxValue;
}
Else
{
Cookies never Save
response.cookies["UserName"]. Expires = DateTime.Now;
response.cookies["Password"]. Expires = DateTime.Now;
}
}
}
When we open the site again (the same browser), it will pop up "admin welcome"