本文整理汇总了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);
}
示例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
}