Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Xamarin表单HttpClient卡滞_C#_Xamarin_Httpwebrequest_Xamarin.forms_Dotnet Httpclient - Fatal编程技术网

C# Xamarin表单HttpClient卡滞

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

我正在尝试从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=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
use
wait response.Content.ReadAsStringAsync()
您是否尝试在PCL项目中运行此代码?@Eser我认为OP正在使用ModernHttpClient,因此需要在PCL和本机项目中引用它。
var response = httpClient.GetAsync(ApiUrl).ConfigureAwait(false).GetAwaiter().GetResult();