本文整理汇总了C#中System.Net.Http.StringContent.ReadAsAsync方法的典型用法代码示例。如果您正苦于以下问题:C# StringContent.ReadAsAsync方法的具体用法?C# StringContent.ReadAsAsync怎么用?C# StringContent.ReadAsAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Http.StringContent
的用法示例。
在下文中一共展示了StringContent.ReadAsAsync方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReadAsAsyncOfT_WhenTypeIsReferenceTypeAndNoMediaType_ReturnsNull
public void ReadAsAsyncOfT_WhenTypeIsReferenceTypeAndNoMediaType_ReturnsNull()
{
var content = new StringContent("{}");
content.Headers.ContentType = null;
var formatters = new MediaTypeFormatter[] { new JsonMediaTypeFormatter() };
Assert.Equal(null, content.ReadAsAsync<List<string>>(formatters).Result);
}
示例2: ReadAsAsyncOfT_WhenNoMatchingFormatterFoundForContentWithNoMediaType_Throws
public void ReadAsAsyncOfT_WhenNoMatchingFormatterFoundForContentWithNoMediaType_Throws()
{
var content = new StringContent("{}");
content.Headers.ContentType = null;
var formatters = new MediaTypeFormatter[] { new JsonMediaTypeFormatter() };
Assert.Throws<InvalidOperationException>(() => content.ReadAsAsync<List<string>>(formatters),
"No MediaTypeFormatter is available to read an object of type 'List`1' from content with media type ''undefined''.");
}
示例3: ReadAsAsyncOfT_WhenTypeIsReferenceTypeAndNoMediaType_Throws
public void ReadAsAsyncOfT_WhenTypeIsReferenceTypeAndNoMediaType_Throws()
{
var content = new StringContent("{}");
content.Headers.ContentType = null;
var formatters = new MediaTypeFormatter[] { new JsonMediaTypeFormatter() };
Assert.Throws<UnsupportedMediaTypeException>(() => content.ReadAsAsync<List<string>>(formatters),
"No MediaTypeFormatter is available to read an object of type 'List`1' from content with media type 'application/octet-stream'.");
}
示例4: ReadAsAsyncOfT_WhenNoMatchingFormatterFound_Throws
public void ReadAsAsyncOfT_WhenNoMatchingFormatterFound_Throws()
{
var content = new StringContent("{}");
content.Headers.ContentType = _mediaType;
content.Headers.ContentType.CharSet = "utf-16";
var formatters = new MediaTypeFormatter[] { new JsonMediaTypeFormatter() };
Assert.Throws<InvalidOperationException>(() => content.ReadAsAsync<List<string>>(formatters),
"No MediaTypeFormatter is available to read an object of type 'List`1' from content with media type 'foo/bar'.");
}
示例5: ReadingFormData
public static void ReadingFormData()
{
var content = new StringContent("a[]=1&a[]=5&a[]=333", Encoding.UTF8, "application/x-www-form-urlencoded");
// var body = await content.ReadAsFormDataAsync();
var body = content.ReadAsAsync<JObject>().Result;
// Verify that the data we read was the same as we submitted to the `StringContent`.
var arr = (JArray)body["a"];
Helpers.AssertEquality(Helpers.__, arr.ToString());
}
示例6: ReadAsAsyncOfT_InvokesFormatterEvenIfContentLengthIsZero
public void ReadAsAsyncOfT_InvokesFormatterEvenIfContentLengthIsZero()
{
var content = new StringContent("");
_formatterMock.Setup(f => f.CanReadType(typeof(string))).Returns(true);
_formatterMock.Object.SupportedMediaTypes.Add(content.Headers.ContentType);
var result = content.ReadAsAsync<string>(_formatters);
result.WaitUntilCompleted();
_formatterMock.Verify(f => f.ReadFromStreamAsync(typeof(string), It.IsAny<Stream>(), content.Headers, It.IsAny<IFormatterLogger>()), Times.Once());
}
示例7: ReadAsAsyncOfT_WhenTypeIsValueTypeAndNoMediaType_ReturnsDefault
public void ReadAsAsyncOfT_WhenTypeIsValueTypeAndNoMediaType_ReturnsDefault()
{
var content = new StringContent("123456");
content.Headers.ContentType = null;
var formatters = new MediaTypeFormatter[] { new JsonMediaTypeFormatter() };
Assert.Equal(0, content.ReadAsAsync<int>(formatters).Result);
}
示例8: ReadAsAsyncOfT_cancellationToken_PassesCancellationTokenFurther
public void ReadAsAsyncOfT_cancellationToken_PassesCancellationTokenFurther()
{
CancellationTokenSource cts = new CancellationTokenSource();
cts.Cancel();
HttpContent content = new StringContent("42", Encoding.Default, "application/json");
Assert.Throws<TaskCanceledException>(() => content.ReadAsAsync<int>(cts.Token).Wait());
}