Пример #1
0
        public static FavIconDescriptor RequestIcon(string baseUri, IWebProxy proxy, ICredentials credentials)
        {
            if (baseUri == null)
            {
                throw new ArgumentNullException("baseUri");
            }

            if (proxy == null)
            {
                proxy = GlobalProxySelection.GetEmptyWebProxy();
            }

            FavIconDescriptor ico = null;

            try {
                using (Stream stream = AsyncWebRequest.GetSyncResponseStream(baseUri, credentials, "RssBandit", proxy)) {
                    string htmlContent = string.Empty;
                    using (StreamReader reader = new StreamReader(stream)) {
                        htmlContent = reader.ReadToEnd();
                        ico         = RequestIcon(baseUri, htmlContent, proxy, credentials);
                    }
                }
            } catch {
                // no default HTML page to examine for a FavIcon entry.
                // we do not stop here: try to get a direct download by requesting favicon.ico directly
            }

            string realIconUrl = null;

            if (ico == null || ico == FavIconDescriptor.Empty)
            {
                try {
                    Uri b = new Uri(new Uri(baseUri), "favicon.ico");
                    realIconUrl = b.ToString();
                } catch (UriFormatException) {}
            }

            if (realIconUrl == null)
            {
                return(FavIconDescriptor.Empty);
            }

            // now get the icon stream
            using (Stream stream = AsyncWebRequest.GetSyncResponseStream(realIconUrl, credentials, "RssBandit", proxy)) {
                Image img = CheckAndScaleImageFromStream(stream);
                if (img != null)
                {
                    return(new FavIconDescriptor(realIconUrl, baseUri, DateTime.Now, img));
                }
            }

            return(FavIconDescriptor.Empty);
        }
Пример #2
0
        public static FavIconDescriptor RequestIcon(string baseUri, string htmlContent, IWebProxy proxy, ICredentials credentials)
        {
            if (htmlContent == null || htmlContent.Length == 0)
            {
                return(FavIconDescriptor.Empty);
            }

            if (proxy == null)
            {
                proxy = WebProxy.GetDefaultProxy();
            }

            string realIconUrl = null;

            //<link rel="shortcut icon" href="url to an .ico">
            MatchCollection matches = autoDiscoverRegex.Matches(htmlContent);

            foreach (Match match in matches)
            {
                if (String.Compare(match.Groups["attName"].Value, "rel", true) == 0)
                {
                    string url = match.Groups["href"].Value;
                    realIconUrl = ConvertToAbsoluteUrl(url, baseUri);
                    break;
                }
            }

            if (realIconUrl == null)
            {
                return(FavIconDescriptor.Empty);
            }

            // now get the icon stream
            using (Stream stream = AsyncWebRequest.GetSyncResponseStream(realIconUrl, credentials, "RssBandit", proxy)) {
                Image img = CheckAndScaleImageFromStream(stream);
                if (img != null)
                {
                    return(new FavIconDescriptor(realIconUrl, baseUri, DateTime.Now, img));
                }
            }

            return(FavIconDescriptor.Empty);
        }