Beispiel #1
0
        FetcherResult Load(Uri uri, bool useCookies, int redirects, string previousPage)
        {
            var req = SetupRequest(uri, useCookies);
            var wb  = WebBytes.Create(req, MaxSizeHtml, MaxSizeNonHtml);

            if (wb.ContentIsHtml)
            {
                var page = WebString.Create(wb, EncHelp.Windows1252);

                Uri refreshUrl;
                if (redirects < MaxRefreshes &&
                    TryGetMetaRefresh(page, out refreshUrl) &&
                    VerifyRefresh(page.Location, refreshUrl))
                {
                    // If during the redirects we get a different page/HTML string, refrain from following more
                    // redirects. It probably means we've arrived, but only more real world testing will tell us
                    // if that's true.
                    // Otherwise keep trying to follow the redirects.
                    if (redirects == 0 || page.Document == previousPage)
                    {
                        return(Load(refreshUrl, useCookies, (redirects + 1), page.Document));
                    }
                }

                return(new FetcherResult(wb, page));
            }
            // Silently handle cookie exceptions, Mono/.NET can be very strict with which cookies it accepts.
            if (!wb.Success && wb.Exception.InnerException is CookieException)
            {
                return(Load(uri, false, redirects, previousPage));
            }

            // If either not HTML or there was an error in getting the resource, return as-is.
            return(new FetcherResult(wb));
        }
Beispiel #2
0
        public static WebString Create(WebRequest request, Encoding fallbackEnc)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            return(Create(WebBytes.Create(request), fallbackEnc));
        }