【问题标题】:Client unable to read respose from API客户端无法从 API 读取响应
【发布时间】:2019-09-01 04:16:17
【问题描述】:

我创造了

  1. 网页接口
  2. 客户。

我正在尝试从客户端向 Api 发出简单的获取请求 当我调试时,我可以看到我的客户端向 web api 发送请求,web api 很好地接收它并返回一个响应。但似乎响应永远不会到达我的客户并且客户一直在等待。

我尝试在我的 Web api 中使用 Postman 工具并以 json 形式收到良好的响应

我还尝试使用 Fiddle 来查看哪里出了问题。它显示了一个向下的箭头,表示正在从服务器读取响应。它没有显示任何错误代码。

有人可以建议我做错了什么吗?

API:

// GET: api/Account
public IEnumerable<Account> Get()
{
    //return new string[] { "value1", "value2" };
      Account a = new Account(100, "RES", new Customer(1, "K", "M"), new Premise(1, "2225", "City A", "Ohio"));
      Account a1 = new Account(101, "RES", new Customer(1, "R", "M"), new Premise(1, "Prior Road", "Texas", "US"));
      Account a2 = new Account(102, "RES", new Customer(1, "A", "M"), new Premise(1, "estern Road", "NY", "US"));
      List<Account> list = new List<Account>();
      list.Add(a);
      list.Add(a1);
      list.Add(a2);
      return (list);
}

客户:

public async Task<List<Account>> SelectAllAccounts()
{
    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri("https://localhost:62617/api/");
        //HTTP GET
        var response = await client.GetAsync("Account");

        var myInstance = JsonConvert.DeserializeObject<Account>(await response.Content.ReadAsStringAsync());

        List<Account> a= null;
        a.Add(myInstance);
        return a;

    }
}

【问题讨论】:

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


    【解决方案1】:

    您的简单示例中存在一些拼写错误,应予以修改:

      // GET: api/Account
            public List<Account> Get()
            {
                  Account a = new Account(100, "RES", new Customer(1, "K", "M"), new Premise(1, "2225", "City A", "Ohio"));
                  // ... 
                  // ... Your Code here
                  // ... 
                  list.Add(a2);
                  return list;            
            }
    

    而且,您还需要修改您的客户端:

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:62617/");
                HttpResponseMessage response = client.GetAsync("api/Account").Result;
    
                string _content = await response.Content.ReadAsStringAsync();
    
                List<Account> myInstance = JsonConvert.DeserializeObject<List<Account>>(_content);
    
    
                return myInstance;
            }
    

    您可以return myInstance 或返回View(myInstance),或将其返回为JsonResult,以适合您的需要。例如:

    return Json(new { Status = 200, LIST = myInstance}, JsonRequestBehavior.AllowGet);
    

    但是,有时最好将_content 作为string 返回并让反序列化在最后一刻完成。

    有关更多信息,以下是关于如何在 ASP.Net MVC 框架中创建和使用 Rest API 的简单示例:

    【讨论】:

      【解决方案2】:

      我建议不要返回 IEnumerable,而是返回 HttpResponseMessage 并将 ContentType 设置为 application/json。 所以你的api会是这样的,

      HttpResponseMessage httpResponseMessage = this.Request.CreateResponse<List<Account>>(HttpStatusCode.OK, list);
      httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
      return httpResponseMessage;
      

      在客户端代码中,您必须在返回集合时反序列化为 List 而不仅仅是 Account。所以客户端代码的变化将是

       var myInstance = JsonConvert.DeserializeObject<List<Account>>(await response.Content.ReadAsStringAsync());
      

      它已经过试验和测试。 :-)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-05-27
        • 2012-06-30
        • 2019-07-26
        • 1970-01-01
        • 2021-08-24
        • 2019-12-18
        • 2012-05-26
        相关资源
        最近更新 更多