/// <summary>
 /// Event handler that is called when the Async Web request is complete
 /// </summary>
 private void WebRequestComplete(object send, EventArgs e)
 {
     IHTMLDocument2 htmlDoc = null;
     Stream stream = m_webRequest.ResponseStream;
     if (stream != null)
     {
         using(StreamReader reader = new StreamReader(stream))
         {
             htmlDoc = HTMLDocumentHelper.StringToHTMLDoc(reader.ReadToEnd(), m_url);
         }
         if (htmlDoc != null)
         {
             MetaData = new HTMLMetaData(htmlDoc);
         }
     }
     FireMetaDataComplete();
 }
Beispiel #2
0
        /// <summary>
        /// Event handler that is called when the Async Web request is complete
        /// </summary>
        private void WebRequestComplete(object send, EventArgs e)
        {
            IHTMLDocument2 htmlDoc = null;
            Stream         stream  = m_webRequest.ResponseStream;

            if (stream != null)
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    htmlDoc = HTMLDocumentHelper.StringToHTMLDoc(reader.ReadToEnd(), m_url);
                }
                if (htmlDoc != null)
                {
                    MetaData = new HTMLMetaData(htmlDoc);
                }
            }
            FireMetaDataComplete();
        }
        /// <summary>
        /// Gets an IHTMLDocument2 from a stream of html
        /// </summary>
        /// <param name="filePath">The filePath of html</param>
        /// <param name="sourceUrl">The source url for the document</param>
        /// <returns>The IHTMLDocument2</returns>
        public static IHTMLDocument2 GetHTMLDocumentFromFile(string filePath, string sourceUrl)
        {
            IHTMLDocument2 htmlDoc = null;
            Encoding currentEncoding = Encoding.Default;
            using (StreamReader reader = new StreamReader(filePath))
            {
                htmlDoc = StringToHTMLDoc(reader.ReadToEnd(), sourceUrl, false);
                currentEncoding = reader.CurrentEncoding;
            }

            // If there no dom, just return null
            if (htmlDoc != null)
            {
                // If there is no metadata that disagrees with our encoding, just return the DOM read with default decoding
                HTMLMetaData metaData = new HTMLMetaData(htmlDoc);
                if (metaData != null && metaData.Charset != null)
                {
                    try
                    {
                        // The decoding is different than the encoding used to read this document, reread it with correct encoding
                        Encoding encoding = Encoding.GetEncoding(metaData.Charset);
                        if (encoding != currentEncoding)
                        {
                            using (StreamReader reader = new StreamReader(filePath, encoding))
                            {
                                htmlDoc = StringToHTMLDoc(reader.ReadToEnd(), sourceUrl, false);
                            }
                        }
                    }
                    catch (NotSupportedException)
                    {
                        // The encoding isn't supported on this system
                    }
                    catch (ArgumentException)
                    {
                        // The encoding isn't an encoding that the OS even knows about (its probably
                        // not well formatted or misspelled or something)
                    }
                }
            }

            return htmlDoc;
        }