【问题标题】:What I'm missing here? IsSuccessStatusCode not valid我在这里缺少什么? IsSuccessStatusCode 无效
【发布时间】:2020-09-21 17:58:25
【问题描述】:

在我的代码中,我正在尝试与 php 网络服务进行通信。在我的 Csharp IsSuccessStatusCode 中的 Xamarin 代码中,它是无效的“'string' 不包含“isSuccessStatusCode'...的定义。我在网上看到的所有代码都包括这个,但我不知道它对我不起作用。

private async void GetDataAsync()
{
    //nota: para que await funcione hay que escribir en la rutina al lado de private async
    HttpClient httpClient = new HttpClient();
    var response = await httpClient.GetStringAsync("http://192.168.1.33:82/usuarios_xamarin/Usuarios.php");


    if (response.IsSuccessStatusCode)
    {
        var content = await response.Content.ReadAsStringAsync();
        var posts = JsonConvert.DeserializeObject<List<Posts>>(content);
    }
    //pertenece al nugget newtonsoft.json
    //si no esta instalado hay que instalarlo en los nuggets

    //var posts = JsonConvert.DeserializeObject<List<Posts>>(response);


}

【问题讨论】:

    标签: c# json xamarin


    【解决方案1】:

    response 是一个字符串,而字符串没有 IsSuccessStatusCode 方法。如果你使用GetAsync而不是GetStringAsync,那么你可以使用response的属性(IsSuccessStatusCode)。

    Documentation on HttpClient.GetAsync Method

    【讨论】:

      【解决方案2】:

      嗯,没错。 string 没有名为 IsSuccessStatusCode 的属性。 HttpResponseMessage does.

      您正在调用端点并将响应直接转换为string。如果您想检查响应的状态码,您需要分两部分进行:

      HttpResponseMessage response = await client.GetAsync("http://192.168.1.33:82/usuarios_xamarin/Usuarios.php");
      if (response.EnsureSuccessStatusCode) 
      {
          string responseBody = await response.Content.ReadAsStringAsync();
      }
      

      【讨论】:

        猜你喜欢
        • 2010-12-24
        • 2012-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-25
        • 1970-01-01
        • 2018-09-03
        相关资源
        最近更新 更多