Analysis of Request object technology in asp.net

Source: Internet
Author: User
Tags servervariables
Request object
When the client sends a Request to execute the asp.net program, the CLR will include the client Request information in the Request object. These request information includes the request header, basic information of the client (such as the browser type, browser version number, user language, and encoding method), request methods (such as post and get ), parameter Name and value.
Request object call method: Request. Collection ["Variable"];
Collection includes four types: QueryString, Form, Cookies, and ServerVariables.
1. The information collected by the QueryString collection is from "? ", Which is called url additional information. For example, www.sina.com/show.asp? Id = 111
In this url, the information collected by QueryString is "show. asp? "Subsequent data" id = 111 ". The statement for obtaining the parameter value of "id" is Request. QueryString ["id"];
QueryString is mainly used to collect the data sent by get requests in the http protocol. If the requested program url address appears in a request event "? ", Indicates that the request method is get. The get method is the default Request Method in http.
So how can I send a get request?
(1) <a href = "show. aspx? Id = 111 "> display articles with ID 111 </a>
(2) <form action = "show. aspx" method = "get">
<Input type = "text" name = "id" value = "111">
</Form>
The following example shows two programs, form1.htmand getinfo1.aspx. form1.htm sends get requests and GetInfo1.aspx receives get requests.
//////////////////////////////////////// ///////////
Form1.htm
//////////////////////////////////////// ///////////
<HTML>
<HEAD>
<TITLE> send a GET request </TITLE>
</HEAD>
<BODY>
<Center>
Send GET request
<Hr>
<Form action = "GetInfo1.aspx" method = "get">
Your name: <input type = "text" name = "nickname"> <br>
<Input type = "submit" value = "send">
<Form>
</Center>
</BODY>
</HTML>
//////////////////////////////////////// //
GetInfo1.aspx
//////////////////////////////////////// //
<% @ Page Language = "C #" %>
<Html>
<Head>
<Title> receive GET requests </title>
</Head>
<Body>
<Center>
Receive the value from the GET method:
<Hr>
<%
String NickName = Request. QueryString ["nickname"]; <% -- receive parameter value -- %>
Response. Write ("the value of nickname sent from the GET method is:" + NickName );
%>
</Center>
</Body>
</Html>
2. The get method is to append the transmitted data to the url. The url address length is limited, so the data that can be transmitted using the get method is also limited. Generally, the get method can transmit 256 bytes of data. In most cases, the length of data transmitted using the get method is far from enough, in this case, you need to use another http Request Method post. The maximum data that the post method can transmit is 2 MB.
Post requests must be sent by form. In addition, when using the post request method, you must set "method" to "post ". Asp.net uses the Request. Form Method to receive data transmitted by the post method: Request. Form ["Variable"];
Next, we will give two examples to illustrate the use of the post method. Similar to the above example, pay attention to the comparison.
//////////////////////////////////////// //////
Form2.htm // send a post request
//////////////////////////////////////// //////
<HTML>
<HEAD>
<TITLE> send a POST request </TITLE>
</HEAD>
<BODY>
<Center>
Send POST request
<Hr>
<Form action = "GetInfo2.aspx" method = "post">
Your name: <input type = "text" name = "nickname"> <br>
<Input type = "submit" value = "send">
<Form>
</Center>
</BODY>
</HTML>
//////////////////////////////////////// //////////
GetInfo2.aspx // receives post requests
//////////////////////////////////////// //////////
<% @ Page Language = "C #" %>
<Html>
<Head>
<Title> receive POST requests </title>
</Head>
<Body>
<Center>
Receive the value from the POST method:
<Hr>
<%
String NickName = Request. Form ["nickname"];
Response. Write ("the value of nickname sent from the POST method is:" + NickName );
%>
</Center>
</Body>
</Html>
3. Use of cookies will be discussed later.
4. ServerVariable
ServerVariable (environment variable) contains the system information of the client and server. The method for obtaining the environment Variable value is: Request. ServerVariables ["Variable"];
Variable parameter description
HTTP_USER_AGENT: Obtain the browser type and version used by the user.
REMOTE_ADDR
REQUEST_METHOD
LOCAL_ADDR: Obtain the IP address of the server
SERVER_NAME: Obtain the Host Name of the server.
PATH_INFO: Obtain the virtual path of the current executable program
PATH_TRANSLATED: Obtain the absolute path of the current execution Program
CONTENT_LENGTH obtains the total number of characters sent by the request program.
CONTENT_TYPE
GATEWAY_INTERFACE
QUERY_STRING get additional url Information
SCRIPT_NAME: get the file name of the current program (including the Virtual Path)
SERVER_PORT: gets the port from which the server accepts the request.
SERVER_PROTOCOL: Obtain the protocol and version number that the server complies.
HTTP_ACCEPT_LANGUAGE: gets the language used by the user.
The last example is about ServerVariable. The result of this example can also be used as the original data of the ServerVariable set for future reference.
//////////////////////////////////////// /////////////////
<% @ Page Language = "C #" %>
<% @ Import Namespace = "System. Data" %>
<Script Language = "C #" Runat = "Server">
Public void Page_Load (Object src, EventArgs e)
{
// Obtain the ServerVariables variable set
NameValueCollection ServerVariables = Request. ServerVariables;

// Generate a data set. Its usage will be discussed later.
DataTable dt = new DataTable ();
DataRow dr;

Dt. Columns. Add (new DataColumn ("environment variable", typeof (string )));
Dt. Columns. Add (new DataColumn ("variable value", typeof (string )));

Foreach (string SingleVariable in ServerVariables)
{
Dr = dt. NewRow ();
Dr [0] = SingleVariable;
Dr [1] = ServerVariables [SingleVariable]. ToString ();
Dt. Rows. Add (dr );
}

DataGrid1.DataSource = new DataView (dt );
DataGrid1.DataBind ();
}
</Script>
<Html>
<Head>
<Title> </title>
</Head>
<Body>
<ASP: DataGrid id = "DataGrid1" runat = "server"
BorderColor = "black"
BorderWidth = "1"
GridLines = "Both"
CellPadding = "3"
CellSpacing = "0"
Font-Name = "Verdana"
Font-Size = "8pt"
HeaderStyle-BackColor = "# aaaadd"
AlternatingItemStyle-BackColor = "# eeeeee"
/>
</Body>
</Html>
//////////////////////////////////////
5. Browser object of the Request
Request. Browser. Browser // check the Browser type
Request. Browser. Version // check the Browser Version
Request. Browser. ActiveXControls // check whether the Browser supports ActiveX Controls
Request. Browser. Cookies // check whether the Browser supports Cookies
Request. Browser. VBScript // check whether the Browser supports VBScript
The following example shows all the accessible attributes of Browser.
//////////////////////////////////////// ////////////////////
<Html>
<Head>
<Script runat = "server" language = "c #">
Public void Page_Load (Object Source, EventArgs E)
{
HttpBrowserCapabilities bc = Request. Browser;

Welcome. Text = "Hello, you are using" + bc. Browser + "v." + bc. Version + ". Your running Platform is" + bc. Platform;

ActiveXControls. Text = bc. ActiveXControls. ToString ();
AOL. Text = bc. AOL. ToString ();
BackgroundSounds. Text = bc. BackgroundSounds. ToString ();
Beta. Text = bc. Beta. ToString ();
Browser. Text = bc. Browser. ToString ();
CDF. Text = bc. CDF. ToString ();
Cookies. Text = bc. Cookies. ToString ();
Crawler. Text = bc. Crawler. ToString ();
Frames. Text = bc. Frames. ToString ();
JavaApplets. Text = bc. JavaApplets. ToString ();
JavaScript. Text = bc. JavaScript. ToString ();
MajorVersion. Text = bc. MajorVersion. ToString ();
MinorVersion. Text = bc. MinorVersion. ToString ();
Platform. Text = bc. Platform. ToString ();
Tables. Text = bc. Tables. ToString ();
Type. Text = bc. Type. ToString ();
VBScript. Text = bc. VBScript. ToString ();
Version. Text = bc. Version. ToString ();
Win16.Text = bc. Win16.ToString ();
Win32.Text = bc. Win32.ToString ();
}
</Script>
<Style>
Body {font-size: 9pt}
Td {font-size: 9pt}
</Style>
</Head>
<Body>
<Form runat = "server" method = "post">
Your browser information is fully in our grasp ^ & ^: <br>
<Asp: Label runat = "server" id = "Welcome" Font-Bold = "True"/>
<Table border = "1" width = "400" bordercolor = "black">
<Tr bgcolor = "skyblue">
<Td width = "50%"> <B> browser properties <B> </td>
<Td width = "50%"> <B> detection result <B> </td>
</Tr>
<Tr>
<Td width = "50%"> ActiveXControls: </td>
<Td width = "50%"> <asp: Label runat = "server" id = "ActiveXControls"/> </td>
</Tr>
<Tr bgcolor = "skyblue">
& Lt; td width = "50%" & gt; AOL: & lt;/td & gt;
<Td width = "50%"> <asp: Label runat = "server" id = "AOL"/> </td>
</Tr>
<Tr>
& Lt; td width = "50%" & gt; BackgroundSounds: & lt;/td & gt;
<Td width = "50%"> <asp: Label runat = "server" id = "BackgroundSounds"/> </td>
</Tr>
<Tr bgcolor = "skyblue">
& Lt; td width = "50%" & gt; Beta: & lt;/td & gt;
<Td width = "50%"> <asp: Label runat = "server" id = "Beta"/> </td>
</Tr>
<Tr>
<Td width = "50%"> Browser: </td>
<Td width = "50%"> <asp: Label runat = "server" id = "Browser"/> </td>
</Tr>
<Tr bgcolor = "skyblue">
<Td width = "50%"> CDF: </td>
<Td width = "50%"> <asp: Label runat = "server" id = "CDF"/> </td>
</Tr>
<Tr>
<Td width = "50%"> Cookies: </td>
<Td width = "50%"> <asp: Label runat = "server" id = "Cookies"/> </td>
</Tr>
<Tr bgcolor = "skyblue">
<Td width = "50%"> Crawler: </td>
<Td width = "50%"> <asp: Label runat = "server" id = "Crawler"/> </td>
</Tr>
<Tr>
<Td width = "50%"> Frames: </td>
<Td width = "50%"> <asp: Label runat = "server" id = "Frames"/> </td>
</Tr>
<Tr bgcolor = "skyblue">
<Td width = "50%"> JavaApplets: </td>
<Td width = "50%"> <asp: Label runat = "server" id = "JavaApplets"/> </td>
</Tr>
<Tr>
<Td width = "50%"> JavaScript: </td>
<Td width = "50%"> <asp: Label runat = "server" id = "JavaScript"/> </td>
</Tr>
<Tr bgcolor = "skyblue">
<Td width = "50%"> MajorVersion: </td>
<Td width = "50%"> <asp: Label runat = "server" id = "MajorVersion"/> </td>
</Tr>
<Tr>
<Td width = "50%"> MinorVersion: </td>
<Td width = "50%"> <asp: Label runat = "server" id = "MinorVersion"/> </td>
</Tr>
<Tr bgcolor = "skyblue">
<Td width = "50%"> Platform: </td>
<Td width = "50%"> <asp: Label runat = "server" id = "Platform"/> </td>
</Tr>
<Tr>
<Td width = "50%"> Tables: </td>
<Td width = "50%"> <asp: Label runat = "server" id = "Tables"/> </td>
</Tr>
<Tr bgcolor = "skyblue">
& Lt; td width = "50%" & gt; Type: & lt;/td & gt;
<Td width = "50%"> <asp: Label runat = "server" id = "Type"/> </td>
</Tr>
<Tr>
<Td width = "50%"> VBScript: </td>
<Td width = "50%"> <asp: Label runat = "server" id = "VBScript"/> </td>
</Tr>
<Tr bgcolor = "skyblue">
<Td width = "50%" type = "codeph" text = "codeph"> Version: </td type = "codeph" text = "/codeph">
<Td width = "50%"> <asp: Label runat = "server" id = "Version"/> </td>
</Tr>
<Tr>
<Td width = "50%"> Win16: </td>
<Td width = "50%"> <asp: Label runat = "server" id = "Win16"/> </td>
</Tr>
<Tr bgcolor = "skyblue">
& Lt; td width = "50%" & gt; Win32: & lt;/td & gt;
<Td width = "50%"> <asp: Label runat = "server" id = "Win32"/> </td>
</Tr>
</Table>
</Form>
</Body>
</Html>
//////////////////////////////////////// //////////////////////////
6. other Request attributes and Methods
FilePath: Obtain the file path of the current request.
HttpMethod
Files is related to file upload, which will be explained later
Params obtains the QueryString + Form + ServerVariable + Cookies set.
Size of the TotalBytes request content
Url to obtain url Information
UserHostAddress: Get the user's IP address
UserHostName: Get the user's host name
Userages gets the user's language

Related Article

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.