从类使用Xamarin Android HttpClient错误 [英] Xamarin Android HttpClient Error when use from a Class

查看:73
本文介绍了从类使用Xamarin Android HttpClient错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,当访问具有SSL的内容时会出错.(错误:SecureChannelFailure(身份验证或解密失败.)其SSL证书有效.直接调用HttpClient代码时没有问题.我的代码有什么问题?

I have the following code that errors out when going to site what has SSL. (Error: SecureChannelFailure (The authentication or decryption has failed.) Their SSL cert is valid. When the HttpClient code is called directly there is not issue. What is wrong with my code?

 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);
    }

推荐答案

感谢 Xamarin中具有TLS 1.2的Https 这是解决方案.添加Paul Betts的Nuget modernhttpclient并在下面使用.那应该在课堂上起作用.

Thanks to Https with TLS 1.2 in Xamarin here is the solution. Add Nuget modernhttpclient by Paul Betts and use below. That should work within class or not.

            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);
            }

这篇关于从类使用Xamarin Android HttpClient错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