public override string Login() { string postData = string.Format("username={0}&password={1}", User.UserName, User.PassWord); var result = HttpUtils.PostData(Site.LoginUrl, postData, _cookie); //这里没必要判断是否启用两步验证了,因为返回的结果Url就能知道是不是两步验证。 User.isEnableTwo_StepVerification if (Site.isEnableTwo_StepVerification && result.Item3 != null && result.Item3.ResponseUri.OriginalString.Contains("verify")) { OnTwoStepVerificationEventArgs e = new OnTwoStepVerificationEventArgs(); e.Site = Site; string code = OnTwoStepVerification(e); if (code.IsNullOrEmptyOrWhiteSpace()) { return("登录失败,无法获取正确的二级验证码。"); } else { postData = string.Format("otp={0}", code); _cookie = result.Item2.CookieContainer; result = HttpUtils.PostData(result.Item3.ResponseUri.OriginalString, postData, _cookie); return(DoLogin(result)); } } else { return(DoLogin(result)); } }
/// <summary> /// 触发两步验证事件 /// </summary> /// <param name="e"></param> /// <returns></returns> public virtual string OnTwoStepVerification(OnTwoStepVerificationEventArgs e) { if (TwoStepVerification != null) { return(TwoStepVerification.Invoke(this, e)); } return(string.Empty); }
private string Pt_TwoStepVerification(object sender, OnTwoStepVerificationEventArgs e) { var pt = sender as AbstractPT; TwoStepVerificationFrm frm = new TwoStepVerificationFrm(e); if (frm.ShowDialog() == DialogResult.OK) { return(frm.TwoStepVerificationKey); } return(string.Empty); }
public TwoStepVerificationFrm(OnTwoStepVerificationEventArgs e) { El = e; InitializeComponent(); if (El != null) { if (El.Site != null) { this.Text = El.Site.Name + "二级验证"; } } }
protected override Tuple <string, HttpWebRequest, HttpWebResponse> DoLoginPostWithOutCookie(Tuple <string, HttpWebRequest, HttpWebResponse> cookieResult) { //如果前面Cookie登录没有成功,则下面尝试没有Cookie的情况。 string otpCode = string.Empty; StringBuilder sb = new StringBuilder(); Dictionary <string, string> postDict = new Dictionary <string, string>(); postDict.Add("username", User.UserName); postDict.Add("password", User.PassWord); postDict.Add("otp", ""); //启用了二级验证 if (Site.isEnableTwo_StepVerification && User.isEnableTwo_StepVerification) { OnTwoStepVerificationEventArgs e = new OnTwoStepVerificationEventArgs(); e.Site = Site; otpCode = OnTwoStepVerification(e); postDict["otp"] = otpCode; } //启用了安全提问 if (Site.IsEableSecurityQuestion) { if (!(User.SecurityQuestionOrder == -1 || User.SecuityAnswer.IsNullOrEmptyOrWhiteSpace())) { postDict.Add("passan", User.SecuityAnswer); postDict.Add("passid", User.SecurityQuestionOrder.TryPareValue <string>()); } else { postDict.Add("passan", ""); postDict.Add("passid", "0"); } } postDict.Add("lang", "0"); postDict.Add("rememberme", "yes"); foreach (var item in postDict) { sb.AppendLine(string.Format("------{0}", YUConst.POST_BOUNDARY)); sb.AppendLine(string.Format("Content-Disposition: form-data; name=\"{0}\"", item.Key)); sb.AppendLine(""); sb.AppendLine(item.Value); } sb.AppendLine(string.Format("{0}--", YUConst.POST_BOUNDARY)); string postData = sb.ToString(); return(HttpUtils.PostData(Site.LoginUrl, postData, _cookie, true)); }
private string DoLogin(int RecursiveNum = 0, bool isForceVerification = false) { RecursiveNum++; StringBuilder sb = new StringBuilder(); Dictionary <string, string> postDict = new Dictionary <string, string>(); postDict.Add("username", User.UserName); postDict.Add("password", User.PassWord); postDict.Add("otp", ""); //如果Cookie存在,则先不要求提交二级验证码 if (isForceVerification || (_cookie == null || _cookie.Count <= 0)) { //如果启用了二级验证 if (Site.isEnableTwo_StepVerification && User.isEnableTwo_StepVerification) { OnTwoStepVerificationEventArgs e = new OnTwoStepVerificationEventArgs(); e.Site = Site; string code = OnTwoStepVerification(e); postDict["otp"] = code; } } //TTG启用了安全提问 if (Site.IsEableSecurityQuestion) { if (!(User.SecurityQuestionOrder == -1 || User.SecuityAnswer.IsNullOrEmptyOrWhiteSpace())) { postDict.Add("passan", User.SecuityAnswer); postDict.Add("passid", User.SecurityQuestionOrder.TryPareValue <string>()); } else { postDict.Add("passan", ""); postDict.Add("passid", "0"); } } postDict.Add("lang", "0"); postDict.Add("rememberme", "yes"); foreach (var item in postDict) { sb.AppendLine(string.Format("------{0}", YUConst.POST_BOUNDARY)); sb.AppendLine(string.Format("Content-Disposition: form-data; name=\"{0}\"", item.Key)); sb.AppendLine(""); sb.AppendLine(item.Value); } sb.AppendLine(string.Format("{0}--", YUConst.POST_BOUNDARY)); string postData = sb.ToString(); var result = HttpUtils.PostData(Site.LoginUrl, postData, _cookie, true); string htmlResult = result.Item1; if (htmlResult.Contains(User.UserName)) { User.Id = GetUserId(htmlResult); _cookie = result.Item2.CookieContainer; SetLocalCookie(_cookie); return("登录成功。"); } else { if (RecursiveNum > 2) { HtmlDocument htmlDocument = new HtmlDocument(); htmlDocument.LoadHtml(htmlResult); //加载HTML字符串,如果是文件可以用htmlDocument.Load方法加载 HtmlNode node = htmlDocument.DocumentNode.SelectSingleNode("//*[@id=\"main_table\"]/tr[1]/td/table/tr/td/table/tr/td"); //跟Xpath一样 string errMsg = htmlResult; if (node != null) { errMsg = node.InnerText; } return(string.Format("登录失败,失败原因:{0}", errMsg)); } else { return(DoLogin(RecursiveNum, true)); } } }
private Tuple <string, HttpWebRequest, HttpWebResponse> DoLoginPostWithOutCookie(Tuple <string, HttpWebRequest, HttpWebResponse> cookieResult, bool isRetry = true) { //如果前面Cookie登录没有成功,则下面尝试没有Cookie的情况。 string postData = "username={0}&password={1}&oneCode={2}&imagestring={3}&imagehash={4}"; if (new Uri(Site.LoginUrl).Scheme == "https") { postData += string.Format("&ssl=yes&trackerssl=yes"); } string checkCodeKey = string.Empty; string checkCodeHash = string.Empty; string otpCode = string.Empty; if (Site.IsEnableVerificationCode) { string htmlResult = string.Empty; //这里先看有没有前面是不是有过请求了,如果有的话,那么直接在这里获取验证码,如果没有,则自己获取。 if (cookieResult != null && !cookieResult.Item1.IsNullOrEmptyOrWhiteSpace()) { htmlResult = cookieResult.Item1; } else { htmlResult = HttpUtils.GetData(Site.Url, _cookie).Item1; } HtmlDocument htmlDocument = new HtmlDocument(); htmlDocument.LoadHtml(htmlResult); HtmlNode node = htmlDocument.DocumentNode.SelectSingleNode(".//table//tr/td/img"); if (node != null) { string imgUrl = HttpUtility.HtmlDecode(node.Attributes["src"].Value); if (imgUrl.IsNullOrEmptyOrWhiteSpace()) { return(new Tuple <string, HttpWebRequest, HttpWebResponse>("无法获取到验证码,登录失败,请稍后重试。", null, null)); } imgUrl = UrlUtils.CombileUrl(Site.Url, imgUrl); checkCodeKey = GetVerificationCode(imgUrl, isRetry); checkCodeHash = imgUrl.UrlSearchKey("imagehash"); if (checkCodeKey.IsNullOrEmptyOrWhiteSpace() || checkCodeHash.IsNullOrEmptyOrWhiteSpace()) { return(new Tuple <string, HttpWebRequest, HttpWebResponse>("无法获取到验证码,登录失败,请稍后重试。", null, null)); } } else { return(new Tuple <string, HttpWebRequest, HttpWebResponse>("无法获取到验证码,登录失败,请稍后重试。", null, null)); } } if (Site.isEnableTwo_StepVerification && User.isEnableTwo_StepVerification) { OnTwoStepVerificationEventArgs e = new OnTwoStepVerificationEventArgs(); e.Site = Site; otpCode = OnTwoStepVerification(e); } postData = string.Format(postData, User.UserName, User.PassWord, otpCode, checkCodeKey, checkCodeHash); var result = HttpUtils.PostData(Site.LoginUrl, postData, _cookie); if (HttpUtils.IsErrorRequest(result.Item1)) { return(result); } //如果登录失败且不是二次尝试的,则重新登录。 if (!isRetry && !IsLoginSuccess(result.Item3)) { Logger.Info(string.Format("{0} 登录没有成功,识别到的验证码为{1}。", Site.Name, checkCodeKey)); return(DoLoginPostWithOutCookie(cookieResult, false)); } else { return(result); } }