Esempio n. 1
0
        private Image DownloadFavicon(Uri baseUrl, string iconUrl)
        {
            Image     icon   = null;
            WebClient client = new WebClientExtended(_timeout);

            try
            {
                // Download fav icon
                if (!string.IsNullOrEmpty(iconUrl))
                {
                    Uri faviconUrl;

                    if (Uri.TryCreate(iconUrl, UriKind.RelativeOrAbsolute, out faviconUrl))
                    {
                        if (!faviconUrl.IsAbsoluteUri)
                        {
                            faviconUrl = new Uri(baseUrl, iconUrl);
                        }

                        Stream dataStream = client.OpenRead(faviconUrl);
                        icon = Image.FromStream(dataStream);
                    }
                }
            }
            catch (WebException ex)
            {
                if (ex.Status != WebExceptionStatus.ProtocolError)
                {
                    throw;
                }
            }

            return(icon);
        }
Esempio n. 2
0
        private string ExtractFavIconUrl(Uri url)
        {
            WebClient client  = new WebClientExtended(_timeout);
            string    iconUrl = null;

            string html = client.DownloadString(url);

            Match match;

            // Link
            foreach (Match m in Regex.Matches(html, "<link[^>]*(rel=\"icon\"|rel=\"shortcut icon\"|rel=\"apple-touch-icon\"|rel=\"apple-touch-icon-precomposed\")[^>]*[\\/]?>", RegexOptions.IgnoreCase))
            {
                match = Regex.Match(m.Value, "href=\"([^\"]*)\"", RegexOptions.IgnoreCase);

                if (match.Success)
                {
                    return(match.Groups[1].Value);
                }
            }

            // Meta
            foreach (Match m in Regex.Matches(html, "<meta[^>]*(itemprop=\"image\")[^>]*[\\/]?>", RegexOptions.IgnoreCase))
            {
                match = Regex.Match(m.Value, "content=\"([^\"]*)\"", RegexOptions.IgnoreCase);

                if (match.Success)
                {
                    return(match.Groups[1].Value);
                }
            }

            return(iconUrl);
        }