How to apply session development non-Web terminal
DuzESSION is a more effective information interaction on the Web. Because of its convenient, stable and safe and reliable, it is favored by many web developers. Especially in Internet identity authentication, online electronic shopping, etc. is more wide. Coincidentally, the author when developing a financial project data center platform, I feel that the identity authentication of the data transfer and the information interaction and the SESSION control of the web are extremely similar. So I want to try this new technology, I feel that the information interaction of the non-Web client with session is practicable by reviewing a large amount of information. After repeated test successfully, it is applied to the project, the effectiveness is remarkable, saving more temporary data preservation and lock state detection, and automatically maintains a status by session. Good things can't enjoy, the author wants to discuss this successful application session control to non-web development key technology points to discuss. We know that cookie is the most commonly used tracking user session on the web. When the cookie is disabled, it is generally rewritten to track sessions with URL. So what is the cookie? According to the definition: cookie is a clip message sent by the server to the customer, stored in the customer environment, and send it back in the client's request. For example, when we log in to an electronic shopping mall with IE, IE also receives the set-cookie response header information while getting the list of goods. The format of this information is "set-cookie: name = value; comment = comment; domain = domainnmae; max-age = seconds; path = path; second; version = 1 * DIGIT", where Name value pair (value) Sectional separation) is necessary, the rest are optional. The most important information is of course in the right value, value is the value of Name, is also the logo of this cookie, the Max-Age defines the maximum time of the cookie, and several other optional values can be referred to http: //www.faqs.org/rfcs/rfc2109.html. When we purchase some kind of item, send an option list to the server, will automatically add the name value in your request information, if the cookie is disabled, use the URL rewrite mode to add Name values on the URL request address. Correct. When the web server receives this request, check if the cookie exists and then tracks the session. From the above analysis, it is not difficult to understand. In fact, the web server tracking session relies on SET-COOKIE header information to track the NAME value to authenticate. If we use a non-Web terminal to receive the response information of the web server, the cookie header information is parsed, and when the request is added to the web server, the WEB server will not be authenticated according to this. With the above analysis, we have written the code very convenient. Below is a demo code interacting with a servlet in the Apache Tomcat 4.0 service engine with a C Builder 6 application with a reference.
The code when the C client sends a request to the server is as follows: TIDHTTP * httpclient = new tidhttp (null); TidHeaderList * hlist; string url = "http: // localhost: 8080 / rev / servlet / test"; try {TRY {HttpClient-> get (URL); if (httpclient-> response! = Null) {hlist = httpclient-> response-> extraheaders; string cookie = hlist-> value ["set-cookie"; int pos = cookie. POS (";;"); if (pOS> 0) session_id = cookie.substring (1, POS-1); else session_id = cookie;}} Catch (Exception & E) {}} __finally {httpclient-> free (); } The above code The variable URL points to the HTTP address of the servlet, assigns the value according to the respective situation; the variable session_id is a global variable, record the cookie. Just add "httpclient-> request-> extraheaders-> add (" cookie: " session_id); Apache Tomcat is automatically discriminated before the HTTPCLIENT request. Is it simple? Servlet validation server is relatively easy, a specific authentication process Cookie Apach Tomcat let the engine to do as follows: public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType ( "text / HTML; Charset = GBK "); PrintWriter Out = response.getwriter (); out.println (" "); out.println ("
identification p>");} else {OUT.PRINTLN ("
authentication failed p>");} out.println (" body> html>");} The most critical is "Request.GetSession (false); ", The parameter is TRUE when apache tomcat creates a new session; the Apache Tomcat is looking for associated session based on the information in the request.