One or more errors occurred (an error occurred while sending the request) - Xamarin.Forms

0

I'm currently in development to create an application with Xamarin.Forms to manage notifications in an intranet.
So I have a button "Connection" on the main page which connects to a webservice to verify the login and the password of the user first.
My problem is the connection to the webservice always failed.

I tested the webservice with postman and it's ok ; I get a response, but when I try it with the app in visual studio (UWP or Droid projects), an exception occurres each time and I don't know why.

This is my code :

private string WebServiceAuthentication()  
{  
    string url = "https://chlevinatier.agiir-portail.com/api/jsonws/AlertesPortail-portlet.portal/check-authentication";  
    string login = "test";  
    string password = "test";`

    using ( HttpClient client = new HttpClient() )
    {
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue( "Basic", "tjhGjfM1" );

        try
        {
            return client.GetAsync( $"{url}?login={login}&password={password}" ).Result.Content.ReadAsStringAsync().Result;
        }
        catch ( AggregateException ae )
        {
            ae.Handle( ( x ) =>
            {
                if ( x is HttpRequestException )
                {
                    HttpRequestException hre = x as HttpRequestException;
                    Debug.WriteLine( $"Data : {hre.Data}\nHelpLink : {hre.HelpLink}\nHResult : {hre.HResult}\nInnerException : {hre.InnerException}\nMessage : {hre.Message}\nSource : {hre.Source}\nStackTrace : {hre.StackTrace}" );
                }

                return false;
            });

            return string.Empty;
        }
        catch ( Eception e )
        {
            Debug.WriteLine( $"Data : {e.Data}\nHelpLink : {e.HelpLink}\nHResult : {e.HResult}\nInnerException : {e.InnerException}\nMessage : {e.Message}\nSource : {e.Source}\nStackTrace : {e.StackTrace}" );
            return string.Empty;
        }
    }
}  

When I clicked on the button "Connection", I get this result on the debug screen on visual studio :

*******************************************************************************
>Data : System.Collections.ListDictionaryInternal  
>HelpLink :  
>HResult : -2147012851  
>InnerException : System.Runtime.InteropServices.COMException (0x80072F0D): Le texte associé à ce code d’erreur est introuvable.

>L'autorité de certification n'est pas valide ou correcte

>   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)  
>   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
>   at System.Net.Http.HttpHandlerToFilter.<SendAsync>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.<SendAsync>d__86.MoveNext()  

>Message : An error occurred while sending the request. 
>Source : System.Net.Http  
>StackTrace :  
>at System.Net.Http.HttpClientHandler.  <SendAsync>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.<FinishSendAsync>d__58.MoveNext()  
*******************************************************************************
c#
web-services
exception
xamarin
httpclient
asked on Stack Overflow Jul 13, 2017 by Quentin Duverger • edited Jul 13, 2017 by forivall

1 Answer

0

The problem is that the domain https://chlevinatier.agiir-portail.com/ doesn't use a valid SSL/TLS certificate so the request doesn't work because connecting with a server with an untrusted certificate isn't safe (in the majority of cases).

Solutions:

  1. Download the certificate, add this to the app and import it for example following this answer. I have been looking for a solution with HttpClient but I didn't find it. This is the right way.
  2. Avoid the validation of certificates, following this answer

I hope this can help you.

answered on Stack Overflow Jul 13, 2017 by ganchito55

User contributions licensed under CC BY-SA 3.0