C# 我';我在这里失踪了?IsSuccessStatusCode无效
在我的代码中,我试图与php Web服务通信。在Csharp IsSuccessStatusCode中的Xamarin代码中,它无效“'string'不包含“IsSuccessStatusCode”的定义…”。。。。我在网上看到的所有代码都包括这个,但我不知道它对我不起作用C# 我';我在这里失踪了?IsSuccessStatusCode无效,c#,json,xamarin,C#,Json,Xamarin,在我的代码中,我试图与php Web服务通信。在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 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);
}
private async void GetDataAsync()
{
//注:请等待职能部门在私人异步方面的描述
HttpClient HttpClient=新HttpClient();
var response=wait httpClient.GetStringAsync(“http://192.168.1.33:82/usuarios_xamarin/Usuarios.php");
if(响应。IsSuccessStatusCode)
{
var content=await response.content.ReadAsStringAsync();
var posts=JsonConvert.DeserializeObject(内容);
}
//pertenece al nugget newtonsoft.json
//我不想在掘金的比赛中出场
//var posts=JsonConvert.DeserializeObject(响应);
}
响应是一个字符串,该字符串没有IsSuccessStatusCode方法。如果使用GetAsync
而不是GetStringAsync
,则可以使用响应的属性(IsSuccessStatusCode
)
嗯,没错<代码>字符串
没有名为IsSuccessStatusCode的属性
您正在调用端点并将响应直接转换为string
。如果要检查响应的状态代码,请分两部分进行:
httpresponsemessageresponse=wait client.GetAsync(“http://192.168.1.33:82/usuarios_xamarin/Usuarios.php");
if(response.EnsureSuccessStatusCode)
{
string responseBody=wait response.Content.ReadAsStringAsync();
}