private void OnLogOutPageLoaded(object sender, HtmlDocumentLoadCompleted args) { _web.LoadCompleted -= OnLogOutPageLoaded; MitbbsWebSessionEventArgs logOutCompleteArgs = new MitbbsWebSessionEventArgs(); logOutCompleteArgs.Success = false; if (args.Document != null) { IsLoggedIn = false; logOutCompleteArgs.Success = true; } IsConnecting = false; if (LogOutCompleted != null) { LogOutCompleted(this, logOutCompleteArgs); } }
private void OnLogInStartPageLoaded(object sender, HtmlDocumentLoadCompleted args) { _web.LoadCompleted -= OnLogInStartPageLoaded; if ((args.Document != null) && (_web.FormElements.ContainsKey("id"))) { _web.FormElements["id"] = _username; _web.FormElements["pwd"] = _password; _web.LoadCompleted += OnLogInPageLoaded; _web.LoadAsync(App.Settings.BuildUrl(_logInPageUrl), HtmlWeb.OpenMode.Post); } else { MitbbsWebSessionEventArgs logInCompleteArgs = new MitbbsWebSessionEventArgs(); logInCompleteArgs.Success = false; if ((args.Document != null)) { IEnumerable<HtmlNode> textNodes = args.Document.DocumentNode.Descendants("#text"); foreach (HtmlNode textNode in textNodes) { if (HtmlUtilities.GetPlainHtmlText(textNode.InnerText) == "您已经登录!") { // Already logged in // logInCompleteArgs.Success = true; IsLoggedIn = true; break; } } } IsConnecting = false; if (!IsLoggedIn) { if (_retries <= 0) { MessageBoxResult result = MessageBox.Show("需要再重试一次吗?", "[" + _username + "]用户登录失败", MessageBoxButton.OKCancel); if (result == MessageBoxResult.OK) { StartLogIn(_username, _password); return; } } else { StartLogIn(_username, _password, _retries - 1); return; } } if (LogInCompleted != null) { LogInCompleted(this, logInCompleteArgs); } } }
public void StartLogOut() { if (IsConnecting) { MitbbsWebSessionEventArgs logOutCompleteArgs = new MitbbsWebSessionEventArgs(); logOutCompleteArgs.Success = false; if (LogOutCompleted != null) { LogOutCompleted(this, logOutCompleteArgs); } return; } IsConnecting = true; IsLoggedIn = false; _web.LoadCompleted += OnLogOutPageLoaded; _web.LoadAsync(App.Settings.BuildUrl(_logOutPageUrl)); }
private void OnLogInPageLoaded(object sender, HtmlDocumentLoadCompleted args) { _web.LoadCompleted -= OnLogInPageLoaded; MitbbsWebSessionEventArgs logInCompleteArgs = new MitbbsWebSessionEventArgs(); logInCompleteArgs.Success = false; if (args.Document != null) { IEnumerable<HtmlNode> titleNodes = args.Document.DocumentNode.Descendants("title"); foreach (HtmlNode titleNode in titleNodes) { String title = HtmlUtilities.GetPlainHtmlText(titleNode.InnerText); if (title.EndsWith("家页")) { logInCompleteArgs.Success = true; IsLoggedIn = true; break; } } } else { logInCompleteArgs.Error = args.Error; } IsConnecting = false; if (!IsLoggedIn) { if (_retries <= 0) { MessageBoxResult result = MessageBox.Show("需要再重试一次吗?", "[" + _username + "]用户登录失败", MessageBoxButton.OKCancel); if (result == MessageBoxResult.OK) { StartLogIn(_username, _password); return; } } else { StartLogIn(_username, _password, _retries - 1); return; } } if (LogInCompleted != null) { LogInCompleted(this, logInCompleteArgs); } }
public void StartLogIn(String username, String password, uint retries = 1) { bool createNewWebObj = !IsLoggedIn; if (IsConnecting) { MitbbsWebSessionEventArgs logInCompleteArgs = new MitbbsWebSessionEventArgs(); logInCompleteArgs.Success = false; if (LogInCompleted != null) { LogInCompleted(this, logInCompleteArgs); } return; } IsConnecting = true; if (IsLoggedIn) { IsLoggedIn = false; MitbbsWebSessionEventArgs logOutCompleteArgs = new MitbbsWebSessionEventArgs(); logOutCompleteArgs.Success = true; if (LogOutCompleted != null) { LogOutCompleted(this, logOutCompleteArgs); } } //_cookies = new CookieContainer(); if (username == null) { username = ""; } if (password == null) { password = ""; } _username = username; _password = password; IsLoggedIn = false; if (createNewWebObj) { if (_web != null) { _web.LoadCompleted -= OnLogInStartPageLoaded; _web.LoadCompleted -= OnLogInPageLoaded; _web.LoadCompleted -= OnLogOutPageLoaded; } _web = new HtmlWeb(); _web.Cookies = _cookies; _web.GenerateFormElements = true; _web.Encoding = _encoding; } _retries = retries; _web.LoadCompleted += OnLogInStartPageLoaded; _web.LoadAsync(App.Settings.BuildUrl(_logInStartPageUrl)); }