private void UpdateAccount(OnlineAccount account, bool logMe = false) { AccountPart.DataContext = null; Processing.IsEnabled = false; if (account == null) { AccountPart.Visibility = Visibility.Collapsed; LoginSignupPart.Visibility = Visibility.Visible; UserNameComboBox.ItemsSource = App.Settings.StoredUser; } else { AccountPart.Visibility = Visibility.Visible; LoginSignupPart.Visibility = Visibility.Collapsed; AccountPart.DataContext = App.LoginedAccount; if (logMe) { App.StoreUser(account.UserName); } LoginSuccessed?.Invoke(this, EventArgs.Empty); } }
private void brower_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { WebBrowser brower = sender as WebBrowser; string cookie = brower.Document.Cookie; //模拟form表单提交 if (e.Url.ToString().Trim().Contains("login/ajax")) { string html = brower.DocumentText; Match match = Regex.Match(html, "_csrf_token\" value=\"([a-z0-9]+)\""); if (match.Success) { string csrf_token = match.Groups[1].Captures[0].Value; HtmlDocument doc = brower.Document; for (int i = 0; i < doc.All.Count; i++) { if (doc.All[i].TagName.ToUpper().Equals("INPUT")) { switch (doc.All[i].Name) { case "_username": doc.All[i].InnerText = username; break; case "_password": doc.All[i].InnerText = password; break; case "_csrf_token": doc.All[i].InnerText = csrf_token; break; } } } HtmlElement formLogin = brower.Document.Forms["login-ajax-form"]; formLogin.InvokeMember("submit"); } } else if (e.Url.ToString().Trim().Contains("goto")) { client = new WebClient(); client.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); client.Headers.Add("Accept-Language", "zh-cn"); client.Headers.Add("Cookie", cookie); //测试登录是否成功 try { if (!isLogin) { client.DownloadString(new Uri("http://www.unitytrain.cn/course/40/lesson/402")); isLogin = true; LoginSuccessed.Invoke(cookie, null); System.Windows.MessageBox.Show("登录成功!"); } } catch (Exception ex) { System.Windows.MessageBox.Show("登录失败!\n" + ex.Message); } } }