简体   繁体   中英

What I'm missing here? IsSuccessStatusCode not valid

In my code I'm trying to communicate with a php webservice. In my Xamarin code in Csharp IsSuccessStatusCode it's invalid "'string' does not contain a definition for "isSuccessStatusCode'.... All codes i seen in the net includes this but i don't know it's not working for me.

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);


}

response is a string and string doesnt have a method IsSuccessStatusCode. If you use GetAsync instead of GetStringAsync , then you can use the property ( IsSuccessStatusCode ) of response .

Documentation on HttpClient.GetAsync Method

Well, it's right. string does not have a property called IsSuccessStatusCode. HttpResponseMessage does .

You are calling the endpoint and converting the response directly to string . If you wanted to check the status code of the response, you want to do this in two parts:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM