zoukankan      html  css  js  c++  java
  • 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 = 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("
    ", "
    ");
                    string[] html = result.Split('
    ');
                    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());
             
            }

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

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

  • 相关阅读:
    mssql 循环的写法,备用
    用了十几年的windows记录下我不知道的几个快捷键
    折腾了下java下webservice,折腾了大半天,居然是eclipse的版本不对
    连接Linux 下mysql 慢的问题,解决之
    解决windows7蓝屏的方法
    MySQL錯誤:Value '00000000' can not be represented as java.sql.Date解決方法[转]
    jdbc连接三种数据库的连接语句写法(备查)
    遇到一个json解析的错误,费了好大的劲,最后发现是少了一个包
    【转】The reference to entity "characterEncoding" must end with the ';' delimiter
    synaptics 插入USB鼠标禁用,网上
  • 原文地址:https://www.cnblogs.com/han1982/p/4125523.html
Copyright © 2011-2022 走看看