/// <summary> /// Establishes connection with MAL, attempts to authenticate. /// </summary> /// <param name="updateToken"> /// Indicates whether created http client is meant to be used further or do we want to dispose it and return null. /// </param> /// <exception cref="WebException"> /// Unable to authorize. /// </exception> /// <returns> /// Returns valid http client which can interact with website API. /// </returns> public static async Task <CsrfHttpClient> GetHttpContextAsync() { try { if (_contextExpirationTime == null || DateTime.Now.CompareTo(_contextExpirationTime.Value) > 0) { _httpClient?.ExpiredDispose(); var httpHandler = new HttpClientHandler { CookieContainer = new CookieContainer(), UseCookies = true, AllowAutoRedirect = false, }; //Utilities.GiveStatusBarFeedback("Performing http authentication..."); _httpClient = new CsrfHttpClient(httpHandler) { BaseAddress = new Uri(MalBaseUrl) }; await _httpClient.GetToken(); //gets token and sets cookies var loginPostInfo = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("user_name", Credentials.UserName), new KeyValuePair <string, string>("password", Credentials.Password), new KeyValuePair <string, string>("sublogin", "Login"), new KeyValuePair <string, string>("cookie", "1"), new KeyValuePair <string, string>("submit", "1"), new KeyValuePair <string, string>("csrf_token", _httpClient.Token) }; var content = new FormUrlEncodedContent(loginPostInfo); //we won't dispose it here because this instance gonna be passed further down to other queries var response = await _httpClient.PostAsync("/login.php", content); if (response.IsSuccessStatusCode || response.StatusCode == HttpStatusCode.Found || response.StatusCode == HttpStatusCode.RedirectMethod) { _contextExpirationTime = DateTime.Now.Add(TimeSpan.FromHours(.5)); return(_httpClient); //else we are returning client that can be used for next queries } throw new WebException("Unable to authorize"); } return(_httpClient); } catch (Exception) { ResourceLocator.MessageDialogProvider.ShowMessageDialog( "Unable to connect to MyAnimeList, they have either changed something in html or your connection is down.", "Something went wrong™"); return(new CsrfHttpClient(new HttpClientHandler()) { Disabled = true }); } }
/// <summary> /// Establishes connection with MAL, attempts to authenticate. /// </summary> /// <param name="updateToken"> /// Indicates whether created http client is meant to be used further or do we want to dispose it and return null. /// </param> /// <exception cref="WebException"> /// Unable to authorize. /// </exception> /// <returns> /// Returns valid http client which can interact with website API. /// </returns> public static async Task<CsrfHttpClient> GetHttpContextAsync() { try { if (_contextExpirationTime == null || DateTime.Now.CompareTo(_contextExpirationTime.Value) > 0) { _httpClient?.ExpiredDispose(); var httpHandler = new HttpClientHandler { CookieContainer = new CookieContainer(), UseCookies = true, AllowAutoRedirect = false, }; //Utilities.GiveStatusBarFeedback("Performing http authentication..."); _httpClient = new CsrfHttpClient(httpHandler) { BaseAddress = new Uri(MalBaseUrl) }; await _httpClient.GetToken(); //gets token and sets cookies var loginPostInfo = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("user_name", Credentials.UserName), new KeyValuePair<string, string>("password", Credentials.Password), new KeyValuePair<string, string>("sublogin", "Login"), new KeyValuePair<string, string>("cookie", "1"), new KeyValuePair<string, string>("submit", "1"), new KeyValuePair<string, string>("csrf_token", _httpClient.Token) }; var content = new FormUrlEncodedContent(loginPostInfo); //we won't dispose it here because this instance gonna be passed further down to other queries var response = await _httpClient.PostAsync("/login.php", content); if (response.IsSuccessStatusCode || response.StatusCode == HttpStatusCode.Found || response.StatusCode == HttpStatusCode.RedirectMethod) { _contextExpirationTime = DateTime.Now.Add(TimeSpan.FromHours(.5)); return _httpClient; //else we are returning client that can be used for next queries } throw new WebException("Unable to authorize"); } return _httpClient; } catch (Exception) { ResourceLocator.MessageDialogProvider.ShowMessageDialog( "Unable to connect to MyAnimeList, they have either changed something in html or your connection is down.", "Something went wrong™"); return new CsrfHttpClient(new HttpClientHandler()) {Disabled = true}; } }