private async void SendAsync(string methodName, string url) { WebRequestMethod method = WebRequestUtility.GetMethod(methodName); var request = new WebRequest(method, url); await OnSendAsync(request); }
public static bool IsMessageSended(Message message, string token) { string url = new RequestUrlFactory(token, message.ChannelId, message.MessageText, message.UserId).SendMessage; string json = WebRequestUtility.GetContent(url); return(IsMessagesSendedFromJson(json)); }
public static ICollection <User> GetUsers(string token) { var url = new RequestUrlFactory(token); string json = WebRequestUtility.GetContent(url.UsersList); var users = GetUsersFromJson(json); return(users); }
public static ICollection <Channel> GetChannels(string token) { var url = new RequestUrlFactory(token); string json = WebRequestUtility.GetContent(url.ChannelsList); var auth = GetChannelsFromJson(json); return(auth); }
public WebConnector(string domain, string userName, string password = "******") { webRequestUtility = new WebRequestUtility(); this.userName = userName; this.domain = domain; this.password = password; transport = ConfigurationSettings.AppSettings["transport"]; TradingApiURL = transport + @"//" + domain + @"/" + "TradingApi"; TradingApiSessionURL = TradingApiURL + @"/session"; }
public static ICollection <Message> GetMessages(string token, string channelId, bool isChannelsMessages = false) { var factory = new RequestUrlFactory(token, channelId); string url = isChannelsMessages ? factory.ChannelHistory : factory.ImHistory; string json = WebRequestUtility.GetContent(url); var messages = GetMessagesInternal(json); return(messages); }
public List <string> FindWordsThatStartWith(string initialLetters, out int totalWordsFound) { totalWordsFound = 0; string content = WebRequestUtility.ReadHtmlPageFromUrl( $@"https://www.morewords.com/most-common-starting-with/{initialLetters}"); var wordsFound = ParseContentOldFormat(content, out totalWordsFound); if (wordsFound == null || totalWordsFound == 0) { wordsFound = ParseContentNewFormat(content, out totalWordsFound); } wordsFound?.Sort(); return(wordsFound); }
public static AuthResponse GetAuthResponse(string token) { if (token == null) { throw new ArgumentNullException(nameof(token)); } if (string.IsNullOrWhiteSpace(token)) { throw new ArgumentException(nameof(token)); } var url = new RequestUrlFactory(token); string json = WebRequestUtility.GetContent(url.Auth); var auth = GetAuthResponseFromJson(json); return(auth); }
public static ICollection <Im> GetIms(string token) { if (token == null) { throw new ArgumentNullException(nameof(token)); } if (string.IsNullOrWhiteSpace(token)) { throw new ArgumentException(nameof(token)); } var url = new RequestUrlFactory(token); string json = WebRequestUtility.GetContent(url.ImsList); var ims = GetImsFromJson(json); return(ims); }
private void btnUpload_Click(object sender, EventArgs e) { if (File.Exists(txtLuaPath.Text)) { WebRequestUtility remoteUploadUtility = new WebRequestUtility("DKPHistory Client", 10000); try { if (Uri.TryCreate(txtRemoteUploadUrl.Text, UriKind.Absolute, out Uri uri)) { if (!uri.Host.Equals("example.com", StringComparison.OrdinalIgnoreCase)) { byte[] fileContent = File.ReadAllBytes(txtLuaPath.Text); string authString = GenerateAuthString(); BpWebResponse response = remoteUploadUtility.POST(uri.OriginalString, fileContent, "application/octet-stream", new string[] { "X-Dkp-Auth", authString }); if (response.StatusCode == 200 && response.str.StartsWith("1")) { // Upload complete MessageBox.Show("Upload Complete"); } else { MessageBox.Show("Unable to upload lua file to remote server. Response status: " + response.StatusCode + ", Response body: " + response.str); } } } else { MessageBox.Show("Unable to parse remote upload URL. Check configuration."); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } else { MessageBox.Show("Lua file not found."); } }
protected virtual UnityWebRequest OnCreateWebRequest(IWebRequest request) { string method = WebRequestUtility.GetMethodName(request.Method); var unityWebRequest = new UnityWebRequest(request.Url, method) { redirectLimit = Description.RedirectLimit, timeout = Description.Timeout, useHttpContinue = Description.UseHttpContinue }; foreach ((string key, string value) in request.Headers) { unityWebRequest.SetRequestHeader(key, value); } OnCreateUploadHandler(request, unityWebRequest); OnCreateDownloadHandler(request, unityWebRequest); return(unityWebRequest); }
public void DownloadPuzFiles_NoAssertions() { int year = 18; // up to 20 for (int month = 1; month < 13; month++) { for (int day = 1; day < 32; day++) { var url = $"http://herbach.dnsalias.com/uc/uc{year:00}{month:00}{day:00}.puz"; try { string content = WebRequestUtility.ReadHtmlPageFromUrl(url); Console.WriteLine($"{content.Length} : {url}"); } catch (WebException exception) { Console.WriteLine($"Exception: {url} {exception.Message}"); } } } }
protected virtual HttpRequestMessage OnCreateRequestMessage(IWebRequest request) { string methodName = WebRequestUtility.GetMethodName(request.Method); var method = new HttpMethod(methodName); var message = new HttpRequestMessage(method, request.Url); foreach ((string key, string value) in request.Headers) { message.Headers.TryAddWithoutValidation(key, value); } if (request.TryGetData(out byte[] bytes)) { message.Content = new ByteArrayContent(bytes); foreach ((string key, string value) in request.Headers) { message.Content.Headers.TryAddWithoutValidation(key, value); } } return(message); }
protected virtual async Task <IWebRequest> OnCreateRequestAsync(HttpListenerContext context) { HttpListenerRequest listenerRequest = context.Request; WebRequestMethod method = WebRequestUtility.GetMethod(listenerRequest.HttpMethod); var request = new WebRequest(method, listenerRequest.RawUrl); foreach (string key in listenerRequest.Headers.AllKeys) { string value = listenerRequest.Headers.Get(key); request.Headers.Add(key, value); } if (listenerRequest.HasEntityBody) { await using var memoryStream = new MemoryStream(); await listenerRequest.InputStream.CopyToAsync(memoryStream); byte[] bytes = memoryStream.ToArray(); if (bytes.Length > 0) { request.SetData(bytes); } } Log.Debug("Received web request", new { request.Method, request.Url, request.HasData }); return(request); }
private void SyncWithOtherServer() { try { WebRequestUtility wru = new WebRequestUtility("MonitorControl " + Globals.AssemblyVersion, 1000); string lastStatus = ""; int connectionFailureCount = 0; while (true) { try { if (!Program.settings.syncAllowLocalOverride) { lastStatus = MonitorControlServer.currentMonitorStatus; } string address = Program.settings.syncAddress; if (!string.IsNullOrWhiteSpace(address)) { string url = "http" + (Program.settings.syncHTTPS ? "s" : "") + "://" + address + ":" + Program.settings.syncPort + "/status"; BpWebResponse response = wru.GET(url); if (response.StatusCode == 0) { connectionFailureCount++; syncedServerStatus = "The remote server is not responding. It may be misconfigured, blocked by a firewall, or not running."; if (connectionFailureCount >= 5) // Don't take action due to a short-term failure. { if (Program.settings.syncFailureAction == 1) { if (lastStatus != "off") { lastStatus = "off"; MonitorControlServer.Off(Program.settings.syncMute); } } else if (Program.settings.syncFailureAction == 2) { if (lastStatus != "on") { lastStatus = "on"; MonitorControlServer.On(null); } } } } else { connectionFailureCount = 0; if (response.StatusCode == 403) { syncedServerStatus = "The remote server rejected our request (this server's IP is probably not authorized there)."; } else if (response.StatusCode != 200) { syncedServerStatus = "The remote server responded with unexpected status code " + response.StatusCode; try { syncedServerStatus += " " + response.str; } catch { } } else { try { syncedServerStatus = "The remote server monitors are " + response.str; if (response.str != lastStatus) { if (response.str == "off") { lastStatus = "off"; MonitorControlServer.Off(Program.settings.syncMute); } else if (response.str == "on") { lastStatus = "on"; MonitorControlServer.On(null); } } } catch { syncedServerStatus = "The remote server provided an invalid response."; } } } } else { connectionFailureCount = 0; syncedServerStatus = "Not configured to synchronize with a remote server."; } Thread.Sleep(1000); } catch (ThreadAbortException) { } catch (Exception ex) { syncedServerStatus = "An error occurred when synchronizing with a remote server. " + ex.ToString(); Logger.Debug(ex); } } } catch (ThreadAbortException) { } catch (Exception ex) { syncedServerStatus = "Remote server sync has experienced a fatal error and will not resume operation until MonitorControl is restarted. " + ex.ToString(); Logger.Debug(ex); } }
private void fileManagement() { try { string localLuaPath = Globals.WritableDirectoryBase + "CommunityDKP.lua"; long minutesWaited = 0; DateTime lastFileWrite = DateTime.MinValue; long lastLocalBackupMinutes = -99999999; long lastRemoteUploadMinutes = -99999999; bool needsLocalBackup = false; bool needsRemoteUpload = false; string previousHash = null; bool firstRun = true; WebRequestUtility remoteUploadUtility = new WebRequestUtility("DKPHistory Client", 10000); while (firstRun || !ewhThreadWaiter.WaitOne(60000)) { firstRun = false; minutesWaited++; string hash = null; bool fileChanged = false; if (string.IsNullOrWhiteSpace(settings.luaFilePath)) { continue; } // Make local copy of file try { lock (LuaFileLock) { FileInfo fiSrc = new FileInfo(settings.luaFilePath); if (!fiSrc.Exists) { Logger.Info("Could not find CommunityDKP.lua. Check configuration."); continue; } if (lastFileWrite != fiSrc.LastWriteTimeUtc) { lastFileWrite = fiSrc.LastWriteTimeUtc; File.Copy(fiSrc.FullName, localLuaPath, true); // Read file history = History.FromLuaFile(localLuaPath); // Calculate Hash Code hash = Hash.GetMD5HexOfFile(localLuaPath); fileChanged = previousHash != hash; previousHash = hash; needsLocalBackup = needsLocalBackup || fileChanged; needsRemoteUpload = needsRemoteUpload || fileChanged; } } } catch (Exception ex) { Logger.Debug(ex, "Error doing local file management."); continue; } // Make local backup of lua file if (needsLocalBackup && !string.IsNullOrWhiteSpace(settings.localBackupPath) && settings.localBackupIntervalMinutes > 0 && minutesWaited - lastLocalBackupMinutes >= settings.localBackupIntervalMinutes) { lastLocalBackupMinutes = minutesWaited; needsLocalBackup = false; try { if (!Directory.Exists(settings.localBackupPath)) { Directory.CreateDirectory(settings.localBackupPath); } if (Directory.Exists(settings.localBackupPath)) { DateTime fileTime = File.GetLastWriteTime(localLuaPath); string bkFileName = Path.Combine(settings.localBackupPath, "CommunityDKP-" + fileTime.ToString("yyyy-MM-dd hh-mm-ss") + ".lua"); if (!File.Exists(bkFileName)) { File.Copy(localLuaPath, bkFileName, true); } // Backup complete } else { Logger.Info("Could not find or create local backup path. Check configuration."); } } catch (Exception ex) { Logger.Debug(ex, "Error doing local backup."); } } // Perform remote upload of lua file if (needsRemoteUpload && !string.IsNullOrWhiteSpace(settings.remoteUploadUrl) && settings.remoteUploadIntervalMinutes > 0 && minutesWaited - lastRemoteUploadMinutes >= settings.remoteUploadIntervalMinutes) { needsRemoteUpload = false; lastRemoteUploadMinutes = minutesWaited; try { if (Uri.TryCreate(settings.remoteUploadUrl, UriKind.Absolute, out Uri uri)) { if (!uri.Host.Equals("example.com", StringComparison.OrdinalIgnoreCase)) { byte[] fileContent = File.ReadAllBytes(localLuaPath); string authString = GenerateAuthString(); BpWebResponse response = remoteUploadUtility.POST(settings.remoteUploadUrl, fileContent, "application/octet-stream", new string[] { "X-Dkp-Auth", authString }); if (response.StatusCode == 200 && response.str.StartsWith("1")) { // Upload complete } else { Logger.Info("Unable to upload lua file to remote server. Response status: " + response.StatusCode + ", Response body: " + response.str); } } } else { Logger.Info("Unable to parse remote upload URL. Check configuration."); } } catch (Exception ex) { Logger.Debug(ex, "Error doing remote upload."); } } } } catch (Exception ex) { Logger.Debug(ex, "File management thread suffered a critical failure!"); } }
protected override void threadLoop() { try { while (true) { try { WebRequestUtility wru = new WebRequestUtility("BandwidthMonitor"); wru.BasicAuthCredentials = new NetworkCredential(User, Pass); // Learn the http_id, used in later requests BpWebResponse response = wru.GET(Address + "ipt-realtime.asp"); string ipt_realtime_page = response.str; Match m_http_id = Regex.Match(ipt_realtime_page, "'http_id' ?: ?'([^']+)',"); string http_id = null; if (m_http_id.Success) { http_id = m_http_id.Groups[1].Value; } else { Logger.Debug("Could not find http_id in response from ipt-realtime.asp"); Thread.Sleep(30000); continue; } // Begin retrieving bandwidth usage data. Stopwatch sw = new Stopwatch(); // Tomato's bandwidth reporting uses cumulative totals, so we need to keep track of the previous records to know how much has changed. Dictionary <string, BandwidthRecord> previousRecords = new Dictionary <string, BandwidthRecord>(); Stopwatch swDevList = new Stopwatch(); response = wru.POST(Address + "update.cgi", new string[] { "exec", "devlist", "_http_id", http_id }); if (string.IsNullOrWhiteSpace(response.str)) { Logger.Info("Received null or whitespace response instead of expected devlist"); Thread.Sleep(30000); continue; } HandleDevListResponse(response.str); swDevList.Start(); while (true) { sw.Restart(); long time = TimeUtil.GetTimeInMsSinceEpoch(); response = wru.POST(Address + "update.cgi", new string[] { "exec", "iptmon", "_http_id", http_id }); foreach (Match m in rxGetIpData.GetMatches(response.str)) { string ip = m.Groups[1].Value; long downloadRaw = Hex.PrefixedHexToLong(m.Groups[2].Value); long uploadRaw = Hex.PrefixedHexToLong(m.Groups[3].Value); DeviceInfo device; BandwidthRecord prev; if (!devices.TryGetValue(ip, out device)) { devices[ip] = device = new DeviceInfo(ip); } if (!previousRecords.TryGetValue(ip, out prev)) { previousRecords[ip] = prev = new BandwidthRecord(time, downloadRaw, uploadRaw); } device.Name = GetName(device.Address); device.MAC = GetMac(device.Address); device.Vendor = GetMacVendor(device.MAC); long downloadBytes; long uploadBytes; if (downloadRaw < prev.Download) { downloadBytes = downloadRaw + (0xFFFFFFFF - prev.Download); } else { downloadBytes = downloadRaw - prev.Download; } if (uploadRaw < prev.Upload) { uploadBytes = uploadRaw + (0xFFFFFFFF - prev.Upload); } else { uploadBytes = uploadRaw - prev.Upload; } device.AddNewBandwidthRecord(new BandwidthRecord(time, downloadBytes / 2, uploadBytes / 2)); previousRecords[ip] = new BandwidthRecord(time, downloadRaw, uploadRaw); } if (swDevList.Elapsed > timeBetweenDevListUpdates) { swDevList.Restart(); response = wru.POST(Address + "update.cgi", new string[] { "exec", "devlist", "_http_id", http_id }); HandleDevListResponse(response.str); } sw.Stop(); TimeSpan timeToWait = timeBetweenBandwidthUpdates - sw.Elapsed; if (timeToWait > TimeSpan.Zero) { Thread.Sleep(timeToWait); } } } catch (ThreadAbortException) { throw; } catch (Exception ex) { Logger.Debug(ex); Thread.Sleep(5000); } } } catch (ThreadAbortException) { throw; } catch (Exception ex) { Logger.Debug(ex); } }
/// <summary> /// Performs HTTP GET request with credentials, returning the body as text /// </summary> /// <param name="url">Url</param> /// <param name="queryParams">Query params</param> /// <param name="extractBody">Extract body handler</param> /// <param name="firstRequestTime">First request time</param> /// <param name="retryCounter">Retry counter</param> /// <param name="baseUrl">Base url</param> /// <param name="acceptsClientId">Accepts client Id</param> /// <param name="requestConfigOverride">Request config override</param> /// <param name="useAuthedUrl">Use authenticated url</param> /// <param name="urlSuffix">Url suffix to append</param> /// <returns>Response body</returns> public dynamic Get(string url, QueryParams queryParams, Func <HttpWebResponse, dynamic> extractBody = null, DateTime?firstRequestTime = null, int retryCounter = 0, string baseUrl = Globals.DefaultBaseUrl, bool acceptsClientId = true, RequestConfig requestConfigOverride = null, bool useAuthedUrl = true, string urlSuffix = null) { // Adjust first request time if (!firstRequestTime.HasValue) { firstRequestTime = DateTime.Now; } // Adjust elapsed time var elapsed = DateTime.Now - firstRequestTime; if (elapsed > new TimeSpan(0, 0, ClientConfig.RetryTimeout)) { throw new TimeoutException(); } // Retry counter if (retryCounter > 0) { // 0.5 * (1.5 ^ i) is an increased sleep time of 1.5x per iteration, // starting at 0.5s when retry_counter=0. The first retry will occur // at 1, so subtract that first. var delay_seconds = 0.5 * Math.Pow(1.5, (retryCounter - 1)); // Jitter this value by 50% and pause. Thread.Sleep((int)(delay_seconds * ((new Random().Next(1)) + 0.5))); } // Get authenticated url var authedUrl = useAuthedUrl ? GenerateAuthUrl(url, queryParams, acceptsClientId) : $"{url}?{UrlEncodeParams(queryParams)}"; // Get response HttpWebResponse resp; try { resp = WebRequestUtility.Get(baseUrl + authedUrl + urlSuffix, requestConfigOverride ?? ClientConfig.RequestConfig); } catch (Exception ex) { throw new TransportErrorException(ex); } // Check if response status code is a retriable one if (Globals.RetriableStatuses.Contains((int)resp.StatusCode)) { // Retry request. return(Get(url, queryParams, extractBody, firstRequestTime, retryCounter + 1, baseUrl, acceptsClientId)); } // If response status code is not OK at this stage, throw an exception if (resp.StatusCode != HttpStatusCode.OK) { // Throw a Http response exception throw new HttpResponseException(resp); } // Check if the time of the nth previous query (where n is queries_per_second) // is under a second ago - if so, sleep for the difference. // TODO: Fix that, seems it won't always work if (SentTimes.Count == ClientConfig.QueriesPerSecond) { var elapsedSinceEarliest = DateTime.Now - SentTimes.ElementAt(0); if (elapsedSinceEarliest.Seconds < 1) { Thread.Sleep(1000 - elapsedSinceEarliest.Milliseconds); } } // Error handling try { // Extract body var result = extractBody != null?extractBody(resp) : GetBody(resp); // Add sent time SentTimes.Add(DateTime.Now); // Return result return(result); } catch (RetriableRequestException) { // Retry request. return(Get(url, queryParams, extractBody, firstRequestTime, retryCounter + 1, baseUrl, acceptsClientId)); } }