private string DownloadString(ConnectionInfoModel connectionInfo, string url) { try { var request = WebRequest.Create(url); if (connectionInfo.HasCredentials) { request.Credentials = CreateBasicAuthCredential(connectionInfo, url); request.PreAuthenticate = true; } request.Timeout = 2000; var response = request.GetResponse(); var reader = new StreamReader(response.GetResponseStream()); return reader.ReadToEnd(); } catch (Exception ex) { Logger.Trace("Failed to get response from: {0}", url); Logger.Trace(ex.Message, ex); } return String.Empty; }
private CredentialCache CreateBasicAuthCredential(ConnectionInfoModel connectionInfo, string url) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; CredentialCache credentialCache = new CredentialCache(); credentialCache.Add(new System.Uri(url), "Basic", new NetworkCredential(connectionInfo.Credentials.Username, connectionInfo.Credentials.Password)); return credentialCache; }
private string GetApiKey(ConnectionInfoModel connectionInfo) { var request = String.Format("http://{0}:{1}/config/general/", connectionInfo.Address, connectionInfo.Port); var result = DownloadString(connectionInfo, request); Regex regex = new Regex("\\<input\\Wtype\\=\\\"text\\\"\\Wid\\=\\\"apikey\\\"\\Wvalue\\=\\\"(?<apikey>\\w+)\\W", RegexOptions.IgnoreCase | RegexOptions.Compiled); var match = regex.Match(result); if (match.Success) { return match.Groups["apikey"].Value; } return String.Empty; }