本文整理汇总了C#中StringContent.CopyToAsync方法的典型用法代码示例。如果您正苦于以下问题:C# StringContent.CopyToAsync方法的具体用法?C# StringContent.CopyToAsync怎么用?C# StringContent.CopyToAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类StringContent
的用法示例。
在下文中一共展示了StringContent.CopyToAsync方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Ctor_UseCustomEncodingAndMediaType_EncodingUsedAndContentTypeHeaderUpdated
public async Task Ctor_UseCustomEncodingAndMediaType_EncodingUsedAndContentTypeHeaderUpdated()
{
// Use UTF-8 encoding to serialize a chinese string.
string sourceString = "\u4f1a\u5458\u670d\u52a1";
var content = new StringContent(sourceString, Encoding.UTF8, "application/custom");
Assert.Equal("application/custom", content.Headers.ContentType.MediaType);
Assert.Equal("utf-8", content.Headers.ContentType.CharSet);
var destination = new MemoryStream(12);
await content.CopyToAsync(destination);
string destinationString = Encoding.UTF8.GetString(destination.ToArray(), 0, (int)destination.Length);
Assert.Equal(sourceString, destinationString);
}
示例2: Ctor_DefineNoEncoding_DefaultEncodingUsed
public async Task Ctor_DefineNoEncoding_DefaultEncodingUsed()
{
string sourceString = "\u00C4\u00E4\u00FC\u00DC";
var content = new StringContent(sourceString);
Encoding defaultStringEncoding = Encoding.GetEncoding("utf-8");
// If no encoding is defined, the default encoding is used: utf-8
Assert.Equal("text/plain", content.Headers.ContentType.MediaType);
Assert.Equal(defaultStringEncoding.WebName, content.Headers.ContentType.CharSet);
// Make sure the default encoding is also used when serializing the content.
var destination = new MemoryStream();
await content.CopyToAsync(destination);
Assert.Equal(8, destination.Length);
destination.Seek(0, SeekOrigin.Begin);
string roundTrip = new StreamReader(destination, defaultStringEncoding).ReadToEnd();
Assert.Equal(sourceString, roundTrip);
}