使用C#的HttpWebRequest模拟登陆网站(续)
生活随笔
收集整理的這篇文章主要介紹了
使用C#的HttpWebRequest模拟登陆网站(续)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
上一篇文章中我們講了,如何采用程序模擬登錄網站,并獲取登錄后網站的內容,今天在此基礎上繼續將,通過程序登錄了網站后而直接進入登錄后的頁面。
首先還是發起一個啟用一個web會話,然后發送模擬數據請求,獲取會話的CooKie,再根據該CooKie將其寫入到本地,通過程序直接打開登錄后的頁面。
該功能可用于無法修改第三方系統源代碼而要做系統單點登錄。
?
我們先在HTMLHelper類中添加一個方法:
View Code 1 /// <summary>2 /// 獲取CookieCollection
3 /// </summary>
4 /// <param name="loginUrl"></param>
5 /// <param name="postdata"></param>
6 /// <param name="header"></param>
7 /// <returns></returns>
8 public static CookieCollection GetCookieCollection(string loginUrl, string postdata, HttpHeader header)
9 {
10 HttpWebRequest request = null;
11 HttpWebResponse response = null;
12 try
13 {
14 CookieContainer cc = new CookieContainer();
15 request = (HttpWebRequest)WebRequest.Create(loginUrl);
16 request.Method = header.method;
17 request.ContentType = header.contentType;
18 byte[] postdatabyte = Encoding.UTF8.GetBytes(postdata);
19 request.ContentLength = postdatabyte.Length;
20 request.AllowAutoRedirect = false;
21 request.CookieContainer = cc;
22 request.KeepAlive = true;
23
24 //提交請求
25 Stream stream;
26 stream = request.GetRequestStream();
27 stream.Write(postdatabyte, 0, postdatabyte.Length);
28 stream.Close();
29
30 //接收響應
31 response = (HttpWebResponse)request.GetResponse();
32 response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
33
34 CookieCollection cook = response.Cookies;
35 //Cookie字符串格式
36 string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);
37
38 return cook;
39 }
40 catch (Exception ex)
41 {
42
43 throw ex;
44 }
45 }
再根據獲取的CookieCollection寫入本地,并打開登錄后的頁面
2
3 public static extern bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData);
4
5
6 HttpHeader header = new HttpHeader();
7 header.accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight-2-b1, */*";
8 header.contentType = "application/x-www-form-urlencoded";
9 header.method = "POST";
10 header.userAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
11 header.maxTry = 300;
12
13
14 CookieCollection mycookie = HTMLHelper.GetCookieCollection("http://www.renren.com/PLogin.do",
15 "email=aaa%40163.com&password=111&icode=&origURL=http%3A%2F%2Fwww.renren.com%2Fhome&domain=renren.com&key_id=1&_rtk=90484476", header);
16
17
18 foreach (Cookie cookie in mycookie) //將cookie設置為瀏覽的cookie
19 {
20
21 InternetSetCookie(
22
23 "http://" + cookie.Domain.ToString(),
24
25 cookie.Name.ToString(),
26
27 cookie.Value.ToString() + ";expires=Sun,22-Feb-2099 00:00:00 GMT");
28
29 }
30 System.Diagnostics.Process.Start("http://guide.renren.com/guide");
這樣即可直接通過程序打開登錄后的頁面:
?
轉載于:https://www.cnblogs.com/hoholuo/archive/2011/12/20/2295014.html
總結
以上是生活随笔為你收集整理的使用C#的HttpWebRequest模拟登陆网站(续)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Log图文详解(Log.v,Log.d,
- 下一篇: 击中-击不中变换(约束)—lhMorpH