http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 31325|回复: 17

[源码分享] 模拟登陆DISCUZ X3型网站登陆源码范例

[复制链接]
发表于 2013-6-21 11:18:57 | 显示全部楼层 |阅读模式
论坛里关于QQ的登陆范例有了. 这里给大家来个discuz论坛登陆的例子. (模拟浏览器登陆模式)

QQ截图20130621111319.png
程序原理:
步骤1: 访问登陆界面
获取登陆所需的formhash等隐藏表单验证选项
步骤2: 模拟post数据进行登录
从winform中填写相关数据, 模拟发送


说明:
1.程序中未使用"登录问题的选择"(默认的是没有填)
2.本登陆代码来自本人的一个未完成项目中, 仅提供登陆代码(其中可能有的函数未完成说明, 如有需要请QQ群[sufeinet总群]联系[电工]). 程序中其他函数以作说明;

程序下载: UserLoginFrm.7z.zip (3.17 KB, 下载次数: 950)

本帖被以下淘专辑推荐:



1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
 楼主| 发表于 2013-6-21 11:19:52 | 显示全部楼层

  1.         #region ---------------------------登陆--------------------------------
  2.         /// <summary>
  3.         /// 设置用户登录状态
  4.         /// </summary>
  5.         /// <param name="status"></param>
  6.         private void loginStatus(bool status,string msg)
  7.         {
  8.             if (status)
  9.             {
  10.                 MessageBox.Show("登陆成功");
  11.                 cookies = msg;
  12.                 if (this.check_remember.Checked)
  13.                 {
  14.                     _com.setIniValue("AUTO", "USERNAME", getListOfName("username"));
  15.                     _com.setIniValue("AUTO", "USERPWD", getListOfName("password"));
  16.                 }
  17.                 else
  18.                 {
  19.                     _com.setIniValue("AUTO", "USERNAME", "");
  20.                     _com.setIniValue("AUTO", "USERPWD", "");
  21.                 }
  22.                 this.DialogResult = DialogResult.OK;
  23.             }
  24.             else
  25.             {
  26.                 MessageBox.Show("登陆失败:\n" + msg);
  27.             }
  28.         }
  29.         /// <summary>
  30.         /// 登陆
  31.         /// </summary>
  32.         private void login()
  33.         {
  34.             for (int i = 0; i < 3; i++)
  35.             {
  36.                 HttpHelper _http = new HttpHelper();
  37.                 HttpItem _item;
  38.                 HttpResult _result;
  39.                 try
  40.                 {
  41.                     _item = new HttpItem()
  42.                     {
  43.                         URL = _com.getIniValue("URL_CONFIG", "INDEX") + formAction + "&inajax=1",
  44.                         Postdata = getList(),
  45.                         Cookie = cookies,
  46.                         ContentType = "application/x-www-form-urlencoded",
  47.                         Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
  48.                         Method = "POST",
  49.                         Referer = _com.getFullUrl("URL_CONFIG", "LOGIN")
  50.                     };
  51.                     _item.Header.Add("Accept-Encoding", "gzip,deflate,sdch");
  52.                     _result = _http.GetHtml(_item);
  53.                     if (_result.StatusCode == System.Net.HttpStatusCode.OK)
  54.                     {
  55.                         if (_result.Html.IndexOf("欢迎") > -1)
  56.                         {
  57.                             this.BeginInvoke(deleLoginStatus, true, _result.Cookie);
  58.                         }
  59.                         else
  60.                         {
  61.                             Match m = Regex.Match(_result.Html, @"CDATA\[(.*?)<script");
  62.                             this.BeginInvoke(deleLoginStatus, false, m.Groups[1].Value.ToString());
  63.                         }
  64.                         break;
  65.                     }
  66.                 }
  67.                 catch (Exception ex) { this.BeginInvoke(deleLoginStatus, false, ex.ToString()); break; }
  68.                 finally { _http = null; _item = null; _result = null; }
  69.             }
  70.         }
  71.         /// <summary>
  72.         /// 获取List中的数据
  73.         /// </summary>
  74.         /// <returns></returns>
  75.         private string getList()
  76.         {
  77.             string s = "";
  78.             foreach (DiscuzLoginFormInput _in in formList)
  79.             {
  80.                 if (!String.IsNullOrEmpty(_in.Value))
  81.                 {
  82.                     //说明:Common.UrlEncode将值进行URL编码
  83.                     //      其中也就是把 : 替换成 %3A
  84.                     //                把 / 替换成 %2F  
  85.                     s += _in.Name + "=" + Common.UrlEncode(_in.Value) + "&";
  86.                 }
  87.                 else
  88.                 {
  89.                     s += _in.Name + "=" + _in.Value + "&";
  90.                 }
  91.             }
  92.             return s.Substring(0, s.Length - 1);
  93.         }
  94.         /// <summary>
  95.         /// 获取list中的数据
  96.         /// </summary>
  97.         /// <param name="name">Name值</param>
  98.         /// <returns></returns>
  99.         private string getListOfName(string name)
  100.         {
  101.             foreach (DiscuzLoginFormInput _in in formList)
  102.             {
  103.                 if (_in.Name == name)
  104.                 {
  105.                     return _in.Value;
  106.                 }
  107.             }
  108.             return String.Empty;
  109.         }
  110.         #endregion ---------------------------登陆--------------------------------

  111.         #region -------------默认访问登陆页----------------------------------------
  112.         /// <summary>
  113.         /// 加载登录页基本信息
  114.         /// </summary>
  115.         private void loadFromInput()
  116.         {
  117.             //尝试获取3次
  118.             for (int i = 0; i < 3; i++)
  119.             {
  120.                 HttpHelper _http = new HttpHelper();
  121.                 HttpItem _item;
  122.                 HttpResult _result;
  123.                 try
  124.                 {
  125.                     _item = new HttpItem()
  126.                     {
  127.                         URL = _com.getFullUrl("URL_CONFIG", "LOGIN"),
  128.                         Referer = _com.getIniValue("URL_CONFIG", "INDEX")
  129.                     };
  130.                     //获取登陆页源码
  131.                     _result = _http.GetHtml(_item);
  132.                     //判断是否获取成功
  133.                     if (_result.StatusCode == System.Net.HttpStatusCode.OK)
  134.                     {
  135.                         Regex r = new Regex(@"(?<=\<form[\s\S]*name=""login""[\s\S]*action=""(?<action>.*?)""\>)(?<con>[\s\S]*?)(?=\<\/form\>)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
  136.                         Match match = r.Match(_result.Html);
  137.                         string s2 = string.Empty;
  138.                         if (match.Groups.Count > 0)
  139.                         {
  140.                             formAction = match.Groups["action"].Value.Replace("&", "&");
  141.                             s2 = match.Groups["con"].Value;
  142.                            
  143.                             Regex r1 = new Regex(@"<input.*?>", RegexOptions.IgnoreCase);
  144.                             Regex r2 = new Regex(@"<select.*?>[\s\S]*?</select>", RegexOptions.IgnoreCase);
  145.                             match = r1.Match(s2);
  146.                             while(match.Success)
  147.                             {
  148.                                 this.BeginInvoke(deleParseHtml, match.ToString());
  149.                                 match = match.NextMatch();
  150.                             }
  151.                             match = r2.Match(s2);
  152.                             while (match.Success)
  153.                             {
  154.                                 this.BeginInvoke(deleParseHtml, match.ToString());
  155.                                 match = match.NextMatch();
  156.                             }
  157.                             //cookies存起,等会登陆用
  158.                             cookies = _result.Cookie;
  159.                             //设置登陆按钮为可见
  160.                             this.BeginInvoke(deleSetInitStatus, true, "登陆");
  161.                             break;
  162.                         }
  163.                     }
  164.                 }
  165.                 catch { }
  166.                 finally { _http = null; _item = null; _result = null; }
  167.             }
  168.         }
  169.         /// <summary>
  170.         /// 设置input的name和value值到formlist
  171.         /// </summary>
  172.         /// <param name="html">input标签HTML</param>
  173.         private void setList(DiscuzLoginFormInput _in)
  174.         {
  175.             if (_in == null) return;
  176.             bool isUse = false;
  177.             foreach (DiscuzLoginFormInput x in formList)
  178.             {
  179.                 if (x.Name == _in.Name)
  180.                 {
  181.                     x.Value = _in.Value;
  182.                     isUse = true;
  183.                 }
  184.             }
  185.             if (!isUse && _in.Name != "")
  186.             {
  187.                 formList.Add(_in);
  188.             }
  189.         }
  190.         /// <summary>
  191.         /// 解析INPUT标签,得到NAME和Value集合
  192.         /// </summary>
  193.         /// <param name="html"></param>
  194.         /// <returns></returns>
  195.         private void parseHtml(string html)
  196.         {
  197.             DiscuzLoginFormInput _in = null;
  198.             Match m;
  199.             if (html.Substring(1, 5) == "input")
  200.             {
  201.                 _in = new DiscuzLoginFormInput();
  202.                 if (html.IndexOf("name") > -1)
  203.                 {
  204.                     m = Regex.Match(html, @"name=""(.*?)""");
  205.                     _in.Name = m.Groups[1].Value.ToString();
  206.                 }
  207.                 if (html.IndexOf("value") > -1)
  208.                 {
  209.                     m = Regex.Match(html, @"value=""(.*?)""");
  210.                     _in.Value = m.Groups[1].Value.ToString();
  211.                 }
  212.             }
  213.             else if (html.Substring(1, 6) == "select")
  214.             {
  215.                 _in = new DiscuzLoginFormInput();
  216.                 if (html.IndexOf("name") > -1)
  217.                 {
  218.                     m = Regex.Match(html, @"name=""(.*?)""");
  219.                     _in.Name = m.Groups[1].Value.ToString();
  220.                 }
  221.                 if (html.IndexOf("option") > -1)
  222.                 {
  223.                     m = Regex.Match(html, @"<option value=""(.*?)"".*?</option>");
  224.                     _in.Value = m.Groups[1].Value.ToString();
  225.                 }
  226.             }
  227.             //压入LIST
  228.             setList(_in);
  229.         }
  230.         #endregion ---------------------模拟访问登陆页------------------------------

复制代码
发表于 2013-6-21 11:59:12 | 显示全部楼层
这个不错,收藏到Httphelper例子中了
发表于 2013-6-24 23:51:09 | 显示全部楼层
能处理验证码么?
发表于 2013-8-11 22:20:33 | 显示全部楼层
LZ代码都比较全,唯独缺少App和Common这两个东西,报错无法编译。
发表于 2013-11-15 17:34:06 | 显示全部楼层
强烈支持楼主ing……
发表于 2014-5-20 21:14:26 | 显示全部楼层
非常感谢你帮了我的大忙,真的太感谢你啦!
发表于 2014-5-27 11:32:30 | 显示全部楼层
现在没时间看,先留个记号,回头细细品味!
发表于 2014-9-12 07:04:59 | 显示全部楼层
强烈支持楼主ing……
发表于 2014-9-28 10:01:00 | 显示全部楼层
不错的代码,先下载下来以后慢慢研究
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-3-28 22:52

© 2014-2021

快速回复 返回顶部 返回列表