Confused-common HTTP request objects

Source: Internet
Author: User
Tags set cookie
Write background Program To obtain parameters such as the front-end request and return a response, you are not allowed to access objects such as request and response. In fact, there are other objects such as Session, cookie, and ASP. net is 7 and JSP is 9. Because I am a coder under net, I will introduce ASP. net. net.
Seven built-in objects provided by response: the response server sends the data as the request result to the browser (output) request. The browser sends the access requests to the current page to the server (input) applications store variables or objects across web applications, and stop the IIS service (public variables and objects) session to store variables or objects across web applications, the server is suspended offline or valid for a single user object. The server defines a web server-related class that provides access cookies for the methods and attributes of the server to save the Server Page requested by the client browser, stores the reserved non-sensitive user information. cache the cache of Web applications. 2. built-in common attributes, methods, and collections of objects. 1. response object: cache get web page Cache Policy (expiration time, confidentiality, etc.) appendtolog custom log information added to IIS log file C Harset settings or get http output character encoding clear clears the buffer content expires settings or get the number of minutes before the cache page expires on the browser end all the content in the current buffer to the client and then disable cookies to get currently, the requested Cookie set flush sends all the data in the buffer to the client isclientconnected to send it back to the client. Does the client still connect to the server? redirect: redirects the webpage to another address, suppresscontent, and sends the HTTP content to the client browser, if true, the webpage will not be transmitted to the client write and the data will be output to the client browser writefile. the specified file will be directly written to the HTTP content data stream instance: response. write ("Welcome to God's blog! ") Response. writefile (@ "E: \ test.txt"); response. redirect ("login. aspx "); response. write ("response. end; used to debug the program to detect the error location (put at the beginning step by step) 2. request object: applicationpath to obtain the Server ASP. net Virtual application root directory path mappath map the virtual path in the request URL to the physical path on the server browser to get or set functional information about the requesting browser saveas Save the HTTP request to disk contentlength specify the content length sent by the client cookies get the cookie set filepath sent by the client get the virtual path of the current request files get the form using the multi-part MIME format uploaded by the client file set form get the variable set item obtains cookies, forms, Querystringservervariables set the specified object Params get cookies, form, querystringservervariables item Combination set path get the virtual path of the current request querystring get http query string variable set userhostaddress get remote client IP host address userhostname get remote client DNS name (host name) instance: query the browser information response. write (request. browser. platform); response. write (""); response. write (request. usrhostaddress); response. write (""); response. write (request. querystring); response. write (""); this. label. text = Request. querystring ["Accept value"]; // accept the address to pass the value 3. application object: contents is used to access the object name allkeys in the application state set and return all application object variable names to a string array. Add an application object variable staticobjects to determine the value of a specified property of an object or traverse the set, retrieve the attribute count of all static objects to get the number of application object variables clear all application object variables item allow using the index or application variable name to return the content value lock all application object variables remove using the variable name remove an application object variable removeall remove all application object variables set use the variable name to update the content of an Application object variable unlock Solution Except for locking the variable syntax of the Application Object: application ["variable"] = "variable content"; instance: application. add ("app1", "value1"); application. add ("app2", "value2"); For (INT I = 0; I {response. write ("variable name:" + application. getkey (I); response. write (", value:" + application [I] + "");} 4. session Object: contents is used to determine the value of a specified session item or traverse timeout to return or set the validity time of the session object variable. When the user does not take action beyond the validity time, the session object will become invalid, the default time is 20 minutes. Abandon ends the current session and clears all information in the session. If the user subsequently accesses the page, he can create a new session for him (re-establish the session) staticobjects to determine the value of a specified property of an object or traverse the set, search all properties of all static objects, clear all session object variables, but do not end the session Syntax: sessionp ["variable name"] = "content "; // The information stored in the object variablesname = session ["variable name"]; // read information from the session instance: session ["name"] = This. text1.text; // put it in default. the Textbox Control response in the ASPX page. redirect ("default. aspx "); this. label1.text = session ["name"]. tostring (); // put the label control on the default2.aspx page to read information in the session. 5. server Object: machinename get server computer name httpdecode get and Set Request timeout value (in seconds) for strings encoded to eliminate invalid HTML characters) httpencode encodes the string to be displayed in the browser. mappath returns the physical file path urldecode corresponding to the specified virtual path on the Web server to decode the string, this string is encoded for HTTP transmission and sent to the urlencode encoding string of the server in the URL, so that the URL can be used to perform a reliable HTTP transmission instance from the Web server to the client: response. write (server. mappath ("default. aspx "); response. write (server. htmlencode ("God"); response. write (server. htmldecode ("Tian Shen Yu Yi"); respon Se. write (server. machinename); response. write (server. scripttimeout); 6. cookie object: expires sets the cookie validity period. The default value is 1000 minutes. If it is set to 0, you can delete the cookie variable finalize in real time to allow the object to try to release resources before "Garbage Collection" recycles the object and perform other cleanup operations. Name: get the cookie variable name memberwiseclone create a superficial copy of the current object. value is obtained. or set the content value of the cookie variable path to get or set the URL port that cookie applies to get or set the list syntax of the TCP port that cookie applies: response. cookies [name]. value = ""; // to store a cookie variable, you can use the cookie set variable name = request of the response object. coo Kies [name]. value; // to retrieve the cookie, use the cookie set of the request object and return the specified Cookie set. Instance: If (request. Cookies ["user"]! = NULL) {This. label1.text = request. cookies ["user"] ["username"]; this. label2.text = request. cookies ["user"] ["password"];} httpcookie acookie; For (INT I = 0; I {acookie = request. cookies [I]; this. label3.text = string. format ("Cookie username = {0} cookie value = {1}", acookie. name, acookie. value);} cookie, which sometimes uses its plural form of cookies, refers to the data (usually encrypted) stored on the user's local terminal for some websites to identify the user's identity ). Defined in rfc2109. It was invented in March 1993 by Lou montulli, a former employee of Netscape. The server can filter and regularly maintain the information contained in cookies to determine the status during HTTP transmission. The most typical application of cookies is to determine whether a registered user has logged on to the website. The user may be prompted to determine whether to keep the user information during the next visit to simplify the logon procedures, these are the functions of cookies. Another important application scenario is "Shopping Cart. Users may select different items on different pages of the same website within a period of time. These information will be written into cookies so that information can be extracted during the final payment. Cookie can maintain the login information to the user's next session with the server. In other words, when accessing the same website next time, the user will find that the user has logged on without having to enter the user name and password (of course, users cannot delete cookies manually ). Some cookies are deleted when the user exits the session, which can effectively protect personal privacy. Program Code // Write protected void button#click (Object sender, eventargs e) {httpcookie cookie = new httpcookie ("mycook"); // The name datetime dt = datetime of the First-made and set cookie. now; timespan Ts = new timespan (0, 0, 1, 0, 0); // The expiration time is 1 minute cookie. expires = DT. add (TS); // set the cookie expiration time. values. add ("userid", "userid_value"); cookie. values. add ("userid2", "userid2_value2"); response. appendcookie (cookie); // output all content of the cookie // response. write (cooki E. value); // The output is: userid = userid_value & userid2 = userid2_value2} // read protected void button2_click (Object sender, eventargs E) {// httpcookie Cokie = new httpcookie ("mycook"); // If (request. cookies ["mycook"]! = NULL) {// response. write ("the key value of cookie is userid:" + request. cookies ["mycook"] ["userid"]); // The whole line // response. write ("the key value of cookie is userid2" + request. cookies ["mycook"] ["userid2"]); response. write (request. cookies ["mycook"]. value); // output all values} // modify the cookie protected void button3_click (Object sender, eventargs e) {// obtain the cookie object httpcookie COK = request from the client. cookies ["mycook"]; If (COK! = NULL) {// two methods for modifying a cookie, COK. values ["userid"] = "alter-value"; COK. values. set ("userid", "alter-value"); // Add the new content COK to the cookie. values. set ("newid", "newvalue"); response. appendcookie (COK) ;}}// Delete cookie protected void button4_click (Object sender, eventargs e) {httpcookie COK = request. cookies ["mycook"]; If (COK! = NULL) {If (! Checkbox1.checked) {COK. values. remove ("userid"); // remove the value whose key value is userid} else {timespan Ts = new timespan (-1, 0, 0, 0); COK. expires = datetime. now. add (TS); // Delete the entire cookie, as long as the expiration time is set to now} response. appendcookie (COK) ;}} 7. cache object: count get the number of items stored in the cache Add add Add the specified item to the cache object item get or set the cache item get at the specified key retrieve the specified item insert to the cache object Insert the item from the cache object. In a specific version of this method, the existing cache items with the same key parameters are removed from the application's cache object to remove the specified instance: A cache item is assigned to the user. add ("key1", "value", null, datetime. now. addseconds (60), timespan. zero, cacheitempriority. high, onremove); ----------------- global. asax ------------------- global. asax file response ASP. program-level and session-Level Event code triggered by the net and HTTP modules. the asax file is compiled into a dynamically generated net framework class, and ASP. net to automatically reject global. any direct URL request of the asax file. external users cannot download or view the code in it. It is created only when you want to process program events or session events, no output Statement (such as response. write, HTML, etc.), it uses

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.