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


C# HttpClient.SendRequestAsync方法代码示例

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


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

示例1: ExecuteRequest

        private static Task<AsyncResponse> ExecuteRequest(HttpClient client, HttpRequestMessage req, string authorizationHeader, ConnectionOptions options, CancellationToken cancellationToken)
        {
            var splitAuth = authorizationHeader.Split(new[] { ' ' }, 2);
            req.Headers.Add("User-Agent", options.UserAgent);
#if WIN8
            req.Headers.ExpectContinue = false;
            req.Headers.Authorization = new AuthenticationHeaderValue(splitAuth[0], splitAuth[1]);
#else
            req.Headers.Expect.Clear();
            req.Headers.Authorization = new HttpCredentialsHeaderValue(splitAuth[0], splitAuth[1]);
#endif
            if(options.BeforeRequestAction != null)
                options.BeforeRequestAction(req);
            var cancellation = new CancellationTokenSource();
            var reg = cancellationToken.Register(cancellation.Cancel);
#if WIN8
            var task = client.SendAsync(req, HttpCompletionOption.ResponseHeadersRead, cancellation.Token);
#else
            var task = client.SendRequestAsync(req, HttpCompletionOption.ResponseHeadersRead).AsTask(cancellation.Token);
#endif
            var timeoutCancellation = new CancellationTokenSource();
            DelayAction(options.Timeout, timeoutCancellation.Token, cancellation.Cancel);
            return task.ContinueWith(t =>
            {
                timeoutCancellation.Cancel();
                reg.Dispose();
                if(t.IsFaulted)
                    throw t.Exception.InnerException;
                if(!cancellationToken.IsCancellationRequested && cancellation.IsCancellationRequested)
                    throw new TimeoutException();
                return new AsyncResponse(t.Result);
            }, cancellationToken);
        }
开发者ID:kb10uy,项目名称:CoreTweet,代码行数:33,代码来源:Request.Async.cs

示例2: ExecuteRequest

        private static async Task<AsyncResponse> ExecuteRequest(HttpRequestMessage req, string authorizationHeader, ConnectionOptions options, CancellationToken cancellationToken)
        {
            var splitAuth = authorizationHeader.Split(new[] { ' ' }, 2);
#if WIN_RT
            req.Headers.Add("User-Agent", options.UserAgent);
            req.Headers.Expect.Clear();
            req.Headers.Authorization = new HttpCredentialsHeaderValue(splitAuth[0], splitAuth[1]);
            if(options.DisableKeepAlive)
            {
                req.Headers.Connection.Clear();
                req.Headers.Connection.Add(new HttpConnectionOptionHeaderValue("close"));
            }
            options.BeforeRequestAction?.Invoke(req);
            var handler = new HttpBaseProtocolFilter();
            handler.AutomaticDecompression = options.UseCompression;
            var cancellation = new CancellationTokenSource();
            cancellationToken.Register(cancellation.Cancel);
            cancellation.CancelAfter(options.Timeout);
            var client = new HttpClient(handler);
            var task = client.SendRequestAsync(req, HttpCompletionOption.ResponseHeadersRead).AsTask(cancellation.Token);
            return new AsyncResponse(await task.ConfigureAwait(false));
#else
            req.Headers.TryAddWithoutValidation("User-Agent", options.UserAgent);
            req.Headers.ExpectContinue = false;
            req.Headers.Authorization = new AuthenticationHeaderValue(splitAuth[0], splitAuth[1]);
            req.Headers.ConnectionClose = options.DisableKeepAlive;
            var handler = new HttpClientHandler();
            if(options.UseCompression)
                handler.AutomaticDecompression = CompressionType;
            var client = new HttpClient(handler) { Timeout = new TimeSpan(TimeSpan.TicksPerMillisecond * options.Timeout) };
            return new AsyncResponse(await client.SendAsync(req, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false));
#endif
        }
开发者ID:kyanro,项目名称:CoreTweet,代码行数:33,代码来源:Request.Async.cs


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