zoukankan      html  css  js  c++  java
  • C#模拟网站登录介绍

    我们在写灌水机器人、抓资源机器人和Web网游辅助工具的时候第一步要实现的就是用户登录。那么怎么用C#来模拟一个用户的登录,要实现用户的登录,那么首先就必须要了解一般网站中是怎么判断用户是否登录的。

    HTTP协议是一个无连接的协议,也就是说这次对话的内容和状态与上次的无关,为了实现和用户的持久交互,网站与浏览器之前在刚建立会话时将在服务 器内存中建立一个Session,该Session标识了该用户(浏览器),每一个Session都有一个唯一的ID,第一次建立会话时服务器将生成的这 个ID传给浏览器,浏览器在接下来的浏览中每一个发向服务器的请求中都将包含该SessionID,从而标识了自己的身份。

    服务器上是使用内存来保存Session中的信息,那么浏览器又使用什么来保存服务器分配的这个SessionID了对,是Cookie。在刚建立 会话时浏览器向服务器的请求中将不包含SessionID在Cookie中,服务器就认为是一个全新的会话,从而在服务器上分配一段内存给该 Session用,同时将该Session的ID在Http Header中使用Set-Cookie发送给浏览器。

    现在原理已经搞清楚了,那么我们就来实现一个网站的登录嘛。下面以某某大学的管理信息系统来进行检验(注意:这里的缺陷就在于没有验证码的识别和多个服务器的跳转)难度相对来说要小很多。

     首先先用httpAnaly或者是httpwatch等专用的抓包工具,来获取网页提交时候的数据信息和头信息。以下代码包含了登陆和在登陆后获取另一个页面数据信息。

          private void Form1_Load(object sender, EventArgs e)
            {
    
               string username = "xxxx";//用户名
                string password = "xxxx";//密码
              //新建一个用于保存cookies的容器     
               CookieContainer container = new CookieContainer();
             //拼接post数据
               string postData = ("username=" + username);
                postData += ("&passwd=" + password);
              postData += ("&login=%B5%C7%A1%A1%C2%BC");
               ASCIIEncoding encoding = new ASCIIEncoding();
              byte[] data = encoding.GetBytes(postData);
               HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://xxxx/xxxx/login.asp");
               request.Method = "Post";
                request.ContentType = "application/x-www-form-urlencoded";
              request.ContentLength = data.Length;
               request.KeepAlive = true;
               request.CookieContainer = container;  //返回的cookie会附加在这个容器里面
              //发送数据
               Stream newStream = request.GetRequestStream();
              newStream.Write(data, 0, data.Length);
               newStream.Close();
               //以下俩句不可缺少
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                response.Cookies = container.GetCookies(request.RequestUri);
    
               HttpWebRequest requestScore = (HttpWebRequest)WebRequest.Create("http://xxxx/xxxx/Score.asp");
                postData = "term=&TermList=%C7%EB%D1%A1%D4%F1&ckind=&lwPageSize=100&lwBtnquery=%B2%E9%D1%AF";
               data = encoding.GetBytes(postData);
                requestScore.Method = "Post";
               requestScore.ContentType = "application/x-www-form-urlencoded";
               requestScore.ContentLength = data.Length;
               requestScore.KeepAlive = true;
    
                //使用登陆的cookies通过接下来的验证
               requestScore.CookieContainer = container;
               Stream stream = requestScore.GetRequestStream();
                stream.Write(data, 0, data.Length);
               stream.Close();
               HttpWebResponse responseSorce = (HttpWebResponse)requestScore.GetResponse();
              StreamReader reader = new StreamReader(responseSorce.GetResponseStream(), Encoding.Default);
               string content = reader.ReadToEnd();
               textBox1.Text = content;
          }
  • 相关阅读:
    164 Maximum Gap 最大间距
    162 Find Peak Element 寻找峰值
    160 Intersection of Two Linked Lists 相交链表
    155 Min Stack 最小栈
    154 Find Minimum in Rotated Sorted Array II
    153 Find Minimum in Rotated Sorted Array 旋转数组的最小值
    152 Maximum Product Subarray 乘积最大子序列
    151 Reverse Words in a String 翻转字符串里的单词
    bzoj3994: [SDOI2015]约数个数和
    bzoj 4590: [Shoi2015]自动刷题机
  • 原文地址:https://www.cnblogs.com/wangchuang/p/3042118.html
Copyright © 2011-2022 走看看