当前位置: 首页>>代码示例>>C#>>正文


C# HttpWebRequest.AddRange方法代码示例

本文整理汇总了C#中HttpWebRequest.AddRange方法的典型用法代码示例。如果您正苦于以下问题:C# HttpWebRequest.AddRange方法的具体用法?C# HttpWebRequest.AddRange怎么用?C# HttpWebRequest.AddRange使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在HttpWebRequest的用法示例。


在下文中一共展示了HttpWebRequest.AddRange方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: InitializeRequest

 public static void InitializeRequest(HttpWebRequest request)
 {
     request.Headers.Add("aw-tenant-code", API_TENANT_CODE);
     request.Credentials = new NetworkCredential(USER_NAME, PASSWORD);
     request.KeepAlive = false;
     request.AddRange(1024);
     request.Timeout = 10000;
 }
开发者ID:Curtisspope,项目名称:MDM_API_AW,代码行数:8,代码来源:DataRetrieval.cs

示例2: SetRequestHeaders

        private static void SetRequestHeaders(HttpWebRequest webRequest, HttpRequestMessage request)
        {
            WebHeaderCollection webRequestHeaders = webRequest.Headers;
            HttpRequestHeaders headers = request.Headers;

            // Most headers are just added directly to HWR's internal headers collection. But there are some exceptions
            // requiring different handling.
            // The following bool vars are used to skip string comparison when not required: E.g. if the 'Host' header
            // was not set, we don't need to compare every header in the collection with 'Host' to make sure we don't
            // add it to HWR's header collection.
            bool isHostSet = headers.Contains(HttpKnownHeaderNames.Host);
            bool isExpectSet = headers.Contains(HttpKnownHeaderNames.Expect);
            bool isTransferEncodingSet = headers.Contains(HttpKnownHeaderNames.TransferEncoding);
            bool isConnectionSet = headers.Contains(HttpKnownHeaderNames.Connection);
            bool isAcceptSet = headers.Contains(HttpKnownHeaderNames.Accept);
            bool isDateSet = headers.Contains(HttpKnownHeaderNames.Date);
            bool isIfModifiedSinceSet = headers.Contains(HttpKnownHeaderNames.IfModifiedSince);
            bool isRangeSet = headers.Contains(HttpKnownHeaderNames.Range);
            bool isRefererSet = headers.Contains(HttpKnownHeaderNames.Referer);
            bool isUserAgentSet = headers.Contains(HttpKnownHeaderNames.UserAgent);

            if (isHostSet)
            {
                string host = headers.Host;
                if (host != null)
                {
                    webRequest.Host = host;
                }
            }

            if (isDateSet)
            {
                DateTimeOffset? date = headers.Date;
                if (date != null)
                {
                    webRequest.Date = date.Value.Date;
                }
            }

            if (isIfModifiedSinceSet)
            {
                DateTimeOffset? ifModifiedSince = headers.IfModifiedSince;
                if (ifModifiedSince != null)
                {
                    webRequest.IfModifiedSince = ifModifiedSince.Value.Date;
                }
            }

            if (isRangeSet)
            {
                RangeHeaderValue range = headers.Range;
                if (range != null)
                {
                    foreach(var rangeItem in range.Ranges)
                    {
                        webRequest.AddRange((long) rangeItem.From, (long) rangeItem.To);
                    }
                }
            }

            if (isRefererSet)
            {
                Uri referer = headers.Referrer;
                if (referer != null)
                {
                    webRequest.Referer = referer.OriginalString;
                }
            }

            // The following headers (Expect, Transfer-Encoding, Connection) have both a collection property and a
            // bool property indicating a special value. Internally (in HttpHeaders) we don't distinguish between
            // "special" values and other values. So we must make sure that we add all but the special value to HWR.
            // E.g. the 'Transfer-Encoding: chunked' value must be set using HWR.SendChunked, whereas all other values
            // can be added to the 'Transfer-Encoding'. The collection property (headers.Expect, headers.Connection,
            // headers.TransferEncoding) only return non-special values.
            if (isExpectSet && (headers.Expect.Count > 0))
            {
                webRequest.Expect = GetValueString(headers.Expect);
            }

            if (isTransferEncodingSet && (headers.TransferEncoding.Count > 0))
            {
                webRequest.TransferEncoding = GetValueString(headers.TransferEncoding);
            }

            if (isConnectionSet && (headers.Connection.Count > 0))
            {
                webRequest.Connection = GetValueString(headers.Connection);
            }

            if (isAcceptSet && (headers.Accept.Count > 0))
            {
                webRequest.Accept = GetValueString(headers.Accept);
            }

            if (isUserAgentSet && headers.UserAgent.Count > 0)
            {
                webRequest.UserAgent = GetValueString(headers.UserAgent);
            }

//.........这里部分代码省略.........
开发者ID:nuxleus,项目名称:WCFWeb,代码行数:101,代码来源:HttpClientChannel.cs

示例3: AddRequestHeaders

        private static void AddRequestHeaders(HttpWebRequest request, HttpRequestHeaders headers)
        {
            foreach (var header in headers) {
                switch (header.Key.ToLowerInvariant ()) {
                case "accept":
                    request.Accept = headers.Accept.ToString ();
                    break;

                case "connection":
                    request.Connection = headers.Connection.ToString ();
                    break;

                case "date":
                    // .NET 3.5 does not expose a property for setting this reserved header
                    goto default;

                case "expect":
                    request.Expect = headers.Expect.ToString ();
                    break;

                case "host":
                    // .NET 3.5 does not expose a property for setting this reserved header
                    goto default;

                case "if-modified-since":
                    request.IfModifiedSince = headers.IfModifiedSince.Value.UtcDateTime;
                    break;

                case "range":
                    foreach (var range in headers.Range.Ranges) {
                        checked {
                            if (!string.IsNullOrEmpty(headers.Range.Unit)) {
                                if (range.To.HasValue)
                                    request.AddRange (headers.Range.Unit, (int) range.From.Value, (int) range.To.Value);
                                else
                                    request.AddRange (headers.Range.Unit, (int) range.From.Value);
                            } else {
                                if (range.To.HasValue)
                                    request.AddRange ((int) range.From.Value, (int) range.To.Value);
                                else
                                    request.AddRange ((int) range.From.Value);
                            }
                        }
                    }

                    break;

                case "referer":
                    request.Referer = headers.Referrer.OriginalString;
                    break;

                case "transfer-encoding":
                    request.TransferEncoding = headers.TransferEncoding.ToString ();
                    break;

                case "user-agent":
                    request.UserAgent = headers.UserAgent.ToString ();
                    break;

                default:
                    foreach (var value in header.Value) {
                        request.Headers.Add (header.Key, value);
                    }

                    break;
                }
            }
        }
开发者ID:RainsSoft,项目名称:dotnet-httpclient35,代码行数:68,代码来源:HttpClientHandler.cs


注:本文中的HttpWebRequest.AddRange方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。