本文整理匯總了C#中System.Net.Http.StringContent.Clear方法的典型用法代碼示例。如果您正苦於以下問題:C# StringContent.Clear方法的具體用法?C# StringContent.Clear怎麽用?C# StringContent.Clear使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Net.Http.StringContent
的用法示例。
在下文中一共展示了StringContent.Clear方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Should_read_serialized_object_from_stream_using_date_handler
public void Should_read_serialized_object_from_stream_using_date_handler()
{
var formatter = new ServiceStackTextFormatter(JsonDateHandler.DCJSCompatible);
var value = GetTestObject();
var utf8Encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);
JsConfig.DateHandler = JsonDateHandler.DCJSCompatible;
byte[] data = utf8Encoding.GetBytes(value.ToJson());
JsConfig.Reset();
var memoryStream = new MemoryStream(data);
var contentHeader = new StringContent(string.Empty).Headers;
contentHeader.Clear();
var resultTask = formatter.ReadFromStreamAsync(typeof(RootClass), memoryStream, contentHeader, new FormatterContext(StandardMediaTypeHeaderValues.ApplicationJson, isRead: false));
resultTask.Wait();
resultTask.Result.ShouldBeType<RootClass>();
var result = (RootClass)resultTask.Result;
result.StringProperty.ShouldEqual(value.StringProperty);
result.DateProperty.ShouldEqual(value.DateProperty);
result.Child.BooleanProperty.ShouldEqual(value.Child.BooleanProperty);
result.Child.DecimalProperty.ShouldEqual(value.Child.DecimalProperty);
result.Child.DoubleProperty.ShouldEqual(value.Child.DoubleProperty);
result.Child.IntegerProperty.ShouldEqual(value.Child.IntegerProperty);
result.Child.StringProperty.ShouldEqual(value.Child.StringProperty);
}
示例2: Should_read_serialized_object_from_stream
public void Should_read_serialized_object_from_stream()
{
var formatter = new ServiceStackTextFormatter();
var value = GetTestObject();
var utf8Encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);
//Media Type Formatter uses ISO8601 date formatting by default;
JsConfig.DateHandler = JsonDateHandler.ISO8601;
byte[] data = utf8Encoding.GetBytes(value.ToJson());
JsConfig.Reset();
var memoryStream = new MemoryStream(data);
var contentHeader = new StringContent(string.Empty).Headers;
contentHeader.Clear();
var resultTask = formatter.ReadFromStreamAsync(typeof(RootClass), memoryStream, contentHeader, null);
resultTask.Wait();
resultTask.Result.ShouldBeType<RootClass>();
var result = (RootClass)resultTask.Result;
result.StringProperty.ShouldEqual(value.StringProperty);
result.DateProperty.ShouldEqual(value.DateProperty);
result.Child.BooleanProperty.ShouldEqual(value.Child.BooleanProperty);
result.Child.DecimalProperty.ShouldEqual(value.Child.DecimalProperty);
result.Child.DoubleProperty.ShouldEqual(value.Child.DoubleProperty);
result.Child.IntegerProperty.ShouldEqual(value.Child.IntegerProperty);
result.Child.StringProperty.ShouldEqual(value.Child.StringProperty);
}
示例3: Should_write_string_to_stream
public void Should_write_string_to_stream()
{
var formatter = new PlainTextFormatter();
var contentHeader = new StringContent(string.Empty).Headers;
contentHeader.Clear();
var memoryStream = new MemoryStream();
var value = "Hello World";
var resultTask = formatter.WriteToStreamAsync(typeof(string), value, memoryStream, contentHeader, transportContext: null);
resultTask.Wait();
memoryStream.Position = 0;
string serializedString = new StreamReader(memoryStream).ReadToEnd();
serializedString.ShouldEqual(value);
}
示例4: Should_read_serialized_object_from_stream
public void Should_read_serialized_object_from_stream()
{
var formatter = new PlainTextFormatter();
var value = "Hello World";
var memoryStream = new MemoryStream();
var sr = new StreamWriter(memoryStream);
sr.Write(value);
sr.Flush();
memoryStream.Position = 0;
var contentHeader = new StringContent(string.Empty).Headers;
contentHeader.Clear();
var resultTask = formatter.ReadFromStreamAsync(typeof(string), memoryStream, contentHeader, null);
resultTask.Wait();
resultTask.Result.ShouldBeType<String>();
var result = (String)resultTask.Result;
result.ShouldEqual(value);
}
示例5: Should_write_serialized_object_to_stream
public void Should_write_serialized_object_to_stream()
{
var formatter = new ServiceStackTextFormatter();
var value = GetTestObject();
var contentHeader = new StringContent(string.Empty).Headers;
contentHeader.Clear();
var memoryStream = new MemoryStream();
var resultTask = formatter.WriteToStreamAsync(typeof(RootClass), value, memoryStream, contentHeader, new FormatterContext(StandardMediaTypeHeaderValues.ApplicationJson, isRead: false), transportContext: null);
resultTask.Wait();
memoryStream.Position = 0;
string serializedString = new StreamReader(memoryStream).ReadToEnd();
// Formatter uses ISO8601 dates by default
JsConfig.DateHandler = JsonDateHandler.ISO8601;
var expextedResult = value.ToJson();
JsConfig.Reset();
serializedString.ShouldEqual(expextedResult);
}
示例6: Should_write_serialized_object_to_stream_using_date_handler
public void Should_write_serialized_object_to_stream_using_date_handler()
{
var formatter = new ServiceStackTextFormatter(JsonDateHandler.TimestampOffset);
var value = GetTestObject();
var contentHeader = new StringContent(string.Empty).Headers;
contentHeader.Clear();
var memoryStream = new MemoryStream();
var resultTask = formatter.WriteToStreamAsync(typeof(RootClass), value, memoryStream, contentHeader, transportContext: null);
resultTask.Wait();
memoryStream.Position = 0;
string serializedString = new StreamReader(memoryStream).ReadToEnd();
JsConfig.DateHandler = JsonDateHandler.TimestampOffset;
var expected = value.ToJson();
JsConfig.Reset();
serializedString.ShouldEqual(expected);
}