本文整理汇总了C#中HttpWebRequest.GetRequestStreamAsync方法的典型用法代码示例。如果您正苦于以下问题:C# HttpWebRequest.GetRequestStreamAsync方法的具体用法?C# HttpWebRequest.GetRequestStreamAsync怎么用?C# HttpWebRequest.GetRequestStreamAsync使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpWebRequest
的用法示例。
在下文中一共展示了HttpWebRequest.GetRequestStreamAsync方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SendAsync
protected async internal override Task<HttpResponseMessage> SendAsync (HttpRequestMessage request, CancellationToken cancellationToken)
{
if (disposed)
throw new ObjectDisposedException (GetType ().ToString ());
Volatile.Write (ref sentRequest, true);
wrequest = CreateWebRequest (request);
if (request.Content != null) {
var headers = wrequest.Headers;
foreach (var header in request.Content.Headers) {
foreach (var value in header.Value) {
headers.AddValue (header.Key, value);
}
}
var stream = await wrequest.GetRequestStreamAsync ().ConfigureAwait (false);
await request.Content.CopyToAsync (stream).ConfigureAwait (false);
} else if (HttpMethod.Post.Equals (request.Method) || HttpMethod.Put.Equals (request.Method) || HttpMethod.Delete.Equals (request.Method)) {
// Explicitly set this to make sure we're sending a "Content-Length: 0" header.
// This fixes the issue that's been reported on the forums:
// http://forums.xamarin.com/discussion/17770/length-required-error-in-http-post-since-latest-release
wrequest.ContentLength = 0;
}
HttpWebResponse wresponse = null;
using (cancellationToken.Register (l => ((HttpWebRequest) l).Abort (), wrequest)) {
try {
wresponse = (HttpWebResponse) await wrequest.GetResponseAsync ().ConfigureAwait (false);
} catch (WebException we) {
if (we.Status != WebExceptionStatus.RequestCanceled)
throw;
}
if (cancellationToken.IsCancellationRequested) {
var cancelled = new TaskCompletionSource<HttpResponseMessage> ();
cancelled.SetCanceled ();
return await cancelled.Task;
}
}
return CreateResponseMessage (wresponse, request, cancellationToken);
}
示例2: SendAsync
protected internal override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (disposed != 0)
throw new ObjectDisposedException (GetType ().ToString ());
Interlocked.Exchange(ref sentRequest, 1);
wrequest = CreateWebRequest (request);
Task intermediate;
if (request.Content != null) {
AddContentHeaders(wrequest, request.Content.Headers);
intermediate = wrequest.GetRequestStreamAsync ()
.Then (streamTask => request.Content.CopyToAsync (streamTask.Result));
} else if (HttpMethod.Post.Equals (request.Method) || HttpMethod.Put.Equals (request.Method) || HttpMethod.Delete.Equals (request.Method)) {
// Explicitly set this to make sure we're sending a "Content-Length: 0" header.
// This fixes the issue that's been reported on the forums:
// http://forums.xamarin.com/discussion/17770/length-required-error-in-http-post-since-latest-release
wrequest.ContentLength = 0;
intermediate = CompletedTask.Default;
} else {
intermediate = CompletedTask.Default;
}
HttpWebResponse wresponse = null;
Func<Task<IDisposable>> resource =
() => CompletedTask.FromResult<IDisposable> (cancellationToken.Register (l => ((HttpWebRequest) l).Abort (), wrequest));
Func<Task<IDisposable>, Task> body =
_ => {
return wrequest.GetResponseAsync ().Select(task => wresponse = (HttpWebResponse)task.Result)
.Catch<WebException> (
(task, we) => {
if (we.Status == WebExceptionStatus.ProtocolError) {
// HttpClient shouldn't throw exceptions for these errors
wresponse = (HttpWebResponse) we.Response;
} else if (we.Status != WebExceptionStatus.RequestCanceled) {
// propagate the antecedent
return task;
}
return CompletedTask.Default;
})
.Then (
task => {
if (cancellationToken.IsCancellationRequested) {
return CompletedTask.Canceled<HttpResponseMessage> ();
} else {
return CompletedTask.Default;
}
});
};
return intermediate
.Then (_ => TaskBlocks.Using (resource, body))
.Select (_ => CreateResponseMessage (wresponse, request, cancellationToken));
}