|
楼主 |
发表于 2013-6-21 11:19:52
|
显示全部楼层
- #region ---------------------------登陆--------------------------------
- /// <summary>
- /// 设置用户登录状态
- /// </summary>
- /// <param name="status"></param>
- private void loginStatus(bool status,string msg)
- {
- if (status)
- {
- MessageBox.Show("登陆成功");
- cookies = msg;
- if (this.check_remember.Checked)
- {
- _com.setIniValue("AUTO", "USERNAME", getListOfName("username"));
- _com.setIniValue("AUTO", "USERPWD", getListOfName("password"));
- }
- else
- {
- _com.setIniValue("AUTO", "USERNAME", "");
- _com.setIniValue("AUTO", "USERPWD", "");
- }
- this.DialogResult = DialogResult.OK;
- }
- else
- {
- MessageBox.Show("登陆失败:\n" + msg);
- }
- }
- /// <summary>
- /// 登陆
- /// </summary>
- private void login()
- {
- for (int i = 0; i < 3; i++)
- {
- HttpHelper _http = new HttpHelper();
- HttpItem _item;
- HttpResult _result;
- try
- {
- _item = new HttpItem()
- {
- URL = _com.getIniValue("URL_CONFIG", "INDEX") + formAction + "&inajax=1",
- Postdata = getList(),
- Cookie = cookies,
- ContentType = "application/x-www-form-urlencoded",
- Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
- Method = "POST",
- Referer = _com.getFullUrl("URL_CONFIG", "LOGIN")
- };
- _item.Header.Add("Accept-Encoding", "gzip,deflate,sdch");
- _result = _http.GetHtml(_item);
- if (_result.StatusCode == System.Net.HttpStatusCode.OK)
- {
- if (_result.Html.IndexOf("欢迎") > -1)
- {
- this.BeginInvoke(deleLoginStatus, true, _result.Cookie);
- }
- else
- {
- Match m = Regex.Match(_result.Html, @"CDATA\[(.*?)<script");
- this.BeginInvoke(deleLoginStatus, false, m.Groups[1].Value.ToString());
- }
- break;
- }
- }
- catch (Exception ex) { this.BeginInvoke(deleLoginStatus, false, ex.ToString()); break; }
- finally { _http = null; _item = null; _result = null; }
- }
- }
- /// <summary>
- /// 获取List中的数据
- /// </summary>
- /// <returns></returns>
- private string getList()
- {
- string s = "";
- foreach (DiscuzLoginFormInput _in in formList)
- {
- if (!String.IsNullOrEmpty(_in.Value))
- {
- //说明:Common.UrlEncode将值进行URL编码
- // 其中也就是把 : 替换成 %3A
- // 把 / 替换成 %2F
- s += _in.Name + "=" + Common.UrlEncode(_in.Value) + "&";
- }
- else
- {
- s += _in.Name + "=" + _in.Value + "&";
- }
- }
- return s.Substring(0, s.Length - 1);
- }
- /// <summary>
- /// 获取list中的数据
- /// </summary>
- /// <param name="name">Name值</param>
- /// <returns></returns>
- private string getListOfName(string name)
- {
- foreach (DiscuzLoginFormInput _in in formList)
- {
- if (_in.Name == name)
- {
- return _in.Value;
- }
- }
- return String.Empty;
- }
- #endregion ---------------------------登陆--------------------------------
- #region -------------默认访问登陆页----------------------------------------
- /// <summary>
- /// 加载登录页基本信息
- /// </summary>
- private void loadFromInput()
- {
- //尝试获取3次
- for (int i = 0; i < 3; i++)
- {
- HttpHelper _http = new HttpHelper();
- HttpItem _item;
- HttpResult _result;
- try
- {
- _item = new HttpItem()
- {
- URL = _com.getFullUrl("URL_CONFIG", "LOGIN"),
- Referer = _com.getIniValue("URL_CONFIG", "INDEX")
- };
- //获取登陆页源码
- _result = _http.GetHtml(_item);
- //判断是否获取成功
- if (_result.StatusCode == System.Net.HttpStatusCode.OK)
- {
- Regex r = new Regex(@"(?<=\<form[\s\S]*name=""login""[\s\S]*action=""(?<action>.*?)""\>)(?<con>[\s\S]*?)(?=\<\/form\>)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
- Match match = r.Match(_result.Html);
- string s2 = string.Empty;
- if (match.Groups.Count > 0)
- {
- formAction = match.Groups["action"].Value.Replace("&", "&");
- s2 = match.Groups["con"].Value;
-
- Regex r1 = new Regex(@"<input.*?>", RegexOptions.IgnoreCase);
- Regex r2 = new Regex(@"<select.*?>[\s\S]*?</select>", RegexOptions.IgnoreCase);
- match = r1.Match(s2);
- while(match.Success)
- {
- this.BeginInvoke(deleParseHtml, match.ToString());
- match = match.NextMatch();
- }
- match = r2.Match(s2);
- while (match.Success)
- {
- this.BeginInvoke(deleParseHtml, match.ToString());
- match = match.NextMatch();
- }
- //cookies存起,等会登陆用
- cookies = _result.Cookie;
- //设置登陆按钮为可见
- this.BeginInvoke(deleSetInitStatus, true, "登陆");
- break;
- }
- }
- }
- catch { }
- finally { _http = null; _item = null; _result = null; }
- }
- }
- /// <summary>
- /// 设置input的name和value值到formlist
- /// </summary>
- /// <param name="html">input标签HTML</param>
- private void setList(DiscuzLoginFormInput _in)
- {
- if (_in == null) return;
- bool isUse = false;
- foreach (DiscuzLoginFormInput x in formList)
- {
- if (x.Name == _in.Name)
- {
- x.Value = _in.Value;
- isUse = true;
- }
- }
- if (!isUse && _in.Name != "")
- {
- formList.Add(_in);
- }
- }
- /// <summary>
- /// 解析INPUT标签,得到NAME和Value集合
- /// </summary>
- /// <param name="html"></param>
- /// <returns></returns>
- private void parseHtml(string html)
- {
- DiscuzLoginFormInput _in = null;
- Match m;
- if (html.Substring(1, 5) == "input")
- {
- _in = new DiscuzLoginFormInput();
- if (html.IndexOf("name") > -1)
- {
- m = Regex.Match(html, @"name=""(.*?)""");
- _in.Name = m.Groups[1].Value.ToString();
- }
- if (html.IndexOf("value") > -1)
- {
- m = Regex.Match(html, @"value=""(.*?)""");
- _in.Value = m.Groups[1].Value.ToString();
- }
- }
- else if (html.Substring(1, 6) == "select")
- {
- _in = new DiscuzLoginFormInput();
- if (html.IndexOf("name") > -1)
- {
- m = Regex.Match(html, @"name=""(.*?)""");
- _in.Name = m.Groups[1].Value.ToString();
- }
- if (html.IndexOf("option") > -1)
- {
- m = Regex.Match(html, @"<option value=""(.*?)"".*?</option>");
- _in.Value = m.Groups[1].Value.ToString();
- }
- }
- //压入LIST
- setList(_in);
- }
- #endregion ---------------------模拟访问登陆页------------------------------
复制代码 |
|