4
votes

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.

2
Do you mind showing your url ?Nico Zhu - MSFT
It is something like xx.xxx.xxx.xx/v1/login. Cant show the full URL sorry. Starts wıth https:// .Tartar
I think you could use DependencyService to implement the login function within uwp client project independently via Windows.Web.Http.Nico Zhu - MSFT
Let me try it. Thanks.Tartar
Tartar, Have you found any solution to bypass self-signed certificate?Ankit Jain

2 Answers

-1
votes

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.

0
votes

This code help for resolve the below error in Xamarin- Windows (UWP) - Windows 8.1

"The text associated with this error code could not be found."

  public interface IServerCommunication
  {
   Task<string> GetFromServerAsync(string URL);
  }   

//later on, when I download the data: (URL is a provided string)

  string result = await DependencyService.Get<IServerCommunication> 
  ().GetFromServerAsync(URL);

   public async Task<string> GetFromServerAsync(string URL)
   {
    HttpClient client = await PreparedClientAsync();

    HttpResponseMessage response;

    try
    {
    response = await client.GetAsync(new Uri(URL));

    IBuffer buffer = await response.Content.ReadAsBufferAsync();
    DataReader reader = DataReader.FromBuffer(buffer);
    byte[] fileContent = new byte[reader.UnconsumedBufferLength];
    reader.ReadBytes(fileContent);
    string result = Encoding.UTF8.GetString(fileContent, 0, fileContent.Length);

    return result;
    }
    catch (Exception ex)
    {
    return "error";
    }
    }


  private async Task<HttpClient> PreparedClientAsync()
  {
   var filter = new Windows.Web.Http.Filters.HttpBaseProtocolFilter();

   filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Expired);
   filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Untrusted);
   filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.InvalidName);

    HttpClient client = new HttpClient(filter);

    //I also handle other stuff here (client certificate, authentification), but the 
    lines above should allow the Httpclient to accept all certificates

    return client;
   }