本方法通过将账号和密码提交到登录页面,返回一个CookieContainer类型的COOKIE容器,需要模拟登录访问的时候带着这个CookieContainer访问指定地址便可。
1 #region Discuz论坛登录
2
3 /// <summary>
4
5 /// Discuz论坛登录
6
7 /// </summary>
8
9 /// <param name="UserName">账号</param>
10
11 /// <param name="PassWord">密码</param>
12
13 /// <param name="domain">论坛地址(末尾加斜线),如 http://www.njxsw.com/</param>
14
15 /// <returns></returns>
16
17 protected CookieContainer DiscuzLogin(string UserName, string PassWord, string domain)
18
19 { #region 取得登录表单页源代码
20
21 string loginPageUrl = domain + "member.php?mod=logging&action=login&infloat=yes&handlekey=login&inajax=1&ajaxtarget=fwin_content_login";
22
23 string loginPageHtml = ForSunSoft.Net.Url.Post(new NameValueCollection(),
24
25 loginPageUrl,
26
27 System.Text.Encoding.GetEncoding("GBK"),
28
29 new CookieContainer(),
30
31 "*/*",
32
33 domain + "forum.php",
34
35 "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2)");
36
37 #endregion #region 获取验证页面地址
38
39 Match match_UserValidateUrl = new Regex(";\" action=\"(?<key>.*?)\">", RegexOptions.None).Match(loginPageHtml);
40
41 string UserValidateUrl = match_UserValidateUrl.Groups["key"].Value.ToS();
42
43 #endregion #region 获取表单哈希码
44
45 Match match_FormHash = new Regex("<input type=\"hidden\" name=\"formhash\" value=\"(?<key>.*?)\" />", RegexOptions.None).Match(loginPageHtml);
46
47 string FormHash = match_FormHash.Groups["key"].Value.ToS();
48
49 #endregion #region 创建登录表单
50
51 NameValueCollection loginForm = new NameValueCollection();
52
53 loginForm.Add("formhash", FormHash);
54
55 loginForm.Add("referer", domain + "forum.php");
56
57 loginForm.Add("loginfield", "username");
58
59 loginForm.Add("username", UserName);
60
61 loginForm.Add("password", PassWord);
62
63 loginForm.Add("questionid", "0");
64
65 loginForm.Add("answer", "");
66
67 loginForm.Add("cookietime", "2592000");
68
69 #endregion #region 获得Auth和Sid
70
71 CookieCollection userCookie = new CookieCollection(); CookieCollection ResCookie = ForSunSoft.Net.Url.PostAndGetCookie(loginForm, domain + UserValidateUrl.Replace("&", "&"), System.Text.Encoding.GetEncoding("GBK"));
72
73 foreach (Cookie c in ResCookie)
74
75 {
76
77 if (c.Name == "8TcA_2132_auth" || c.Name == "8TcA_2132_sid")
78
79 {
80
81 userCookie.Add(c);
82
83 }
84
85 }
86
87 #endregion
88
89 CookieCollection cookieResult = ForSunSoft.Net.Url.PostAndGetCookie(new NameValueCollection(), domain + "forum.php", System.Text.Encoding.GetEncoding("GBK"), userCookie.ToCookieContainer());
90
91 foreach (Cookie c in userCookie)
92
93 {
94
95 cookieResult.Add(c);
96
97 } return cookieResult.ToCookieContainer(); }
98
99 #endregion
100
2
3 /// <summary>
4
5 /// Discuz论坛登录
6
7 /// </summary>
8
9 /// <param name="UserName">账号</param>
10
11 /// <param name="PassWord">密码</param>
12
13 /// <param name="domain">论坛地址(末尾加斜线),如 http://www.njxsw.com/</param>
14
15 /// <returns></returns>
16
17 protected CookieContainer DiscuzLogin(string UserName, string PassWord, string domain)
18
19 { #region 取得登录表单页源代码
20
21 string loginPageUrl = domain + "member.php?mod=logging&action=login&infloat=yes&handlekey=login&inajax=1&ajaxtarget=fwin_content_login";
22
23 string loginPageHtml = ForSunSoft.Net.Url.Post(new NameValueCollection(),
24
25 loginPageUrl,
26
27 System.Text.Encoding.GetEncoding("GBK"),
28
29 new CookieContainer(),
30
31 "*/*",
32
33 domain + "forum.php",
34
35 "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2)");
36
37 #endregion #region 获取验证页面地址
38
39 Match match_UserValidateUrl = new Regex(";\" action=\"(?<key>.*?)\">", RegexOptions.None).Match(loginPageHtml);
40
41 string UserValidateUrl = match_UserValidateUrl.Groups["key"].Value.ToS();
42
43 #endregion #region 获取表单哈希码
44
45 Match match_FormHash = new Regex("<input type=\"hidden\" name=\"formhash\" value=\"(?<key>.*?)\" />", RegexOptions.None).Match(loginPageHtml);
46
47 string FormHash = match_FormHash.Groups["key"].Value.ToS();
48
49 #endregion #region 创建登录表单
50
51 NameValueCollection loginForm = new NameValueCollection();
52
53 loginForm.Add("formhash", FormHash);
54
55 loginForm.Add("referer", domain + "forum.php");
56
57 loginForm.Add("loginfield", "username");
58
59 loginForm.Add("username", UserName);
60
61 loginForm.Add("password", PassWord);
62
63 loginForm.Add("questionid", "0");
64
65 loginForm.Add("answer", "");
66
67 loginForm.Add("cookietime", "2592000");
68
69 #endregion #region 获得Auth和Sid
70
71 CookieCollection userCookie = new CookieCollection(); CookieCollection ResCookie = ForSunSoft.Net.Url.PostAndGetCookie(loginForm, domain + UserValidateUrl.Replace("&", "&"), System.Text.Encoding.GetEncoding("GBK"));
72
73 foreach (Cookie c in ResCookie)
74
75 {
76
77 if (c.Name == "8TcA_2132_auth" || c.Name == "8TcA_2132_sid")
78
79 {
80
81 userCookie.Add(c);
82
83 }
84
85 }
86
87 #endregion
88
89 CookieCollection cookieResult = ForSunSoft.Net.Url.PostAndGetCookie(new NameValueCollection(), domain + "forum.php", System.Text.Encoding.GetEncoding("GBK"), userCookie.ToCookieContainer());
90
91 foreach (Cookie c in userCookie)
92
93 {
94
95 cookieResult.Add(c);
96
97 } return cookieResult.ToCookieContainer(); }
98
99 #endregion
100
代码中有部分自定义方法,需要的留下邮箱。