博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c# 模拟get请求例子,演示Session会话状态。
阅读量:4684 次
发布时间:2019-06-09

本文共 3951 字,大约阅读时间需要 13 分钟。

创建一个控制台 程序:

using System;using System.Collections.Generic;using System.IO;using System.IO.Compression;using System.Linq;using System.Net;using System.Net.Sockets;using System.Runtime.Remoting.Messaging;using System.Text;using System.Text.RegularExpressions;using System.Threading;using System.Threading.Tasks;namespace ConsoleApplication1{    class Program    {        static void Main(string[] args)        {            string CookieStr = string.Empty;            string result = "";            for (int i = 0; i < 10000; i++)            {                CookieStr = string.Empty; //每次都清除cookie SessionID                result = 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", ref CookieStr);                result = result.Replace("\r\n", "\r");                string[] html = result.Split('\r');                Console.WriteLine(html[0]);                Thread.Sleep(10);            }            Console.ReadKey();        }        private static string SimulatedGet(string Url,ref string CookieStr)        {            //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            string result = "";            WebClient context = new WebClient();            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); //把cookie添加到请求报文头中。            }            context.Encoding = Encoding.UTF8;            result = context.DownloadString(Url);            if (string.IsNullOrEmpty(CookieStr))            {                CookieStr = context.ResponseHeaders["Set-Cookie"].ToString();                CookieStr = GetCookie(CookieStr);            }            return result;        }        private static string GetCookie(string CookieStr)        {            string result = "";            string[] myArray = CookieStr.Split(',');            if (myArray.Count() > 0)            {                result = "Cookie: ";                foreach (var str in myArray)                {                    string[] CookieArray = str.Split(';');                    result += CookieArray[0].Trim();                    result += "; ";                }                result = result.Substring(0, result.Length - 2);            }            return result;        }        }}

 

Global.asax Session_Start事件

统计在线人数。

protected void Session_Start(object sender, EventArgs e)        {            Response.Write(Session.SessionID);            Application.Lock();            int num = Application["OnLineUsers"] == null ? 0 : Convert.ToInt32(Application["OnLineUsers"]);            num++;            Application["OnLineUsers"] = num;            Application.UnLock();        }

aspx访问页面后台page_load事件中显示在线人数。

protected void Page_Load(object sender, EventArgs e)        {            Response.Write("当前在线人数:" + Application["OnLineUsers"].ToString());                 }

 

 

当cookie中存在sessionID时,保持会话状态。

当每次清空cookie:sessionID时,重新创建新的Session会话。

转载于:https://www.cnblogs.com/han1982/p/4125523.html

你可能感兴趣的文章
【JBPM4】判断节点decision 方法3 handler
查看>>
filter 过滤器(监听)
查看>>
node启动时, listen EADDRINUSE 报错;
查看>>
杭电3466————DP之01背包(对状态转移方程的更新理解)
查看>>
kafka中的消费组
查看>>
python--注释
查看>>
SQL case when else
查看>>
MVc Identity登陆锁定
查看>>
cdn连接失败是什么意思_关于CDN的原理、术语和应用场景那些事
查看>>
ultraedit26 运行的是试用模式_免费试用U盘数据恢复工具 – 轻松找回U盘丢失的各种数据!...
查看>>
python sum函数导入list_python sum函数iterable参数为二维list,start参数为“[]”该如何理解...
查看>>
UVa540 Team Queue
查看>>
android 练习之路 (八)
查看>>
tp5 中 model 的聚合查询
查看>>
android wear开发之:增加可穿戴设备功能到通知中 - Adding Wearable Features to Notifications...
查看>>
压缩文件函数库(转载)
查看>>
【转】ubuntu12.04没有/var/log/messages解决
查看>>
Oracle EBS 初始化用户密码
查看>>
SYS_CONTEXT 详细用法
查看>>
Pycharm配置autopep8让Python代码更符合pep8规范
查看>>