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 ...
随机推荐
- H3C系列之三层交换机文件管理
笔者本篇文章所用h3c交换机的型号为三层交换机S3600-28TP-SI 对于文件的操作一般都在用户视图下操作,常见的有如下一些操作: 1.查看操作,常用的查看操作可以使用如下命令: <H3C& ...
- 【BZOJ5091】摘苹果 概率
[BZOJ5091]摘苹果 Description 小Q的工作是采摘花园里的苹果.在花园中有n棵苹果树以及m条双向道路,苹果树编号依次为1到n,每条道路的两 端连接着两棵不同的苹果树.假设第i棵苹果树 ...
- [转]理解Linux的处理器负载均值
[转自]http://www.mike.org.cn/articles/understanding-of-linux-processor-load-average/ 你可能对于Linux的负载均值(l ...
- vue之单表输入绑定
vue的核心:声明式的指令和数据的双向绑定. 那么声明式的指令,已经给大家介绍完了.接下来我们来研究一下什么是数据的双向绑定? 另外,大家一定要知道vue的设计模式:MVVM M是Model的简写,V ...
- Java语言快速实现简单MQ消息队列服务
目录 MQ基础回顾 主要角色 自定义协议 流程顺序 项目构建流程 具体使用流程 代码演示 消息处理中心 Broker 消息处理中心服务 BrokerServer 客户端 MqClient 测试MQ 小 ...
- Java中-classpath和路径的使用
javac -classpath的使用: javac:如果当前你要编译的java文件中引用了其它的类(比如说:继承),但该引用类的.class文件不在当前目录下,这种情况下就需要在javac命令后面加 ...
- vs中如何添加库目录、包含目录以及依赖-----转
在生成时,可能需要首先生成某些项目,以便生成由其他项目使用的可执行代码.使用 “解决方案属性页”对话框 ->“通用属性”->“项目依赖项” 设置当前生成顺序.若要访问此对话框,请在“解决方 ...
- 反正切函数atan与atan2的区别
atan 和 atan2 都是求反正切函数,如:有两个点 point(x1,y1), 和 point(x2,y2); 那么这两个点形成的斜率的角度计算方法分别是: float angle = atan ...
- ZOJ 3983 - Crusaders Quest - [DFS]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3983 题意: 给出一个长度为 $9$ 的字符串 $s$,且 $s ...
- oracle(七)索引
一.B-Tree索引 (1). 选择索引字段的原则: 在WHERE子句中最频繁使用的字段 联接语句中的联接字段 选择高选择性的字段(如果很少的字段拥有相同值,即有很多独特值,则选择性很好) Oracl ...