本文整理匯總了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());
}