コード例 #1
0
        async Task<IHttpTransferResult> _retryDownload(DownloadQueueObject obj, IHttpTransferConfig downloadConfig)
        {
            var succeed = false;

            IHttpTransferResult result = null;

            var retryCount = 0;

            do
            {
                result = await _doDownload(obj, downloadConfig);

                if (retryCount < downloadConfig.Retries &&
                    (result == null || result.DownloadException != null ||
                     (!result.IsSuccessCode && downloadConfig.RetryOnNonSuccessCode)))
                {
                    succeed = false;
                }
                else
                {
                    succeed = true;
                }

                retryCount++;

            } while (succeed == false);

            return result;
        }
コード例 #2
0
        async Task <IHttpTransferResult> _retryDownload(DownloadQueueObject obj, IHttpTransferConfig downloadConfig)
        {
            var succeed = false;

            IHttpTransferResult result = null;

            var retryCount = 0;

            do
            {
                result = await _doDownload(obj, downloadConfig);

                if (retryCount < downloadConfig.Retries &&
                    (result == null || result.DownloadException != null ||
                     (!result.IsSuccessCode && downloadConfig.RetryOnNonSuccessCode)))
                {
                    succeed = false;
                }
                else
                {
                    succeed = true;
                }

                retryCount++;
            } while (succeed == false);

            return(result);
        }
コード例 #3
0
        public override Task OnUnsuccessfulResult(HttpResponseMessage result, IHttpTransferConfig originalConfig)
        {
            if (result.StatusCode == HttpStatusCode.NotFound)
            {
                new NoNetworkErrorMessage().Send();
            }

            return base.OnUnsuccessfulResult(result, originalConfig);
        }
コード例 #4
0
 public async virtual Task OnUnsuccessfulResult(HttpResponseMessage result, IHttpTransferConfig originalConfig)
 {
     string resultText = null;
     if (result.Content != null)
     {
         resultText = await result.Content.ReadAsStringAsync();
     }
     Debug.WriteLine("Unsuccess: {0} - {1}", result.StatusCode, resultText ?? " no more info");
 }
コード例 #5
0
        public async virtual Task OnUnsuccessfulResult(HttpResponseMessage result, IHttpTransferConfig originalConfig)
        {
            string resultText = null;

            if (result.Content != null)
            {
                resultText = await result.Content.ReadAsStringAsync();
            }
            Debug.WriteLine("Unsuccess: {0} - {1}", result.StatusCode, resultText ?? " no more info");
        }
コード例 #6
0
        public async virtual Task<IHttpTransferResult> GetResult(HttpResponseMessage result, IHttpTransferConfig originalConfig)
        {
            try
            {
                var resultText = "";

                var isSuccess = true;

                if (result.Content != null)
                {
                    resultText = await result.Content.ReadAsStringAsync();
                }

                if (result.StatusCode == HttpStatusCode.Unauthorized)
                {
                    await OnUnauthorizedResult(result, originalConfig);
                    isSuccess = false;
                }

                if (!result.IsSuccessStatusCode)
                {
                    await OnUnsuccessfulResult(result, originalConfig);
                    isSuccess = false;
                }

                var headers = new Dictionary<string,List<string>>();
                
                if (result.Headers != null)
                {
                    foreach (var item in result.Headers)
                    {
                        headers.Add(item.Key, item.Value.ToList());
                    }
                }

                return new HttpTransferResult
                {
                    HttpStatusCode = result.StatusCode,
                    Result = resultText,
                    IsSuccessCode = isSuccess,
                    Headers = headers
                };
            }
            catch (Exception ex)
            {
                return GetExceptionResult(ex, "DownloadConfigService", originalConfig);
            }
        }
コード例 #7
0
 protected virtual void OnDownloadException(Exception ex, string source, IHttpTransferConfig originalConfig)
 {
 }
コード例 #8
0
 public async virtual Task OnUnauthorizedResult(HttpResponseMessage result, IHttpTransferConfig originalConfig)
 {
 }
