gpt4 book ai didi

ssl - 从类中使用 Xamarin Android HttpClient 错误

转载 作者:太空宇宙 更新时间:2023-11-03 14:44:11 31 4
gpt4 key购买 nike

我有以下代码在访问具有 SSL 的站点时出错。 (Error: SecureChannelFailure (The authentication or decryption has failed.) 他们的 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);
}

最佳答案

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

关于ssl - 从类中使用 Xamarin Android HttpClient 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40445255/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com