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 ...
随机推荐
- 使用HtmlAgilityPack解析html
HtmlAgilityPack是.net下使用xPath来解析html的类库,可以方便的做html的页面分析处理 项目地址: http://htmlagilitypack.codeplex.com/ ...
- 【CF720D】Slalom 扫描线+线段树
[CF720D]Slalom 题意:一个n*m的网格,其中有k个矩形障碍,保证这些障碍不重叠.问你从(1,1)走到(n,m),每步只能往右或往上走,不经过任何障碍的方案数.两种方案被视为不同,当且仅当 ...
- ElasticSearch 聚合函数
一.简单聚合 桶 :简单来说就是满足特定条件的文档的集合. 指标:大多数 指标 是简单的数学运算(例如最小值.平均值.最大值,还有汇总),这些是通过文档的值来计算. 桶能让我们划分文档到有意义的集合, ...
- Java实现网易企业邮箱发送邮件
最近项目需要用网易企业邮箱发送邮件,特意来将实现过程记录一下: maven导入jar包 <!-- javax.mai 核心包 --> <dependency> <grou ...
- Qt qDebug() 的使用方法
在Qt程序调试的时候,经常需要打印一些变量,那么我们就需要使用qDebug()函数,这种函数有两种使用方法,如下所示: QString s = "Jack"; qDebug() & ...
- Pangolin中opengl的混合(gl_blend)
Blend 混合是将源色和目标色以某种方式混合生成特效的技术.混合常用来绘制透明或半透明的物体.在混合中起关键作用的α值实际上是将源色和目标色按给定比率进行混合,以达到不同程度的透明.α值为0则完全透 ...
- python 时间字符串和时间戳之间的转换
https://blog.csdn.net/qq_37193537/article/details/78987949 1.将字符串的时间转换为时间戳 方法: a = " ...
- mysql主从服务器的配置
使用mysql主从复制的好处有: 1.采用主从服务器这种架构,稳定性得以提升.如果主服务器发生故障,我们可以使用从服务器来提供服务. 2.在主从服务器上分开处理用户的请求,可以提升数据处理效率. 3. ...
- SQL语句常见视图操作部分试题(一)
创建一个名称为EMPLOYEES_VU的视图,它基于EMPLOYEES表中的雇员号.雇员名和部门号.将雇员名的列标题改为EMPLOYEE. CREATE VIEW EMPLOYEES_VU AS SE ...
- 非常可乐---hdu 1495(BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=1495 题意: 有3个杯子a b c:a=b+c:然后刚开始时只有a是满的,其它为空的,然后a b c三个之间互相 ...