使用c# 類比網站登入

來源:互聯網
上載者:User

我們在寫灌水機器人、抓資源機器人和Web網遊協助工具輔助的時候第一步要實現的就是使用者登入。那麼怎麼用C#來類比一個使用者的登入拉要實現使用者的登入,那麼首先就必須要瞭解一般網站中是怎麼判斷使用者是否登入的。

HTTP協議是一個不需連線的協議,也就是說這次對話的內容和狀態與上次的無關,為了實現和使用者的持久互動,網站與瀏覽器之前在剛建立會話時將在服務 器記憶體中建立一個Session,該Session標識了該使用者(瀏覽器),每一個Session都有一個唯一的ID,第一次建立會話時伺服器將產生的這 個ID傳給瀏覽器,瀏覽器在接下來的瀏覽中每一個發向伺服器的請求中都將包含該SessionID,從而標識了自己的身份。

伺服器上是使用記憶體來儲存Session中的資訊,那麼瀏覽器又使用什麼來儲存伺服器分配的這個SessionID了對,是Cookie。在剛建立 會話時瀏覽器向伺服器的請求中將不包含SessionID在Cookie中,伺服器就認為是一個全新的會話,從而在伺服器上分配一段記憶體給該 Session用,同時將該Session的ID在Http Header中使用Set-Cookie發送給瀏覽器。

現在原理已經搞清楚了,那麼我們就來實現一個網站的登入嘛。下面以某某大學的管理資訊系統來進行檢驗(注意:這裡的缺陷就在於沒有驗證碼的識別和多個伺服器的跳轉)難度相對來說要小很多。

 首先先用httpAnaly或者是httpwatch等專用的抓包工具,來擷取網頁提交時候的資料資訊和頭資訊。以下程式碼封裝含了登陸和在登陸後擷取另一個頁面資料資訊。
 1        private void Form1_Load(object sender, EventArgs e)
 2        {
 3
 4            string username = "xxxx";//使用者名稱
 5            string password = "xxxx";//密碼
 6           //建立一個用於儲存cookies的容器    
 7            CookieContainer container = new CookieContainer();
 8           //拼接post資料
 9            string postData = ("username=" + username);
10            postData += ("&passwd=" + password);
11            postData += ("&login=%B5%C7%A1%A1%C2%BC");
12            ASCIIEncoding encoding = new ASCIIEncoding();
13            byte[] data = encoding.GetBytes(postData);
14            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://xxxx/xxxx/login.asp");
15            request.Method = "Post";
16            request.ContentType = "application/x-www-form-urlencoded";
17            request.ContentLength = data.Length;
18            request.KeepAlive = true;
19            request.CookieContainer = container;  //返回的cookie會附加在這個容器裡面
20            //發送資料
21            Stream newStream = request.GetRequestStream();
22            newStream.Write(data, 0, data.Length);
23            newStream.Close();
24            //以下倆句不可缺少
25            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
26            response.Cookies = container.GetCookies(request.RequestUri);
27
28            HttpWebRequest requestScore = (HttpWebRequest)WebRequest.Create("http://xxxx/xxxx/Score.asp");
29            postData = "term=&TermList=%C7%EB%D1%A1%D4%F1&ckind=&lwPageSize=100&lwBtnquery=%B2%E9%D1%AF";
30            data = encoding.GetBytes(postData);
31            requestScore.Method = "Post";
32            requestScore.ContentType = "application/x-www-form-urlencoded";
33            requestScore.ContentLength = data.Length;
34            requestScore.KeepAlive = true;
35
36            //使用登陸的cookies通過接下來的驗證
37            requestScore.CookieContainer = container;
38            Stream stream = requestScore.GetRequestStream();
39            stream.Write(data, 0, data.Length);
40            stream.Close();
41            HttpWebResponse responseSorce = (HttpWebResponse)requestScore.GetResponse();
42            StreamReader reader = new StreamReader(responseSorce.GetResponseStream(), Encoding.Default);
43            string content = reader.ReadToEnd();
44            textBox1.Text = content;
45
46        }

    
摘自紅色駭客聯盟(www.7747.net) 原文:http://www.7747.net/kf/201007/52330.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.