/// <summary>
        /// Fetches the HTMLMetaData
        /// </summary>
        private LightWeightHTMLMetaData GetMetaData(WebRequestWithCache.CacheSettings cacheSettings)
        {
            WebRequestWithCache webRequest = new WebRequestWithCache(m_url);
            Stream stream = webRequest.GetResponseStream(cacheSettings);
            LightWeightHTMLDocument document = null;
            try
            {
                document = LightWeightHTMLDocument.FromStream(stream, m_url);
            }
            catch (Exception e)
            {
                Debug.Fail("Couldn't get metadata from stream: " + e.Message);
            }

            if (document != null)
                return new LightWeightHTMLMetaData(document);
            else
                return null;
        }
        /// <summary>
        /// Retrieve the content type by requesting the content type from the server hosting the URL
        /// </summary>
        /// <param name="url">The url for which to check content type</param>
        /// <param name="timeOutMs">The duration in MS that the operation will execute before failing</param>
        /// <returns>The content type</returns>
        private static UrlContentTypeInfo GetContentTypeUsingNetworkIO(string url, int timeOutMs)
        {
            UrlContentTypeInfo contentType = null;

            if (UrlHelper.IsFileUrl(url))
            {
                string content = GuessContentTypeLocally(url);
                if (content != null)
                {
                    contentType = new UrlContentTypeInfo(content, url);
                }
            }

            if (contentType == null)
            {
                WebRequestWithCache webRequest = new WebRequestWithCache(url);

                WebResponse response;
                if (timeOutMs == -1)
                {
                    response = webRequest.GetHeadOnly();
                }
                else
                {
                    response = webRequest.GetHeadOnly(timeOutMs);
                }

                if (response != null && response.ContentType != null && response.ContentType != string.Empty)
                {
                    string contentTypeString     = response.ContentType;
                    string contentEncodingString = null;
                    if (contentTypeString.IndexOf(";", StringComparison.OrdinalIgnoreCase) > 0)
                    {
                        string[] contentTypeParts = contentTypeString.Split(';');
                        contentTypeString     = contentTypeParts[0];
                        contentEncodingString = contentTypeParts[1];
                    }
                    contentType = new UrlContentTypeInfo(contentTypeString, contentEncodingString, UrlHelper.SafeToAbsoluteUri(response.ResponseUri), Convert.ToInt32(response.ContentLength));
                }
            }
            return(contentType);
        }
示例#3
0
        /// <summary>
        /// Fetches the HTMLMetaData
        /// </summary>
        private LightWeightHTMLMetaData GetMetaData(WebRequestWithCache.CacheSettings cacheSettings)
        {
            WebRequestWithCache webRequest = new WebRequestWithCache(m_url);
            Stream stream = webRequest.GetResponseStream(cacheSettings);
            LightWeightHTMLDocument document = null;

            try
            {
                document = LightWeightHTMLDocument.FromStream(stream, m_url);
            }
            catch (Exception e)
            {
                Debug.Fail("Couldn't get metadata from stream: " + e.Message);
            }

            if (document != null)
            {
                return(new LightWeightHTMLMetaData(document));
            }
            else
            {
                return(null);
            }
        }
        /// <summary>
        /// Retrieve the content type by requesting the content type from the server hosting the URL
        /// </summary>
        /// <param name="url">The url for which to check content type</param>
        /// <param name="timeOutMs">The duration in MS that the operation will execute before failing</param>
        /// <returns>The content type</returns>
        private static UrlContentTypeInfo GetContentTypeUsingNetworkIO(string url, int timeOutMs)
        {
            UrlContentTypeInfo contentType = null;

            if (UrlHelper.IsFileUrl(url))
            {
                string content = GuessContentTypeLocally(url);
                if (content != null)
                    contentType = new UrlContentTypeInfo(content, url);
            }

            if (contentType == null)
            {
                WebRequestWithCache webRequest = new WebRequestWithCache(url);

                WebResponse response;
                if (timeOutMs == -1)
                    response = webRequest.GetHeadOnly();
                else
                    response = webRequest.GetHeadOnly(timeOutMs);

                if (response != null && response.ContentType != null && response.ContentType != string.Empty)
                {
                    string contentTypeString = response.ContentType;
                    string contentEncodingString = null;
                    if (contentTypeString.IndexOf(";", StringComparison.OrdinalIgnoreCase) > 0)
                    {
                        string[] contentTypeParts = contentTypeString.Split(';');
                        contentTypeString = contentTypeParts[0];
                        contentEncodingString = contentTypeParts[1];
                    }
                    contentType = new UrlContentTypeInfo(contentTypeString, contentEncodingString, UrlHelper.SafeToAbsoluteUri(response.ResponseUri), Convert.ToInt32(response.ContentLength));
                }

            }
            return contentType;
        }
        /// <summary>
        /// Gets the document type string for a given URL (note that this could actually download the page!)
        /// </summary>
        /// <param name="url">The url for which to get the document type</param>
        /// <returns>The entire document type string</returns>
        public static SpecialHeaders GetSpecialHeaders(string url)
        {
            WebRequestWithCache wr = new WebRequestWithCache(url);
            Stream stream = Stream.Null;
            if (UrlHelper.IsFileUrl(url) && UrlHelper.IsUrl(url) && PathHelper.IsWebPage(new Uri(url).LocalPath))
                stream = new FileStream(new Uri(url).LocalPath, FileMode.Open, FileAccess.Read);
            else
                stream = wr.GetResponseStream(WebRequestWithCache.CacheSettings.CACHEONLY, 5000);

            return GetSpecialHeaders(stream, url);
        }
        /// <summary>
        /// Gets the body text for a given url
        /// </summary>
        /// <param name="url">The url to get the text for</param>
        /// <param name="timeout">The request timeout, in MS</param>
        /// <returns></returns>
        public static IHTMLDocument2 GetHTMLDocumentForUrl(string url, int timeout, IProgressHost progressHost)
        {
            WebRequestWithCache wr = new WebRequestWithCache(url);

            // return the html document
            return GetHTMLDocumentFromStream(wr.GetResponseStream(WebRequestWithCache.CacheSettings.CHECKCACHE, timeout), url);
        }