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


C# HttpWebRequest.GetRequestStreamAsync方法代码示例

本文整理汇总了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);
		}
开发者ID:frje,项目名称:SharpLang,代码行数:43,代码来源:HttpClientHandler.cs

示例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));
        }
开发者ID:RainsSoft,项目名称:dotnet-httpclient35,代码行数:56,代码来源:HttpClientHandler.cs


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