C # simulates a GET request example to demonstrate session state.

Source: Internet
Author: User

To create a console program:

usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.IO.Compression;usingSystem.Linq;usingSystem.Net;usingSystem.Net.Sockets;usingSystem.Runtime.Remoting.Messaging;usingSystem.Text;usingSystem.Text.RegularExpressions;usingSystem.Threading;usingSystem.Threading.Tasks;namespaceconsoleapplication1{classProgram {Static voidMain (string[] args) {            stringCOOKIESTR =string.            Empty; stringresult ="";  for(inti =0; I <10000; i++) {cookiestr=string. Empty;//Clear Cookies every time SessionIDresult = Simulatedget ("http://localhost:1342/%E5%85%A8%E5%B1%80%E5%BA%94%E7%94%A8%E7%A8%8B%E5%BA%8F%E5%8F%98%E9%87%8F%E7%BB%9F%E8 %ae%a1%e5%9c%a8%e7%ba%bf%e4%ba%ba%e6%95%b0.aspx",refcookiestr); Result= result. Replace ("\ r \ n","\ r"); string[] html = result. Split ('\ r'); Console.WriteLine (html[0]); Thread.Sleep (Ten);        } console.readkey (); }        Private Static stringSimulatedget (stringUrlref stringcookiestr) {            //get/newsadmin/login.aspx http/1.1//Host:localhost//user-agent:mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) gecko/20100101 firefox/33.0//accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8//accept-language:zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3//Accept-encoding:gzip, deflate//connection:keep-alive            stringresult =""; WebClient Context=NewWebClient (); Context. Headers.add ("Host:localhost"); Context. Headers.add ("user-agent:mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) gecko/20100101 firefox/33.0"); Context. Headers.add ("accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); Context. Headers.add ("accept-language:zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3"); Context. Headers.add ("Content-type:multipart/form-data"); Context. Headers.add ("Accept-encoding:gzip, deflate"); Context. Headers.add ("Cache-control:no-cache");//connection:keep-alive            if(!string. IsNullOrEmpty (cookiestr)) {context. Headers.add (COOKIESTR); //Add the cookie to the request header. } context. Encoding=Encoding.UTF8; Result=context.            Downloadstring (URL); if(string. IsNullOrEmpty (cookiestr)) {cookiestr= Context. responseheaders["Set-cookie"].                ToString (); Cookiestr=GetCookie (COOKIESTR); }            returnresult; }        Private Static stringGetCookie (stringcookiestr) {            stringresult =""; string[] MyArray = Cookiestr.split (','); if(Myarray.count () >0) {result="Cookies:"; foreach(varStrinchMyArray) {                    string[] Cookiearray = str. Split (';'); Result+ = cookiearray[0].                    Trim (); Result+="; "; } result= result. Substring (0, result. Length-2); }            returnresult; }        }}

Global.asax Session_Start Events

Count the number of people online.

protected voidSession_Start (Objectsender, EventArgs e)            {Response.Write (Session.SessionID);            Application.Lock (); intnum = application["onlineusers"] ==NULL?0: Convert.ToInt32 (application["onlineusers"]); Num++; application["onlineusers"] =num;        Application.UnLock (); }

The ASPX access page shows the number of people online in the background Page_Load event.

protected void Page_Load (object  sender, EventArgs e)        {            Response.Write (" current online number:" + application["onlineusers"]. ToString ());                 }

Maintain session state when there is sessionid in the cookie.

When the Cookie:sessionid is emptied each time, a new session is recreated.

C # simulates a GET request example to demonstrate session state.

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.