我想在我的xamarin.android应用程序中从我的api usign GetStringAsync方法获取数据,但得到以下错误:
System.Net.WebException: Error: ConnectFailure (Connection refused)
我的代码:
private static async void MainAsync()
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:57558/");
var getResponse = await client.GetStringAsync("/api/home");
var getTask = client.GetStringAsync("/api/home");
await getTask.ContinueWith((antecidentTask) =>
{
var result = antecidentTask.Result;
});
}
答案 0 :(得分:0)
尝试这种方法:
using (var client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync($"{uri}{route}").ConfigureAwait(false);
var responseString = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
if (response.IsSuccessStatusCode)
{
return responseString;
}
// ...
}