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


C# MultipartFormDataContent.CopyToAsync方法代码示例

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


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

示例1: PrepareRequest

        /// <summary>
        /// Prepares the request. It is not nessisary to call this method unless additional configuration is required.
        /// </summary>
        /// <returns>A <see cref="HttpClient"/> object fully configured and ready for execution.</returns>
        public async Task<HttpRequestMessage> PrepareRequest()
        {
            SetupOAuth();

			formData = null;
			string contentType = string.Empty;

            if (!Multipart)
			{	//We don't add the parameters to the query if we are multipart-ing
				AddQueryStringParametersToUri();
			}
			else
			{
				string dataBoundary = "--------------------r4nd0m";
				contentType = "multipart/form-data; boundary=" + dataBoundary;

				formData = GetMultipartFormData(Parameters, dataBoundary);

				this.Verb = HttpMethod.Post;
			}

            if (Multipart)
            {	
                //Parameters are not added to the query string, post them in the request body instead
                MultipartFormDataContent content = new MultipartFormDataContent();

                content.Headers.ContentType = new MediaTypeHeaderValue(contentType);

                using (Stream requestStream = new MemoryStream())
                {
                    if (formData != null)
                    {
                        requestStream.Write(formData, 0, formData.Length);
                    }
                    await content.CopyToAsync(requestStream);
                }
            }

            request = new HttpRequestMessage(this.Verb, this.RequestUri);
            
            request.Headers.ExpectContinue = false;

            if (!this.UseOAuth && this.networkCredentials != null)
            {
                request.Headers.ProxyAuthorization = new System.Net.Http.Headers.AuthenticationHeaderValue("basic", String.Format(networkCredentials.UserName, ":", networkCredentials.Password));
            }

            request.Headers.Add("User-Agent", (string.IsNullOrEmpty(userAgent)) ? string.Format(CultureInfo.InvariantCulture, "Twitterizer/{0}", Information.AssemblyVersion()) : userAgent);
            
            if (this.UseOAuth)
            {
                request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("OAuth", GenerateAuthorizationHeader());
            }

            return request;
        }
开发者ID:DigitallyBorn,项目名称:Twitterizer3,代码行数:60,代码来源:WebRequestBuilder.cs


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