从类中使用Xamarin Android HttpClient时出错

时间:2016-11-06 01:51:51

标签: ssl xamarin.android httpclient

我有以下代码在访问具有SSL的网站时出错。 (错误:SecureChannelFailure(身份验证或解密失败。)他们的SSL证书有效。当直接调用HttpClient代码时没有问题。我的代码出了什么问题?

 Uri uri =new Uri("https://jsonplaceholder.typicode.com/posts/1");
  using (HttpClient httpclient = new HttpClientClass())
   {
       var tt = await httpclient.GetAsync(uri);
       string tx = await tt.Content.ReadAsStringAsync();
       Log.Info(TAG, tx);
    }




 public class HttpClientClass : HttpClient
  {
     private HttpClient _httpclient = null;
     private HttpClientHandler messagehandler = new Xamarin.Android.Net.AndroidClientHandler();

     public  HttpClientClass()
     {
       _httpclient = new HttpClient(messagehandler);
      }
   }

没有问题的代码

Uri uri =new Uri("https://jsonplaceholder.typicode.com/posts/1");
  using (HttpClient httpclient = new HttpClient())
   {
       var tt = await httpclient.GetAsync(uri);
       string tx = await tt.Content.ReadAsStringAsync();
       Log.Info(TAG, tx);
    }

1 个答案:

答案 0 :(得分:0)

感谢Https with TLS 1.2 in Xamarin 这是解决方案。由Paul Betts添加Nuget modernhttpclient并在下面使用。这应该在课堂上工作。

            Uri uri = new Uri("https://jsonplaceholder.typicode.com/posts/1");
            using (var httpClient = new HttpClient(new NativeMessageHandler()))
            {
                var tt = await httpClient.GetAsync(uri);
                string tx = await tt.Content.ReadAsStringAsync();
                //Log.Info(TAG, tx);
            }