Xamarin.Forms HTTPS和自签名证书问题

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

 public async Task 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(mobileResult); return result; } 

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

System.Net.Http.HttpRequestException:发送请求时发生错误。 —> System.Runtime.InteropServices.COMException:找不到与此错误代码关联的文本。

证书颁发机构无效或不正确

在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)在System.Net.Http.HttpHandlerToFilter.d__4.MoveNext()—堆栈跟踪结束抛出exception的前一个位置— System.Runtime.Compiler服务上的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务),System.Net.Http.HttpClientHandler.d__86.MoveNext的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) )—内部exception堆栈跟踪的结束—在System.Net.Http.HttpClientHandler.d__86.MoveNext()—从抛出exception的先前位置的堆栈跟踪结束—在System.Runtime.CompilerServices System.Net.Http.HttpClient.d__58.MoveNext()的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中的.TaskAwaiter.ThrowForNonSuccess(任务任务)—来自先前位置的堆栈跟踪结束 rown —位于System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务), 1.GetResult() at SampleApp.Services.LoginService.d__2.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter 1.GetResult()在SampleApp.Views.LoginPage.d__1.MoveNext()

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

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

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

确保包含System.Net。

希望这可以帮助。