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