Esempio n. 1
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\")[^>]*[\\/]?>", RegexOptions.IgnoreCase)) /*|rel=\"apple-touch-icon\"|rel=\"apple-touch-icon-precomposed\"*/
            {
                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);
        }
Esempio n. 2
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);
                        }
                        System.Net.ServicePointManager.ServerCertificateValidationCallback += (send, certificate, chain, sslPolicyErrors) => { return(true); };
                        ServicePointManager.Expect100Continue = true;
                        ServicePointManager.SecurityProtocol  = SecurityProtocolType.Tls12;
                        Stream dataStream = client.OpenRead(faviconUrl);
                        icon = Image.FromStream(dataStream);
                    }
                }
            }
            catch (WebException ex)
            {
                if (ex.Status != WebExceptionStatus.ProtocolError)
                {
                    throw;
                }
            }
            catch (Exception ex) {
                return(null);
            }
            return(icon);
        }