コード例 #9
0
        async Task <IHttpTransferResult> _doDownload(DownloadQueueObject obj, IHttpTransferConfig downloadConfig)
        {
            // add support for Gzip decompression
            var native = new NativeMessageHandler();

            var httpClient = new HttpClient(native);

            using (httpClient)
            {
                var method = HttpMethod.Get;

                switch (obj.Verb)
                {
                case "GET":
                    method = HttpMethod.Get;
                    break;

                case "POST":
                    method = HttpMethod.Post;
                    break;

                case "PUT":
                    method = HttpMethod.Put;
                    break;

                case "DELETE":
                    method = HttpMethod.Delete;
                    break;
                }

                using (var message = new HttpRequestMessage(method, obj.Url))
                {
                    native.RegisterForProgress(message, (bytes, totalBytes, expected) => new TransferProgressMessage(obj.Url, bytes, totalBytes, expected, downloadConfig.Verb.ToLower() != "get").Send());

                    if (downloadConfig.Headers != null)
                    {
                        foreach (var item in downloadConfig.Headers)
                        {
                            message.Headers.Add(item.Key, item.Value);
                        }
                    }

                    // Accept-Encoding:
                    if (downloadConfig.AcceptEncoding != null)
                    {
                        //message.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue(""));
                        message.Headers.Add("Accept-Encoding", downloadConfig.AcceptEncoding);
                    }


                    // Accept:
                    if (!string.IsNullOrWhiteSpace(downloadConfig.Accept))
                    {
                        message.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(downloadConfig.Accept));
                    }


                    if (!string.IsNullOrWhiteSpace(obj.Data))
                    {
                        var content = new StringContent(obj.Data, Encoding.UTF8,
                                                        downloadConfig.ContentEncoding ?? "application/json");
                        message.Content = content;
                    }

                    if (obj.ByteData != null)
                    {
                        var content = new ByteArrayContent(obj.ByteData, 0, obj.ByteData.Length);

                        message.Content = content;
                    }

                    if (downloadConfig.Auth != null && downloadConfig.AuthScheme != null)
                    {
                        message.Headers.Authorization = new AuthenticationHeaderValue(downloadConfig.AuthScheme,
                                                                                      downloadConfig.Auth);
                    }

                    try
                    {
                        Debug.WriteLine("{0}: {1}", downloadConfig.Verb.ToLower() == "get" ? "Downloading" : "Uploading", obj.Url);

                        using (var result = await httpClient.SendAsync(message))
                        {
                            Debug.WriteLine("Finished: {0}", obj.Url);
                            return(await _httpTransferService.GetResult(result, downloadConfig));
                        }
                    }
                    catch (HttpRequestException ex)
                    {
                        Debug.WriteLine("Warning - HttpRequestException encountered: {0}", ex.Message);

                        return(_httpTransferService.GetExceptionResult(ex,
                                                                       "XamlingCore.Portable.Net.Downloaders.HttpClientDownloader", downloadConfig));
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("Warning - general HTTP exception encountered: {0}", ex.Message);
                        return(_httpTransferService.GetExceptionResult(ex,
                                                                       "XamlingCore.Portable.Net.Downloaders.HttpClientDownloader", downloadConfig));
                    }
                }
            }
        }
コード例 #10
0
        protected virtual void OnDownloadException(Exception ex, string source, IHttpTransferConfig originalConfig)
        {

        }
コード例 #11
0
        async Task<IHttpTransferResult> _doDownload(DownloadQueueObject obj, IHttpTransferConfig downloadConfig)
        {
            // add support for Gzip decompression

            HttpClient httpClient;
            var httpHandler = new HttpClientHandler();
            
            if (downloadConfig.Gzip)
            {

                if (httpHandler.SupportsAutomaticDecompression)
                {
                    httpHandler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
                }
            }

            if (downloadConfig.AllowRedirect && httpHandler.SupportsAllowAutoRedirect())
            {
                httpHandler.AllowAutoRedirect = true;
            }
            else
            {
                httpHandler.AllowAutoRedirect = false;
            }

            httpClient = new HttpClient(httpHandler);

            if (downloadConfig.Cookies != null)
            {
                var uri = new Uri(obj.Url);
                var cookies = new CookieContainer();
                httpHandler.CookieContainer = cookies;
                
                foreach (var c in downloadConfig.Cookies)
                {
                    cookies.Add(uri, new Cookie(c.Key, c.Value));
                }
            }

            using (httpClient)
            {
                var method = HttpMethod.Get;

                switch (obj.Verb)
                {
                    case "GET":
                        method = HttpMethod.Get;
                        break;
                    case "POST":
                        method = HttpMethod.Post;
                        break;
                    case "PUT":
                        method = HttpMethod.Put;
                        break;
                    case "DELETE":
                        method = HttpMethod.Delete;
                        break;
                }

                using (var message = new HttpRequestMessage(method, obj.Url))
                {
                    if (downloadConfig.Headers != null)
                    {
                        foreach (var item in downloadConfig.Headers)
                        {
                            message.Headers.Add(item.Key, item.Value);
                        }
                    }

                    // Accept-Encoding:
                    if (downloadConfig.AcceptEncoding != null)
                    {
                        //message.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue(""));
                        message.Headers.Add("Accept-Encoding", downloadConfig.AcceptEncoding);
                    }


                    // Accept:
                    if (!string.IsNullOrWhiteSpace(downloadConfig.Accept))
                    {
                        message.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(downloadConfig.Accept));
                    }


                    if (!string.IsNullOrWhiteSpace(obj.Data))
                    {
                        var content = new StringContent(obj.Data, Encoding.UTF8,
                            downloadConfig.ContentEncoding ?? "application/json");
                        message.Content = content;
                    }

                    if (obj.ByteData != null)
                    {
                        var content = new ByteArrayContent(obj.ByteData, 0, obj.ByteData.Length);
                        
                        message.Content = content;
                    }

                    if (downloadConfig.Auth != null && downloadConfig.AuthScheme != null)
                    {
                        message.Headers.Authorization = new AuthenticationHeaderValue(downloadConfig.AuthScheme,
                            downloadConfig.Auth);
                    }

                    if (downloadConfig.Timeout != 0)
                    {
                        httpClient.Timeout = TimeSpan.FromSeconds(downloadConfig.Timeout);
                    }

                    try
                    {
                        Debug.WriteLine("{0}: {1}", downloadConfig.Verb.ToLower() == "get" ? "Downloading": "Uploading", obj.Url);

                        using (var result = await httpClient.SendAsync(message))
                        {
                            Debug.WriteLine("Finished: {0}", obj.Url);
                            return await _httpTransferService.GetResult(result, downloadConfig);
                        }

                       

                    }
                    catch (HttpRequestException ex)
                    {
                        Debug.WriteLine("Warning - HttpRequestException encountered: {0}", ex.Message);

                        return _httpTransferService.GetExceptionResult(ex,
                            "XamlingCore.Portable.Net.Downloaders.HttpClientDownloader", downloadConfig);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("Warning - general HTTP exception encountered: {0}", ex.Message);
                        return _httpTransferService.GetExceptionResult(ex,
                            "XamlingCore.Portable.Net.Downloaders.HttpClientDownloader", downloadConfig);
                    }
                }
            }


        }
コード例 #12
0
        async Task<IHttpTransferResult> _doDownload(DownloadQueueObject obj, IHttpTransferConfig downloadConfig)
        {
            // add support for Gzip decompression
            var native = new NativeMessageHandler();
            
            var httpClient = new HttpClient(native);

            using (httpClient)
            {
                var method = HttpMethod.Get;

                switch (obj.Verb)
                {
                    case "GET":
                        method = HttpMethod.Get;
                        break;
                    case "POST":
                        method = HttpMethod.Post;
                        break;
                    case "PUT":
                        method = HttpMethod.Put;
                        break;
                    case "DELETE":
                        method = HttpMethod.Delete;
                        break;
                }

                using (var message = new HttpRequestMessage(method, obj.Url))
                {

                    native.RegisterForProgress(message, (bytes, totalBytes, expected) => new TransferProgressMessage(obj.Url, bytes, totalBytes, expected, downloadConfig.Verb.ToLower() != "get").Send());

                    if (downloadConfig.Headers != null)
                    {
                        foreach (var item in downloadConfig.Headers)
                        {
                            message.Headers.Add(item.Key, item.Value);
                        }
                    }

                    // Accept-Encoding:
                    if (downloadConfig.AcceptEncoding != null)
                    {
                        //message.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue(""));
                        message.Headers.Add("Accept-Encoding", downloadConfig.AcceptEncoding);
                    }


                    // Accept:
                    if (!string.IsNullOrWhiteSpace(downloadConfig.Accept))
                    {
                        message.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(downloadConfig.Accept));
                    }


                    if (!string.IsNullOrWhiteSpace(obj.Data))
                    {
                        var content = new StringContent(obj.Data, Encoding.UTF8,
                            downloadConfig.ContentEncoding ?? "application/json");
                        message.Content = content;
                    }

                    if (obj.ByteData != null)
                    {
                        var content = new ByteArrayContent(obj.ByteData, 0, obj.ByteData.Length);

                        message.Content = content;
                    }

                    if (downloadConfig.Auth != null && downloadConfig.AuthScheme != null)
                    {
                        message.Headers.Authorization = new AuthenticationHeaderValue(downloadConfig.AuthScheme,
                            downloadConfig.Auth);
                    }

                    try
                    {
                        Debug.WriteLine("{0}: {1}", downloadConfig.Verb.ToLower() == "get" ? "Downloading" : "Uploading", obj.Url);

                        using (var result = await httpClient.SendAsync(message))
                        {
                            Debug.WriteLine("Finished: {0}", obj.Url);
                            return await _httpTransferService.GetResult(result, downloadConfig);
                        }



                    }
                    catch (HttpRequestException ex)
                    {
                        Debug.WriteLine("Warning - HttpRequestException encountered: {0}", ex.Message);

                        return _httpTransferService.GetExceptionResult(ex,
                            "XamlingCore.Portable.Net.Downloaders.HttpClientDownloader", downloadConfig);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("Warning - general HTTP exception encountered: {0}", ex.Message);
                        return _httpTransferService.GetExceptionResult(ex,
                            "XamlingCore.Portable.Net.Downloaders.HttpClientDownloader", downloadConfig);
                    }
                }
            }


        }
コード例 #13
0
 public virtual IHttpTransferResult GetExceptionResult(Exception ex, string source, IHttpTransferConfig originalConfig)
 {
     OnDownloadException(ex, source, originalConfig);
     return(new HttpTransferResult {
         DownloadException = ex, Result = null, IsSuccessCode = false
     });
 }
コード例 #14
0
 protected override void OnDownloadException(Exception ex, string source, IHttpTransferConfig originalConfig)
 {
     new NoNetworkErrorMessage().Send();
     base.OnDownloadException(ex, source, originalConfig);
 }
コード例 #15
0
 public override Task OnUnauthorizedResult(HttpResponseMessage result,
     IHttpTransferConfig originalConfig)
 {
     new TokenExpiredMessage().Send();
     return base.OnUnauthorizedResult(result, originalConfig);
 }
コード例 #16
0
        async Task <IHttpTransferResult> _doDownload(DownloadQueueObject obj, IHttpTransferConfig downloadConfig)
        {
            // add support for Gzip decompression

            HttpClient httpClient;
            var        httpHandler = new HttpClientHandler();

            if (downloadConfig.Gzip)
            {
                if (httpHandler.SupportsAutomaticDecompression)
                {
                    httpHandler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
                }
            }

            if (downloadConfig.AllowRedirect && httpHandler.SupportsAllowAutoRedirect())
            {
                httpHandler.AllowAutoRedirect = true;
            }
            else
            {
                httpHandler.AllowAutoRedirect = false;
            }

            httpClient = new HttpClient(httpHandler);

            if (downloadConfig.Cookies != null)
            {
                var uri     = new Uri(obj.Url);
                var cookies = new CookieContainer();
                httpHandler.CookieContainer = cookies;

                foreach (var c in downloadConfig.Cookies)
                {
                    cookies.Add(uri, new Cookie(c.Key, c.Value));
                }
            }

            using (httpClient)
            {
                var method = HttpMethod.Get;

                switch (obj.Verb)
                {
                case "GET":
                    method = HttpMethod.Get;
                    break;

                case "POST":
                    method = HttpMethod.Post;
                    break;

                case "PUT":
                    method = HttpMethod.Put;
                    break;

                case "DELETE":
                    method = HttpMethod.Delete;
                    break;
                }

                using (var message = new HttpRequestMessage(method, obj.Url))
                {
                    if (downloadConfig.Headers != null)
                    {
                        foreach (var item in downloadConfig.Headers)
                        {
                            message.Headers.Add(item.Key, item.Value);
                        }
                    }

                    // Accept-Encoding:
                    if (downloadConfig.AcceptEncoding != null)
                    {
                        //message.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue(""));
                        message.Headers.Add("Accept-Encoding", downloadConfig.AcceptEncoding);
                    }


                    // Accept:
                    if (!string.IsNullOrWhiteSpace(downloadConfig.Accept))
                    {
                        message.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(downloadConfig.Accept));
                    }


                    if (!string.IsNullOrWhiteSpace(obj.Data))
                    {
                        var content = new StringContent(obj.Data, Encoding.UTF8,
                                                        downloadConfig.ContentEncoding ?? "application/json");
                        message.Content = content;
                    }

                    if (obj.ByteData != null)
                    {
                        var content = new ByteArrayContent(obj.ByteData, 0, obj.ByteData.Length);

                        message.Content = content;
                    }

                    if (downloadConfig.Auth != null && downloadConfig.AuthScheme != null)
                    {
                        message.Headers.Authorization = new AuthenticationHeaderValue(downloadConfig.AuthScheme,
                                                                                      downloadConfig.Auth);
                    }

                    if (downloadConfig.Timeout != 0)
                    {
                        httpClient.Timeout = TimeSpan.FromSeconds(downloadConfig.Timeout);
                    }

                    try
                    {
                        Debug.WriteLine("{0}: {1}", downloadConfig.Verb.ToLower() == "get" ? "Downloading": "Uploading", obj.Url);

                        using (var result = await httpClient.SendAsync(message))
                        {
                            Debug.WriteLine("Finished: {0}", obj.Url);
                            return(await _httpTransferService.GetResult(result, downloadConfig));
                        }
                    }
                    catch (HttpRequestException ex)
                    {
                        Debug.WriteLine("Warning - HttpRequestException encountered: {0}", ex.Message);

                        return(_httpTransferService.GetExceptionResult(ex,
                                                                       "XamlingCore.Portable.Net.Downloaders.HttpClientDownloader", downloadConfig));
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("Warning - general HTTP exception encountered: {0}", ex.Message);
                        return(_httpTransferService.GetExceptionResult(ex,
                                                                       "XamlingCore.Portable.Net.Downloaders.HttpClientDownloader", downloadConfig));
                    }
                }
            }
        }
コード例 #17
0
 public virtual IHttpTransferResult GetExceptionResult(Exception ex, string source, IHttpTransferConfig originalConfig)
 {
     OnDownloadException(ex, source, originalConfig);
     return new HttpTransferResult { DownloadException = ex, Result = null, IsSuccessCode = false };
 }
コード例 #18
0
        public async virtual Task <IHttpTransferResult> GetResult(HttpResponseMessage result, IHttpTransferConfig originalConfig)
        {
            try
            {
                var resultText = "";

                var isSuccess = true;

                if (result.Content != null)
                {
                    resultText = await result.Content.ReadAsStringAsync();
                }

                if (result.StatusCode == HttpStatusCode.Unauthorized)
                {
                    await OnUnauthorizedResult(result, originalConfig);

                    isSuccess = false;
                }

                if (!result.IsSuccessStatusCode)
                {
                    await OnUnsuccessfulResult(result, originalConfig);

                    isSuccess = false;
                }

                var headers = new Dictionary <string, List <string> >();

                if (result.Headers != null)
                {
                    foreach (var item in result.Headers)
                    {
                        headers.Add(item.Key, item.Value.ToList());
                    }
                }

                return(new HttpTransferResult
                {
                    HttpStatusCode = result.StatusCode,
                    Result = resultText,
                    IsSuccessCode = isSuccess,
                    Headers = headers
                });
            }
            catch (Exception ex)
            {
                return(GetExceptionResult(ex, "DownloadConfigService", originalConfig));
            }
        }
コード例 #19
0
        public async virtual Task OnUnauthorizedResult(HttpResponseMessage result, IHttpTransferConfig originalConfig)
        {

        }