本文介绍了"ReadAsAsync< string>"和"ReadAsStringAsync"应用于什么用途?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HttpContentExtensions.ReadAsAsync<string>HttpContent.ReadAsStringAsync应该用于什么?

他们似乎做类似的事情,但工作方式奇怪.以下是一些测试及其输出.在某些情况下,抛出JsonReaderException,在某些情况下,将输出JSON,但带有附加的转义字符.

They would seem to do similiar things but work in curious ways. A couple of tests and their outputs are below. In some cases JsonReaderException are thrown, in some cases, the JSON is output but with additional escape characters.

我最终在我的代码库中同时使用了这两个函数,但是如果我能理解它们应该如何工作的话,我希望能够与之对接.

I've ended up using both functions across my code base, but was hoping to align on one if I could understand how they were supposed to work.

//Create data and serialise to JSON
var data = new
{
    message = "hello world"
};
string dataAsJson = JsonConvert.SerializeObject(data);

//Create request with data
HttpConfiguration config = new HttpConfiguration();
HttpRequestMessage request = new HttpRequestMessage();
request.SetConfiguration(config);
request.Method = HttpMethod.Post;
request.Content = new StringContent(dataAsJson, Encoding.UTF8, "application/json");

string requestContentT = request.Content.ReadAsAsync<string>().Result; // -> JsonReaderException: Error reading string.Unexpected token: StartObject.Path '', line 1, position 1.
string requestContentS = request.Content.ReadAsStringAsync().Result; // -> "{\"message\":\"hello world\"}"

//Create response from request with same data
HttpResponseMessage responseFromRequest = request.CreateResponse(HttpStatusCode.OK, dataAsJson, "application/json");

string responseFromRequestContentT = responseFromRequest.Content.ReadAsAsync<string>().Result; // -> "{\"message\":\"hello world\"}"
string responseFromRequestContentS = responseFromRequest.Content.ReadAsStringAsync().Result; // -> "\"{\\\"message\\\":\\\"hello world\\\"}\""

//Create a standalone new response
HttpResponseMessage responseNew = new HttpResponseMessage();
responseNew.Content = new StringContent(dataAsJson, Encoding.UTF8, "application/json");

string responseNewContentT = responseNew.Content.ReadAsAsync<string>().Result; // -> JsonReaderException: Error reading string.Unexpected token: StartObject.Path '', line 1, position 1.
string responseNewContentS = responseNew.Content.ReadAsStringAsync().Result; // -> "{\"message\":\"hello world\"}"

推荐答案

ReadAsStringAsync :这是一种基本的以字符串形式获取内容"方法.因为它只是字符串,所以它可以在您扔给它的任何东西上工作.

ReadAsStringAsync: This is a basic "get me the content as a string" method. It will work on anything you throw at it because it's just strings.

ReadAsAsync<T> :这用于将JSON响应反序列化为对象.失败的原因是,返回中的JSON不是单个字符串的有效JSON表示形式.例如,如果您序列化一个字符串:

ReadAsAsync<T>: This is meant to be used to deserialise a JSON response into an object. The reason it fails is because the JSON in the return is not a valid JSON representation of a single string. For example, if you serialise a string:

var result = JsonConvert.SerializeObject("hello world");
Console.WriteLine(result);

输出为:

"hello world"

请注意如何用双引号将其引起来.如果您尝试将任意JSON直接反序列化为格式不是"....."的字符串,则会抛出您看到的异常,因为它期望JSON以"开头.

Note how it is surrounded by double quotes. If you try to deserialise any arbitrary JSON directly into a string that isn't in the format "....." it will throw the exception you see because it is expecting the JSON to start with a ".

这篇关于"ReadAsAsync&lt; string&gt;"和"ReadAsStringAsync"应用于什么用途?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 09:17