Exemple #1
0
        protected virtual HttpClient CreateHttpClient(Uri url, IWebReader parent, ContentKind contentKind, ContentType contentType)
        {
            url = HttpClientWebReaderManager.GetUrl(url, parent);
            Uri referrer = HttpClientWebReaderManager.GetReferrer(parent);

            if ((Uri)null != referrer)
            {
                url = new Uri(referrer, url);
            }
            return(this._httpClientFactory.CreateClient(url, referrer, contentKind, contentType));
        }
Exemple #2
0
        public virtual async Task <ContentType> DetectContentTypeAsync(Uri url, ContentKind contentKind, CancellationToken cancellationToken, IWebReader parent = null)
        {
            ContentType contentType = EnumerableExtensions.SingleOrDefaultSafe <ContentType>((IEnumerable <ContentType>) this._contentTypeDetector.GetContentType(url, (string)null, (string)null));
            ContentType contentType1;

            if ((ContentType)null != contentType)
            {
                Debug.WriteLine("HttpClientWebReaderManager.DetectContentTypeAsync() url ext \"{0}\" type {1}", (object)url, (object)contentType);
                contentType1 = contentType;
            }
            else
            {
                Uri referrer = HttpClientWebReaderManager.GetReferrer(parent);
                using (HttpClient client = this._httpClientFactory.CreateClient(url, referrer, ContentKind.Unknown, (ContentType)null))
                {
                    try
                    {
                        using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Head, url))
                        {
                            using (HttpResponseMessage httpResponseMessage = await HttpClientExtensions.SendAsync(client, request, HttpCompletionOption.ResponseHeadersRead, cancellationToken, referrer, new long?(), new long?()).ConfigureAwait(false))
                            {
                                if (httpResponseMessage.IsSuccessStatusCode)
                                {
                                    contentType = EnumerableExtensions.SingleOrDefaultSafe <ContentType>((IEnumerable <ContentType>)HttpClientContentTypeDetectorExtensions.GetContentType(this._contentTypeDetector, request.RequestUri, httpResponseMessage.Content.Headers, HttpContentExtensions.FileName(httpResponseMessage.Content)));
                                    if ((ContentType)null != contentType)
                                    {
                                        Debug.WriteLine("HttpClientWebReaderManager.DetectContentTypeAsync() url HEAD \"{0}\" type {1}", (object)url, (object)contentType);
                                        contentType1 = contentType;
                                        goto label_49;
                                    }
                                }
                            }
                        }
                    }
                    catch (HttpRequestException ex)
                    {
                    }
                    try
                    {
                        using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url))
                        {
                            using (HttpResponseMessage httpResponseMessage = await HttpClientExtensions.SendAsync(client, request, HttpCompletionOption.ResponseHeadersRead, cancellationToken, referrer, new long?(0L), new long?(0L)).ConfigureAwait(false))
                            {
                                if (httpResponseMessage.IsSuccessStatusCode)
                                {
                                    contentType = EnumerableExtensions.SingleOrDefaultSafe <ContentType>((IEnumerable <ContentType>)HttpClientContentTypeDetectorExtensions.GetContentType(this._contentTypeDetector, request.RequestUri, httpResponseMessage.Content.Headers, HttpContentExtensions.FileName(httpResponseMessage.Content)));
                                    if ((ContentType)null != contentType)
                                    {
                                        Debug.WriteLine("HttpClientWebReaderManager.DetectContentTypeAsync() url range GET \"{0}\" type {1}", (object)url, (object)contentType);
                                        contentType1 = contentType;
                                        goto label_49;
                                    }
                                }
                            }
                        }
                    }
                    catch (HttpRequestException ex)
                    {
                    }
                    try
                    {
                        using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url))
                        {
                            using (HttpResponseMessage httpResponseMessage = await HttpClientExtensions.SendAsync(client, request, HttpCompletionOption.ResponseHeadersRead, cancellationToken, referrer, new long?(), new long?()).ConfigureAwait(false))
                            {
                                if (httpResponseMessage.IsSuccessStatusCode)
                                {
                                    contentType = EnumerableExtensions.SingleOrDefaultSafe <ContentType>((IEnumerable <ContentType>)HttpClientContentTypeDetectorExtensions.GetContentType(this._contentTypeDetector, request.RequestUri, httpResponseMessage.Content.Headers, HttpContentExtensions.FileName(httpResponseMessage.Content)));
                                    if ((ContentType)null != contentType)
                                    {
                                        Debug.WriteLine("HttpClientWebReaderManager.DetectContentTypeAsync() url GET \"{0}\" type {1}", (object)url, (object)contentType);
                                        contentType1 = contentType;
                                        goto label_49;
                                    }
                                }
                            }
                        }
                    }
                    catch (HttpRequestException ex)
                    {
                    }
                }
                Debug.WriteLine("HttpClientWebReaderManager.DetectContentTypeAsync() url header \"{0}\" unknown type", (object)url);
                contentType1 = (ContentType)null;
            }
label_49:
            return(contentType1);
        }