代码改变世界

使用C#的HttpWebRequest模拟登陆网站(续)

2011-12-20 18:25  hoho_luo  阅读(4447)  评论(4编辑  收藏  举报

上一篇文章中我们讲了,如何采用程序模拟登录网站,并获取登录后网站的内容,今天在此基础上继续将,通过程序登录了网站后而直接进入登录后的页面。

首先还是发起一个启用一个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写入本地,并打开登录后的页面

View Code
 1   [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
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");

这样即可直接通过程序打开登录后的页面: