代码之家  ›  专栏  ›  技术社区  ›  Ali123

Httpclient Xamarin表单调用在Android中不工作

  •  4
  • Ali123  · 技术社区  · 8 年前

    在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();
    

    编辑:设备日志中有两个主要错误:

    [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 回复  |  直到 7 年前
        1
  •  5
  •   Brandon Minnick    8 年前

    更改Xamarin中的默认SSL/TLS实现。安卓

    转到Android项目设置->Android选项->高级->SSL/TLS实现并将其设置为 本机TLS 1.2+

    enter image description here

        2
  •  1
  •   Mohamad Mahmoud Darwish    7 年前

    在某些情况下,你需要改变 HttpClient实现 成为 AndroidClientHandler 而不是 Default 如下所示:

    enter image description here

    推荐文章