How to apply session development non-Web terminal

zhaozj2021-02-16  41

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 (" identity authentication </ title> </ head> " ); out.println ("<body>"); httpsession session = request.getSession (false); if (session! = null) {Out.println ("<p> identification </ p>");} else {OUT.PRINTLN ("<p> 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.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-23652.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="23652" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.052</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = '_2FsQ3xARubnusn3f9ZmMOBzMRG1HGfmvJNmcQXM7F7y6bCQiippADngX3OkjU4HP5GV2KmXOVPe_2BGg_2Bv1pGt1KQ_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>