Httpclient Xamarin Forms调用无法在Android中运行

时间:2017-11-24 21:10:09

标签: xamarin.forms xamarin.android httpclient

在Android物理设备中调试xamarin表单应用程序时出现了一个奇怪的问题。

应用程序进入中断模式,这是我收到的输出:

  Mono.Android[0xd6be8960] -> System[0xd6be9680]: 14
    11-24 23:44:44.098 I/Choreographer(18685): Skipped 930 frames!  The application may be doing too much work on its main thread.
    Thread started: <Thread Pool> #8
    Thread started: <Thread Pool> #9
    11-24 23:44:44.807 D/Mono    (18685): Assembly Ref addref Mono.Security[0xc17989e0] -> System[0xd6be9680]: 15
    Thread started: <Thread Pool> #10
    An unhandled exception occured.

我尝试了许多解决方案,例如在应用程序中允许网络访问,确保所有需要的软件包都存在,例如:

Microsoft.Bcl.Build
Microsoft.net.HTTP
Newtonsoft.json

这是我在UWP中运行良好的代码  var request = new HttpRequestMessage();

    request.RequestUri = new Uri("https://jsonplaceholder.typicode.com/users");
    request.Method = HttpMethod.Get;

    request.Headers.Add("Accept", "application/json");
    var client = new HttpClient();
    HttpResponseMessage response = await client.SendAsync(request);
    HttpContent content = response.Content;
    var statusCode = response.StatusCode;
    var json = await content.ReadAsStringAsync();

编辑:我在设备日志中收到2个主要错误:

[ERROR] FATAL UNHANDED EXCEPTION: System.Net.HttpRequestException:An error occurred while sending the request --> System.Net.WebException:Error:SecureChannelFailure(The authentication or decryption has failed)

2 个答案:

答案 0 :(得分:4)

更改Xamarin.Android中的默认SSL / TLS实现。

转到Android项目设置 - &gt; Android选项 - &gt;高级 - &gt; SSL / TLS实施并将其设置为 Native TLS 1.2 +

enter image description here

答案 1 :(得分:1)

在某些情况下,您需要将 HttpClient实现更改为AndroidClientHandler,而不是Default,如下所示:

enter image description here