C# Xamarin表单HttpClient卡滞
我正在尝试从SoundCloudAPI获取响应。这是我的密码C# Xamarin表单HttpClient卡滞,c#,xamarin,httpwebrequest,xamarin.forms,dotnet-httpclient,C#,Xamarin,Httpwebrequest,Xamarin.forms,Dotnet Httpclient,我正在尝试从SoundCloudAPI获取响应。这是我的密码 public static async Task<string> GetTheGoodStuff() { var client = new HttpClient(new NativeMessageHandler()); var response = await client.GetAsync("http://api.soundcloud.com/playlists?client_id=17ecae4
public static async Task<string> GetTheGoodStuff()
{
var client = new HttpClient(new NativeMessageHandler());
var response = await client.GetAsync("http://api.soundcloud.com/playlists?client_id=17ecae4040e171a5cf25dd0f1ee47f7e&limit=1");
var responseString = response.Content.ReadAsStringAsync().Result;
return responseString;
}
publicstaticasync任务GetTheGoodStuff()
{
var client=newhttpclient(new NativeMessageHandler());
var response=wait client.GetAsync(“http://api.soundcloud.com/playlists?client_id=17ecae4040e171a5cf25dd0f1ee47f7e&limit=1");
var responseString=response.Content.ReadAsStringAsync().Result;
回报率;
}
但是它在var response=await client.GetAsync
上遇到了问题。我怎样才能解决这个问题
谢谢 我只是在PCL中使用了您的代码,我唯一更改的是url(改为
https
),以满足iOS ATS要求,并从异步方法调用它。在iOS设备上运行似乎效果良好。我确实在PCL中获得了对Microsoft.Net.Http的引用,在PCL和平台特定项目中获得了对ModernHttpClient的引用(通过NuGet)
您在某些PCL视图模型类中的代码:
using System.Net.Http;
using System.Threading.Tasks;
using ModernHttpClient;
public class ItemsViewModel
{
...
public async Task<string> GetPlaylist()
{
// Use https to satisfy iOS ATS requirements.
var client = new HttpClient(new NativeMessageHandler());
var response = await client.GetAsync("https://api.soundcloud.com/playlists?client_id=17ecae4040e171a5cf25dd0f1ee47f7e&limit=1");
var responseString = await response.Content.ReadAsStringAsync();
return responseString;
}
...
}
希望这有帮助。我也有同样的问题。我通过以下方式修复了它:
var response = httpClient.GetAsync(ApiUrl).ConfigureAwait(false).GetAwaiter().GetResult();
你可以试试。不使用
新的NativeMessageHandler()
你的代码对我有用。。。顺便说一句,不要阻塞Result
usewait response.Content.ReadAsStringAsync()
您是否尝试在PCL项目中运行此代码?@Eser我认为OP正在使用ModernHttpClient,因此需要在PCL和本机项目中引用它。
var response = httpClient.GetAsync(ApiUrl).ConfigureAwait(false).GetAwaiter().GetResult();