【问题标题】:Alternative to ReadAsAsync method in HttpContentHttpContent 中 ReadAsAsync 方法的替代方法
【发布时间】:2014-10-29 07:04:59
【问题描述】:

我正在使用Microsoft.AspNet.WebApi.Client 在我的 ASP.MVC 5 项目中使用休息服务。我正在按照this 教程来使用 HttpClient。代码未编译,因为 HttpContent 中不再提供 ReadAsAsync 方法。经过一番挖掘,我知道这是System.Net.Http.Formatting.dll中定义的扩展方法。我为同一个 dll here 找到了一个 nuget 包,但该包已被弃用,我无法安装它。我还尝试根据this 帖子在 Program Files 文件夹中搜索该 dll,但我无法得到它。任何想法如何使ReadAsAsync 工作?非常感谢任何帮助。谢谢。

【问题讨论】:

  • 添加参考并包括System.Net.Http;System.Net.Http.Formating;版本4.0.0.0
  • 我可以从哪里得到那个 dll?任何想法?我尝试通过Packge Manger Console安装它,但它显示'无法找到'System.Net.Http.Formatting'包的'4.0.0.0'版本。我猜它已被弃用。
  • 您的 GAC 中已经有 Http.Formatting
  • @TejasSutar 右键单击​​您的项目>添加参考>程序集>并包括 System.Net.HttpSystem.Net.Http.Formating
  • 你的回答真的帮助了@YuvalItzchakov..!!谢谢。

标签: c# asp.net-mvc asp.net-web-api httpclient


【解决方案1】:

你需要做的是添加新的引用System.Net.HttpClient;System.Net.HttpClient.Formating;

这是我在 HttpClient 中的示例代码:

以下代码用于使用 HttpClient 从 saba 获取证书。

using System.Net.Http;
using System.Net.Http.Headers;
using GoSaba.Models.Saba;

namespace GoSaba.Controllers.Saba
{
    class LoginController
    {
        //HTTP GET: Saba/api/login
        public async Task<string> GetCertificate(string host, string user, string password, string site)
        {
            StringBuilder getCertificate = new StringBuilder();

            if(!string.IsNullOrEmpty(host))
            {
                using(var httpClient = new HttpClient())
                {
                    httpClient.BaseAddress = new Uri(string.Format("http://{0}/", host));
                    httpClient.DefaultRequestHeaders.Accept.Clear();
                    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    httpClient.DefaultRequestHeaders.Add("user", user);
                    httpClient.DefaultRequestHeaders.Add("password", password);
                    httpClient.DefaultRequestHeaders.Add("site", site);

                    HttpResponseMessage httpResponse = await httpClient.GetAsync("Saba/api/login");

                    if(httpResponse.IsSuccessStatusCode)
                    {
                        LoginModel.GetCertificate saba = await httpResponse.Content.ReadAsAsync<LoginModel.GetCertificate>();//LoginModel.GetCertificate is model.
                        getCertificate.Append(saba.certificate);
                    }
                }
            }

            return getCertificate.ToString();
        }
    }
}

您可以参考如何使用 HttpClient。

【讨论】:

  • 我的电脑没有System.Net.Http.Formatting,是哪个.Net版本安装的?
  • 它在我的工作电脑上,但不在我的家用电脑上。两者都安装了 .Net 4.6.1。
【解决方案2】:

这是使用相同 Microsoft.AspNet.WebApi.Client 的另一种方法。代码行数增加了,但你不会发现像 Newtonsoft 版本问题这样的问题,这促使我寻找 ReadAsAsync 的替代方案。

这是一个解释代码的链接 - https://www.newtonsoft.com/json/help/html/Performance.htm

HttpClient client = new HttpClient();

using (Stream s = client.GetStreamAsync("http://www.test.com/large.json").Result)
using (StreamReader sr = new StreamReader(s))
using (JsonReader reader = new JsonTextReader(sr))
{
    JsonSerializer serializer = new JsonSerializer();

    // read the json from a stream
    // json size doesn't matter because only a small piece is read at a time from the HTTP request
    T p = serializer.Deserialize<T>(reader); // Where T is any type
}

【讨论】:

    猜你喜欢
    • 2015-01-12
    • 1970-01-01
    • 2012-09-25
    • 1970-01-01
    • 1970-01-01
    • 2011-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多