/// <summary> /// Attempts to download a string from a remote URL. Throws WebException if anything goes awry/the request times out/task is cancelled. /// </summary> /// <param name="Url">The URL to download the string from.</param> /// <param name="TimeOut">After this specified amount of time in seconds, the request will timeout.</param> /// <param name="cancellationToken">The cancellation token to use for the download task.</param> /// <returns>The downloaded string, or null if it failed to download.</returns> public static async Task <string> DownloadStringTimeoutAsync(string Url, int TimeOut, CancellationToken cancellationToken) { string downloadResult = null; WebClientWithTimeout webClient = new WebClientWithTimeout(TimeOut); webClient.Headers["user-agent"] = "WebUtils Parsing"; using (webClient) { using (var registration = cancellationToken.Register(() => webClient.CancelAsync())) { downloadResult = await webClient.DownloadStringTaskAsync(Url); } } return(downloadResult); }