c# 模拟get请求例子,演示Session会话状态。
创建一个控制台 程序:
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会话状态。的更多相关文章
- php curl模拟post请求的例子
curl 在php中要模拟post请求数据提交我们会使用到curl函数,下面我来给大家举几个curl模拟post请求提交数据例子有需要的朋友可参考参考. 注意:curl函数在php中默认是不被支持的, ...
- php curl模拟post请求提交数据例子总结
php curl模拟post请求提交数据例子总结 [导读] 在php中要模拟post请求数据提交我们会使用到curl函数,下面我来给大家举几个curl模拟post请求提交数据例子有需要的朋友可参考参考 ...
- requests模拟浏览器请求模块初识
requests模拟浏览器请求模块初识 一.下载 requests模拟浏览器请求模块属于第三方模块 源码下载地址http://docs.python-requests.org/zh_CN/lates ...
- ASP模拟POST请求异步提交数据的方法
这篇文章主要介绍了ASP模拟POST请求异步提交数据的方法,本文使用MSXML2.SERVERXMLHTTP.3.0实现POST请求,需要的朋友可以参考下 有时需要获取远程网站的某些信息,而服务器又限 ...
- 使用 jQuery Mockjax 插件模拟 Ajax 请求
在实际的开发过程中,前端后台协商好了统一的接口,就各自开始自己的任务了.这时候我有这么一个 Ajax 请求需要从后台获取数据: $.ajax({ url: '/products/' }).done(f ...
- 使用 HttpWebRequest 发送模拟 POST 请求
使用HttpWebRequest发送模拟POST请求 网页中,如果form的method="POST",这时点击submit按钮可以给服务器发送了一个POST请求,如果metho ...
- vue-cli axios跨域 + 反向代理模拟http请求host+referer
axios跨域 配置config->index.js中的proxyTable,内容如下: proxyTable: { // 跨域请求 '/api': { // 注意此处可设置为 '*' 代表不限 ...
- 使用httpClient模拟http请求
在很多场景下都需要用到java代码来发送http请求:如和短信后台接口的数据发送,发送数据到微信后台接口中: 这里以apache下的httpClient类来模拟http请求:以get和Post请求为例 ...
- java模拟post请求发送json
java模拟post请求发送json,用两种方式实现,第一种是HttpURLConnection发送post请求,第二种是使用httpclient模拟post请求, 方法一: package main ...
随机推荐
- 【CF700E】Cool Slogans 后缀自动机+线段树合并
[CF700E]Cool Slogans 题意:给你一个字符串S,求一个最长的字符串序列$s_1,s_2,...,s_k$,满足$\forall s_i$是S的子串,且$s_i$在$s_{i-1}$里 ...
- numpy生成随机数组
python想要生成随机数的话用使用random库很方便,不过如果想生成随机数组的话,还是用numpy更好更强大一点. 生成长度为10,在[0,1)之间平均分布的随机数组: rarray=numpy. ...
- git回退之前版本
所有没有 commit 的本地改动,都会随着 reset --hard 丢掉,无法恢复. 如果只是想回到 pull 之前当前分支所在的commit位置,则可以.比方说你在 master 分支上,可以用 ...
- AudioUnit录音并同步播放时遇到的问题
AudioComponentDescription desc; desc.componentType =kAudioUnitType_Output; desc.componentSubType = k ...
- IntelliJ IDEA 2018.3注册码
修改hosts windows,打开C:/Windows/System32/drivers/etc/hosts linux打开 vi /etc/hosts 输入: 0.0.0.0 account.je ...
- pyobjc-framework-Cocoa 5.1.2
Introduction — PyObjC - the Python to Objective-C bridge https://pyobjc.readthedocs.io/en/latest/ py ...
- Mybatis 代码自动生成[myeclipse版]
使用环境说明: OS:windows 7 64位 myeclipse: 2017 CI 1.安装 打开myeclipse--help---Install from catalog--选择eclipse ...
- 聊一聊Linux中的工作队列
2018-01-18 工作队列是Linux内核中把工作延迟执行的一种手段,其目的不同于软中断,软中断是提高CPU的响应,尽可能的缩短关中断的时间:而工作队列主要目的是节省资源,其比较适合很微小的任务, ...
- CentOS 7显卡驱动问题
CentOS 7 KDE桌面安装后有时会出现nouveau 驱动问题,导致系统不定时死机或者重启,那么这时只能禁用nouveau 1. 在配置文件中禁用nouveauvi /etc/modprobe. ...
- os模块学习+open行数
os模块的使用https://www.cnblogs.com/juandx/p/4962089.html 注意:新建和关闭文件,可以直接用,无需os模块 python中对文件.文件夹(文件操作函数)的 ...