创建一个控制台 程序:

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 = ; i < ; 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[]);
Thread.Sleep();
}
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() > )
{
result = "Cookie: ";
foreach (var str in myArray)
{
string[] CookieArray = str.Split(';');
result += CookieArray[].Trim();
result += "; ";
}
result = result.Substring(, result.Length - );
}
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 ? : 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会话。

c# 模拟get请求例子,演示Session会话状态。的更多相关文章

  1. php curl模拟post请求的例子

    curl 在php中要模拟post请求数据提交我们会使用到curl函数,下面我来给大家举几个curl模拟post请求提交数据例子有需要的朋友可参考参考. 注意:curl函数在php中默认是不被支持的, ...

  2. php curl模拟post请求提交数据例子总结

    php curl模拟post请求提交数据例子总结 [导读] 在php中要模拟post请求数据提交我们会使用到curl函数,下面我来给大家举几个curl模拟post请求提交数据例子有需要的朋友可参考参考 ...

  3. requests模拟浏览器请求模块初识

    requests模拟浏览器请求模块初识  一.下载 requests模拟浏览器请求模块属于第三方模块 源码下载地址http://docs.python-requests.org/zh_CN/lates ...

  4. ASP模拟POST请求异步提交数据的方法

    这篇文章主要介绍了ASP模拟POST请求异步提交数据的方法,本文使用MSXML2.SERVERXMLHTTP.3.0实现POST请求,需要的朋友可以参考下 有时需要获取远程网站的某些信息,而服务器又限 ...

  5. 使用 jQuery Mockjax 插件模拟 Ajax 请求

    在实际的开发过程中,前端后台协商好了统一的接口,就各自开始自己的任务了.这时候我有这么一个 Ajax 请求需要从后台获取数据: $.ajax({ url: '/products/' }).done(f ...

  6. 使用 HttpWebRequest 发送模拟 POST 请求

    使用HttpWebRequest发送模拟POST请求  网页中,如果form的method="POST",这时点击submit按钮可以给服务器发送了一个POST请求,如果metho ...

  7. vue-cli axios跨域 + 反向代理模拟http请求host+referer

    axios跨域 配置config->index.js中的proxyTable,内容如下: proxyTable: { // 跨域请求 '/api': { // 注意此处可设置为 '*' 代表不限 ...

  8. 使用httpClient模拟http请求

    在很多场景下都需要用到java代码来发送http请求:如和短信后台接口的数据发送,发送数据到微信后台接口中: 这里以apache下的httpClient类来模拟http请求:以get和Post请求为例 ...

  9. java模拟post请求发送json

    java模拟post请求发送json,用两种方式实现,第一种是HttpURLConnection发送post请求,第二种是使用httpclient模拟post请求, 方法一: package main ...

随机推荐

  1. CENTOS安装ElasticSearch

    原文链接:https://my.oschina.net/topeagle/blog/591451?fromerr=mzOr2qzZ CENTOS安装ElasticSearch ElasticSearc ...

  2. Git 使用篇一:初步使用GitHub,下载安装git,并上传项目

    首先在MAC上怎么操作. 在gitHub创立一个账户,在创立一个项目,这就不用我说了对吧. 创建完之后是这样的: 接下来,我们打开https://brew.sh 这是下载homebrew的网站,hom ...

  3. CH6101 最优贸易【最短路】

    6101 最优贸易 0x60「图论」例题 描述 C国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市.任意两个城市之间最多只有一条道路直接相连.这 m 条道路中有一部分为单向通 ...

  4. opencv学习网站

    强烈推荐一个老外的网站,pyimagesearch 网址:https://www.pyimagesearch.com/

  5. 如何将Win10 的环境变量页面设置用在win7上面?

    如何将Win10 的环境变量设置用在win7上面? Win7一般是如下设置: Win10 是如下设置: 解决办法: 注册表 HKEY_LOCAL_MACHINE\SYSTEM\CurrentContr ...

  6. https://pyobjc.readthedocs.io/en/latest/

    https://pyobjc.readthedocs.io/en/latest/ The PyObjC project aims to provide a bridge between the Pyt ...

  7. First normal formal Second normal form

    https://en.wikipedia.org/wiki/First_normal_form https://en.wikipedia.org/wiki/Second_normal_form A r ...

  8. 特征向量-Eigenvalues_and_eigenvectors#Graphs 线性变换

    总结: 1.线性变换运算封闭,加法和乘法 2.特征向量经过线性变换后方向不变 https://en.wikipedia.org/wiki/Linear_map Examples of linear t ...

  9. python MD5步骤

    https://www.cnblogs.com/zipon/p/8340720.html import hashlib def get_token(): md5str = "abc" ...

  10. PAC 自动代理

    最近看了 HTTP权威指南 里面有关于  代理的介绍,代理有很多种,今天主要来说说 自动代理PAC PAC(Proxy Auto Config) 是一个 Script:经由编写这个 Script,我们 ...