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 ...
随机推荐
- [转]Android MediaPlayer状态机
翻译Android Reference Manual的MediaPlayer的状态机 对播放音频/视频文件和流的控制是通过一个状态机来管理的.下图显示一个MediaPlayer对象被支持的播放控制操作 ...
- Qt qDebug() 的使用方法
在Qt程序调试的时候,经常需要打印一些变量,那么我们就需要使用qDebug()函数,这种函数有两种使用方法,如下所示: QString s = "Jack"; qDebug() & ...
- 通过IFeatureClass 接口查询 IWorkspace, 查询通配符
IWorkspace pWsI = ((IDataset)pFtCls).Workspace 查询通配符 ISQLSyntax psqls = (ISQLSyntax)(((IDataset)pFtC ...
- 老师的blog整理
python基础部分: 宝哥blog: https://www.cnblogs.com/guobaoyuan/ 开哥blog: https://home.cnblogs.com/u/Neeo 女神笔记 ...
- UOJ #146. 【NOIP2015】信息传递 连通分量 tarjan模板题
http://uoj.ac/problem/146 题解:强连通分量 tarjan模板题.同时试了一下codeblock #include<bits/stdc++.h> using nam ...
- Ubuntu 14.04 配置 LAMP+phpMyAdmin PHP开发环境!
先安装 Apache Web服务器,终端:sudo apt-get install apache2 apache2-doc,然后测试是否安装成功.浏览器地址栏输入:http://l ...
- 源码解读 zsetAdd
https://github.com/antirez/redis/blob/6a6471aad5e4f8d6cbab677b918b14cdee416296/src/t_zset.c /* Updat ...
- 《MYSQL必知必会》
1. 同一个数据库中不允许出现同名表:不同的数据库中可以出现同名表2. 每一行记录都用有一个key(一列或一组列作为key)3. 作为key的列不允许值为空(NULL)4. 多个列作为key时,多个列 ...
- Linux上Oracle 11g安装步骤图解
Oracle 11g下载地址: http://www.oracle.com/technetwork/database/enterprise-edition/downloads/index.html 选 ...
- RHEL6 Systemtap 安装笔记
以 RHEL6u3 为例 1 Systemtap 安装 yum install systemtap 跟systemtap有关的有6,7个,全装上,别偷懒 就用yum安装,别傻傻的去下rpm包,吃力不 ...