public static async Task <string> DownloadStringAsync(Uri uri, int timeOut = 60000, WebProxy proxy = null) { string output = null; bool cancelledOrError = false; using (var client = new BetterWebClient()) { if (proxy != null) { client.Proxy = proxy; } client.DownloadStringCompleted += (sender, e) => { if (e.Error != null || e.Cancelled) { cancelledOrError = true; } else { output = e.Result; } }; client.DownloadStringAsync(uri); var n = DateTime.Now; while (output == null && !cancelledOrError && DateTime.Now.Subtract(n).TotalMilliseconds < timeOut) { await Task.Delay(100); // wait for respsonse } } return(output); }
public static async Task GetCurrencyRate() { try { using (var wClient = new BetterWebClient { Timeout = 30000 }) { string sourcecode = await wClient.DownloadStringAwareOfEncoding( new Uri("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"));; Match gpdregex = Regex.Match(sourcecode, "<Cube currency='GBP' rate='(.*)'/>"); if (gpdregex.Success) { _gbpRate = gpdregex.Groups[1].Value; } } _lastCheck = DateTime.Now; } catch (Exception) { // ignored } }