Пример #1
0
        private IHttpWebRequest PrepareRequestObject(Uri url, string method, string contentType, int timeoutMilliseconds)
        {
            IHttpWebRequest req = _reqFactory.GetWebRequest(url);

            req.Method            = method;
            req.ContentType       = contentType;       // "application/x-www-form-urlencoded";
            req.UserAgent         = UserAgent;
            req.Accept            = Accept ?? "*/*";
            req.Timeout           = timeoutMilliseconds;
            req.AllowAutoRedirect = false;
            if (UseGZip)
            {
                req.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
            }
            req.CookieContainer = Cookies;
            if (_proxy != null)
            {
                req.Proxy = _proxy;
            }
            if (CurrentState != null)
            {
                req.Referer = this.Url.AbsoluteUri;
            }
            return(req);
        }
Пример #2
0
        private IHttpWebRequest PrepareRequestObject(Uri url, string method, string contentType, int timeoutMilliseconds)
        {
            IHttpWebRequest req = _reqFactory.GetWebRequest(url);

            req.Method            = method;
            req.ContentType       = contentType;
            req.UserAgent         = UserAgent;
            req.Accept            = Accept ?? "*/*";
            req.Timeout           = timeoutMilliseconds;
            req.AllowAutoRedirect = false;
            req.CookieContainer   = Cookies;

            if (UseGZip)
            {
                req.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
            }

            if (_proxy != null)
            {
                req.Proxy = _proxy;
            }

            // Start by using the browser setting for the referrer mode.
            RefererModes refererMode = this.RefererMode;

            // Allow the referrer meta tag to override the global browser mode.
            if (this.CurrentState != null)
            {
                var metatag = this.Find("meta", new { name = "referrer" });
                if (metatag.Exists)
                {
                    string content = metatag.GetAttribute("content").ToLower();
                    if (!string.IsNullOrEmpty(content))
                    {
                        TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;
                        content = textInfo.ToTitleCase(content);

                        if (Enum.TryParse <RefererModes>(content, out refererMode) == false)
                        {
                            refererMode = this.RefererMode;
                        }
                    }
                }
            }

            // Allow the anchor rel attribute to override both the browser setting and meta tag.
            if (this._navigationAttributes != null && this._navigationAttributes.AllKeys.Contains("rel") && this._navigationAttributes["rel"] == "noreferrer")
            {
                refererMode = RefererModes.Never;
            }

            switch (refererMode)
            {
            case RefererModes.Always:
            {
                if (this.CurrentState != null && !string.IsNullOrEmpty(this.CurrentState.Url.ToString()))
                {
                    req.Referer = this.CurrentState.Url.ToString();
                }

                break;
            }

            case RefererModes.Never:
            {
                req.Referer = string.Empty;
                break;
            }

            case RefererModes.Origin:
            {
                if (this.CurrentState != null && !string.IsNullOrEmpty(this.CurrentState.Url.ToString()))
                {
                    req.Referer = string.Format("{0}://{1}", this.CurrentState.Url.Scheme, this.CurrentState.Url.Host);
                }

                break;
            }

            default:                     // Including case Referemodes.Default:
            {
                if (this.CurrentState != null &&
                    !string.IsNullOrEmpty(this.CurrentState.Url.ToString()))
                {
                    if (this.CurrentState.Url.Scheme == "https" && url.Scheme == "http")
                    {
                        req.Referer = string.Empty;
                    }
                    else
                    {
                        req.Referer = this.CurrentState.Url.ToString();
                    }
                }

                break;
            }
            }

            return(req);
        }