private void AuthenticateClient(HttpRequestBuilder requestBuilder, UTorrentSettings settings, bool reauthenticate = false) { var authKey = string.Format("{0}:{1}", requestBuilder.BaseUrl, settings.Password); var cookies = _authCookieCache.Find(authKey); var authToken = _authTokenCache.Find(authKey); if (cookies == null || authToken == null || reauthenticate) { _authCookieCache.Remove(authKey); _authTokenCache.Remove(authKey); var authLoginRequest = BuildRequest(settings).Resource("/gui/token.html").Build(); HttpResponse response; try { response = _httpClient.Execute(authLoginRequest); _logger.Debug("uTorrent authentication succeeded."); var xmlDoc = new System.Xml.XmlDocument(); xmlDoc.LoadXml(response.Content); authToken = xmlDoc.FirstChild.FirstChild.InnerText; } catch (HttpException ex) { if (ex.Response.StatusCode == HttpStatusCode.Unauthorized) { _logger.Debug("uTorrent authentication failed."); throw new DownloadClientAuthenticationException("Failed to authenticate with uTorrent."); } throw new DownloadClientException("Unable to connect to uTorrent, please check your settings", ex); } catch (WebException ex) { throw new DownloadClientException("Unable to connect to uTorrent, please check your settings", ex); } cookies = response.GetCookies(); _authCookieCache.Set(authKey, cookies); _authTokenCache.Set(authKey, authToken); } requestBuilder.SetCookies(cookies); requestBuilder.AddPrefixQueryParam("token", authToken, true); }