如何使用system.net.http以流形式读取HttpResponseContent

时间:2018-12-10 02:27:11

标签: c# cookies azure-functions

我有一段代码可以使用HttpResponseContent来读取windows.web.http

        using System.IO;
        using Windows.Web.Http;
        using Newtonsoft.Json;

        var responseContent = JsonConvert.DeserializeObject<HttpResponseContent>(response.Content.ToString());
        if (response.StatusCode == HttpStatusCode.Ok) 
        {
            //convert only from this line
            using (var input =
                await response.Content.ReadAsInputStreamAsync()
                                      .AsTask()
                                      .ConfigureAwait(false))
            using (var stream = input.AsStreamForRead())
            using (StreamReader sr = new StreamReader(stream))
            using (JsonReader reader = new JsonTextReader(sr)) //to this line
        {
                return DeserializeData(reader);
        }

问题1 :如何使用system.net.http名称空间来实现这一点?

注意:我使用uwpwindows.web.http中编写了这些代码,我想要的是使用不同的包/命名空间(即我在Azure Functions中使用的system.net.http)转换该代码。

参考链接: https://docs.microsoft.com/en-us/dotnet/api/system.net.http?view=netframework-4.7.2 https://docs.microsoft.com/en-us/uwp/api/windows.web.http

谢谢

1 个答案:

答案 0 :(得分:0)

using (var input =
                await response.Content.ReadAsStreamAsync()
                                      .ConfigureAwait(false))
            using (StreamReader sr = new StreamReader(input))
            using (JsonReader reader = new JsonTextReader(sr))
            {
                return DeserializeData(reader);
            }