Xamarin.Forms HTTPS and Self Signed Certificate Is

2019-05-11 21:24发布

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

System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Runtime.InteropServices.COMException: The text associated with this error code could not be found.

The certificate authority is invalid or incorrect

at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Net.Http.HttpHandlerToFilter.d__4.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.Net.Http.HttpClientHandler.d__86.MoveNext() --- End of inner exception stack trace --- at System.Net.Http.HttpClientHandler.d__86.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.Net.Http.HttpClient.d__58.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.TaskAwaiter1.GetResult() at SampleApp.Services.LoginService.<Login>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.TaskAwaiter1.GetResult() at SampleApp.Views.LoginPage.d__1.MoveNext()

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.

1条回答
萌系小妹纸
2楼-- · 2019-05-11 21:50

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.

查看更多
登录 后发表回答