1

I have created a Xamarin.Forms Portable project which targets Android, iOS, UWP, Windows and WindowsPhone platforms. I am trying to make a Http request using HttpClient which uses System.Net.Http namespace. The application is trying to request some secure endpoints. Here is a part of my code:

            HttpClientHandler handler = new HttpClientHandler();
            HttpClient httpClient = new HttpClient();
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, reqUrl);


            string charset = "UTF-8";
            string contentType = "application/json";

            bool isAuthOrReg = (reqUrl.Contains("/user/xxx") ||
                                reqUrl.Contains("/user/xxx") ||
                                reqUrl.Contains("/user/xxx));

            if (isAuthOrReg)
            {
                request.Headers.Add("KEY", getKey());
                request.Headers.Add("SECRET", getSecret());
            }
            request.Headers.Add("Accept-Charset", charset);
            request.Content = new StringContent(postData, Encoding.UTF8, contentType);
            request.Content.Headers.ContentLength = postData.Length;

            HttpResponseMessage response = await httpClient.SendAsync(request);
            string serverResponse =  await response.Content.ReadAsStringAsync();

When I run the project as a Windows 8.1 application, it works fine. but, when I run it as an Android application, I get Unhandled Exception:

System.Net.WebException: Error: SendFailure (Error writing headers)"
06-16 13:59:27.012 D/Mono    ( 1702): DllImport attempting to load:    '/system/lib/liblog.so'.
06-16 13:59:27.012 D/Mono    ( 1702): DllImport loaded library '/system/lib/liblog.so'.
06-16 13:59:27.012 D/Mono    ( 1702): DllImport searching in: '/system/lib/liblog.so' ('/system/lib/liblog.so').
06-16 13:59:27.012 D/Mono    ( 1702): Searching for '__android_log_print'.
06-16 13:59:27.012 D/Mono    ( 1702): Probing '__android_log_print'.
06-16 13:59:27.012 D/Mono    ( 1702): Found as '__android_log_print'.
06-16 13:59:27.040 I/MonoDroid( 1702): UNHANDLED EXCEPTION:
06-16 13:59:27.064 I/MonoDroid( 1702): System.Net.WebException: Error:    SendFailure (Error writing headers) ---> System.Net.WebException: Error writing headers ---> System.IO.IOException: The authentication or decryption has failed. ---> Mono.Security.Protocol.Tls.TlsException: The authentication or decryption has failed.
06-16 13:59:27.065 I/MonoDroid( 1702):   at Mono.Security.Protocol.Tls.RecordProtocol.EndReceiveRecord (IAsyncResult asyncResult) [0x0003a] in /Users/builder/data/lanes/3236/ee215fc9/source/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/RecordProtocol.cs:430 
06-16 13:59:27.065 I/MonoDroid( 1702):   at Mono.Security.Protocol.Tls.SslClientStream.SafeEndReceiveRecord (IAsyncResult ar, Boolean ignoreEmpty) [0x00000] in /Users/builder/data/lanes/3236/ee215fc9/source/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/SslClientStream.cs:285 
06-16 13:59:27.065 I/MonoDroid( 1702):   at Mono.Security.Protocol.Tls.SslClientStream.NegotiateAsyncWorker (IAsyncResult result) [0x00071] in /Users/builder/data/lanes/3236/ee215fc9/source/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/SslClientStream.cs:447 
06-16 13:59:27.065 I/MonoDroid( 1702):   --- End of inner exception stack trace ---

In my personal opinion, this is due to Android being unable to ignore ssl. So, I tried to add following by refering to Ignore SSL certificate errors in Xamarin.Forms (PCL)

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

But, I am getting the same error. Is there any way to solve this problem? Thanks.

Community
  • 1
  • 1
  • One possible cause of this error is if the server does not allow TLS 1.0 ciphers. See for example https://bugzilla.xamarin.com/show_bug.cgi?id=26658. See also "Native HttpClientHandler" on the latest Xamarin.Android release notes: https://developer.xamarin.com/releases/android/xamarin.android_6/xamarin.android_6.1/#AndroidHttpClientHandler – Brendan Zagaeski Jun 17 '16 at 20:07

0 Answers0