Exemple #1
0
        private static void ConfigureHeader(HttpWebRequest webRequest, string headerName, string headerValue)
        {
            // If the header in question corresponds to an HttpWebRequest property, set it.
            if (string.Compare(headerName, CommonHeaders.Accept, StringComparison.OrdinalIgnoreCase) == 0)
            {
                webRequest.Accept = headerValue;
            }
            else if (string.Compare(headerName, CommonHeaders.Connection, StringComparison.OrdinalIgnoreCase) == 0)
            {
                webRequest.Connection = headerValue;
            }
            else if (string.Compare(headerName, CommonHeaders.ContentLength, StringComparison.OrdinalIgnoreCase) == 0)
            {
                webRequest.ContentLength = Convert.ToInt64(headerValue);
            }
            else if (string.Compare(headerName, CommonHeaders.ContentType, StringComparison.OrdinalIgnoreCase) == 0)
            {
                webRequest.ContentType = headerValue;
            }
            else if (string.Compare(headerName, CommonHeaders.Date, StringComparison.OrdinalIgnoreCase) == 0)
            {
                DateTime parsedDateTime;

                if (NScrapeUtility.TryParseHttpDate(headerValue, out parsedDateTime))
                {
                    webRequest.Date = parsedDateTime;
                }
            }
            else if (string.Compare(headerName, CommonHeaders.Expect, StringComparison.OrdinalIgnoreCase) == 0)
            {
                webRequest.Expect = headerValue;
            }
            else if (string.Compare(headerName, CommonHeaders.Host, StringComparison.OrdinalIgnoreCase) == 0)
            {
                webRequest.Host = headerValue;
            }
            else if (string.Compare(headerName, CommonHeaders.IfModifiedSince, StringComparison.OrdinalIgnoreCase) == 0)
            {
                DateTime parsedDateTime;

                if (NScrapeUtility.TryParseHttpDate(headerValue, out parsedDateTime))
                {
                    webRequest.IfModifiedSince = parsedDateTime;
                }
            }
            else if (string.Compare(headerName, CommonHeaders.Range, StringComparison.OrdinalIgnoreCase) == 0)
            {
                // TODO: To support, we'd need to parse the provided range specification (which can include multiple ranges) and
                // make one or more calls to HttpWebRequest.AddRange(String, Int64, Int64).
                // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
                throw new NotSupportedException("The Range header is not currently supported.");
            }
            else if (string.Compare(headerName, CommonHeaders.Referer, StringComparison.OrdinalIgnoreCase) == 0)
            {
                webRequest.Referer = headerValue;
            }
            else if (string.Compare(headerName, CommonHeaders.TransferEncoding, StringComparison.OrdinalIgnoreCase) == 0)
            {
                throw new NotSupportedException("The Transfer-Encoding header is not currently supported.");
            }
            else
            {
                // A header for which there is no property.
                webRequest.Headers.Add(headerName, headerValue);
            }
        }