码迷,mamicode.com
首页 > Windows程序 > 详细

c# 模拟get请求例子,演示Session会话状态。

时间:2014-11-27 06:45:27      阅读:286      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   for   

创建一个控制台 程序:

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

 

 

bubuko.com,布布扣

当cookie中存在sessionID时,保持会话状态。

当每次清空cookie:sessionID时,重新创建新的Session会话。

bubuko.com,布布扣

c# 模拟get请求例子,演示Session会话状态。

标签:style   blog   http   io   ar   color   os   sp   for   

原文地址:http://www.cnblogs.com/han1982/p/4125523.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!