public static void SetHeader(this IHttpWebRequest webRequest, string headerName, string headerValue)
        {
            switch (headerName.ToLowerInvariant())
            {
            case "accept":
                webRequest.Accept = headerValue;
                break;

            case "content-type":
                webRequest.ContentType = headerValue;
                break;

            case "range": {
                var range = ParseRangeHeader(headerValue);

                webRequest.AddRange(range.Item1, range.Item2);
            }
            break;

            case "user-agent":
                webRequest.UserAgent = headerValue;
                break;

            default:
                webRequest.Headers.Set(headerName, headerValue);
                break;
            }
        }
예제 #2
0
        private static void SetRangeHeader(IHttpWebRequest httpWebRequest, string value)
        {
            var range = ParseRangeHeader(value);

            httpWebRequest.AddRange(range.Item1, range.Item2);
        }
예제 #3
0
        void SetupHeader()
        {
            if (!PersistCookies || cookieContainer == null)
            {
                cookieContainer = new CookieContainer();
            }

            httpWebRequest.CookieContainer        = cookieContainer;
            httpWebRequest.ContentType            = ContentType;
            httpWebRequest.Accept                 = Accept;
            httpWebRequest.Method                 = Method.ToString();
            httpWebRequest.UserAgent              = UserAgent;
            httpWebRequest.Referer                = Referer;
            httpWebRequest.CachePolicy            = _cachePolicy;
            httpWebRequest.KeepAlive              = KeepAlive;
            httpWebRequest.AutomaticDecompression = DisableAutomaticCompression
                                                    ? DecompressionMethods.None
                                                    : DecompressionMethods.Deflate | DecompressionMethods.GZip | DecompressionMethods.None;

            ServicePointManager.Expect100Continue = Expect;
            ServicePointManager.ServerCertificateValidationCallback = AcceptAllCertifications;

            if (Timeout > 0)
            {
                httpWebRequest.Timeout = Timeout;
            }


            if (Cookies != null)
            {
                httpWebRequest.CookieContainer.Add(Cookies);
            }

            if (IfModifiedSince != DateTime.MinValue)
            {
                httpWebRequest.IfModifiedSince = IfModifiedSince;
            }


            if (Date != DateTime.MinValue)
            {
                httpWebRequest.Date = Date;
            }

            if (!String.IsNullOrEmpty(Host))
            {
                httpWebRequest.Host = Host;
            }

            if (MaxForwards != 0)
            {
                httpWebRequest.MaximumAutomaticRedirections = MaxForwards;
            }

            if (Range != 0)
            {
                httpWebRequest.AddRange(Range);
            }

            SetupAuthentication();

            AddExtraHeader("From", From);
            AddExtraHeader("Accept-CharSet", AcceptCharSet);
            AddExtraHeader("Accept-Encoding", AcceptEncoding);
            AddExtraHeader("Accept-Language", AcceptLanguage);
            AddExtraHeader("If-Match", IfMatch);
            AddExtraHeader("Content-Encoding", ContentEncoding);

            foreach (var header in RawHeaders)
            {
                httpWebRequest.Headers.Add(String.Format("{0}: {1}", header.Key, header.Value));
            }
        }