HTTPS和自签名证书问题

HTTPS和自签名证书问题

本文介绍了Xamarin.Forms HTTPS和自签名证书问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Xamarin.Forms,我的优先级是UWP。我正在尝试通过 System.Net.Http.HttpClient 发布帖子请求,我的代码看起来像这样

  public async Task< LoginResponse>登录(用户用户)
{
HttpClient client = await GetClient();

var response = await client.PostAsync(Url,new StringContent(JsonConvert.SerializeObject(user),Encoding.UTF8,application / json));
var mobileResult = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject< LoginResponse>(mobileResult);

返回结果;
}

当我提出请求时,我收到此错误

我认为自签名SSL会导致问题。我知道我可以使用Windows.Web HttpClient来忽略SSL错误,但由于一些问题,现在不可能。
我该如何解决这个问题?在此先感谢。

解决方案

使用自签名证书的一种解决方法是在初始化HttpClient之前插入以下代码,以便忽略SSL证书错误:

  ServicePointManager.ServerCertificateValidationCallback + =(sender,cert,chain,sslPolicyErrors)=>真正; 

确保包含System.Net。




I am using Xamarin.Forms and my priority is UWP. I am trying to make a post request via System.Net.Http.HttpClient and my code looks like this

public async Task<LoginResponse> Login(User user)
{
    HttpClient client = await GetClient();

    var response = await client.PostAsync(Url, new StringContent(JsonConvert.SerializeObject(user), Encoding.UTF8, "application/json"));
    var mobileResult = await response.Content.ReadAsStringAsync();
    var result = JsonConvert.DeserializeObject<LoginResponse>(mobileResult);

    return result;
}

When i make the request i am getting this error

I think the self-signed SSL causing the problem. I know i can use Windows.Web HttpClient to ignore SSL errors but due to some problems it is not possible now.How can i solve this problem ? Thanks in advance.

解决方案

A workaround for using self-signed certificates is to insert the following code before initialising your HttpClient in order to ignore SSL certificate errors:

ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;

Make sure to include System.Net.

Hope this helps.

这篇关于Xamarin.Forms HTTPS和自签名证书问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 06:51