Пример #1
0
        public async Task <byte[]> GetAsync(Uri url)
        {
#if DEBUG
            var cached = TestingEnvCore.GetUrlBytes(url);
            if (cached != null)
            {
                return(cached);
            }
#endif

            try
            {
                using (var webClient = new WebClient())
                {
                    var result = await webClient.DownloadDataTaskAsync(url);

#if DEBUG
                    TestingEnvCore.StoreUrlBytes(url, null, result);
#endif
                    return(result);
                }
            }
            catch (Exception e)
            {
                _logger.Exception(e);
                return(null);
            }
        }
Пример #2
0
        public async Task <byte[]> PostAsync(Uri url, string parameters)
        {
#if DEBUG
            var cached = TestingEnvCore.GetUrlBytes(url, parameters);
            if (cached != null)
            {
                return(cached);
            }
#endif
            try
            {
                using (var webClient = new WebClient())
                {
                    webClient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
                    var data = Encoding.Default.GetBytes(parameters);

                    var result = await webClient.UploadDataTaskAsync(url, data);

#if DEBUG
                    TestingEnvCore.StoreUrlBytes(url, parameters, result);
#endif

                    return(result);
                }
            }
            catch (Exception e)
            {
                _logger.Exception(e);
                return(null);
            }
        }
Пример #3
0
        public async Task <string> GetStringAsync(Uri url, CancellationToken cancellationToken, Dictionary <HttpRequestHeader, string> headers = null)
        {
#if DEBUG
            var cached = TestingEnvCore.GetUrlString(url);
            if (cached != null)
            {
                return(cached);
            }
#endif

            try
            {
                using (var webClient = new WebClient())
                    using (cancellationToken.Register(() => webClient.CancelAsync()))
                    {
                        AppendHeaders(webClient, headers);
                        webClient.Encoding = Encoding.UTF8;
                        var result = await webClient.DownloadStringTaskAsync(url);

#if DEBUG
                        TestingEnvCore.StoreUrlString(url, result);
#endif
                        return(result);
                    }
            }
            catch (WebException e) when(e.Status == WebExceptionStatus.RequestCanceled)
            {
                // Ignore this exception
                return(null);
            }
            catch (Exception e)
            {
                _logger.Exception(e);
                return(null);
            }
        }