1

I am trying to make a POST request in an UWP C# app, based on this example - Method A. The code for my example is:

string scriptname = "myscript.php";
var content = new FormUrlEncodedContent(values);
//Exception Line (103):
var response = await client.PostAsync("https://myserver.ddns.net/" + scriptname, content);
var responseString = await response.Content.ReadAsStringAsync();
string SJson = responseString.ToString();
messagedialog.Content = SJson;

Exception log:

System.Net.Http.HttpRequestException
HResult=0x80072F0D
Message=An error occurred while sending the request.
Source=System.Net.Http
StackTrace: at System.Net.Http.HttpClientHandler.d__86.MoveNext() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Net.Http.HttpClient.d__58.MoveNext() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at Aplikacija_iFE.MainPage.d__10.MoveNext() in D:\Onedrive\myproject\myproject\App\App\MainPage.xaml.cs:line 103

Inner Exception 1: COMException: The text associated with this error code could not be found.

Overitelj digitalnih potrdil ni veljaven ali pa je napačen
The bold string is in my native language and tells me that the CA is invalid or wrong (Basically it is ,because I signed it myself). Can this error be fixed temporarily with some C# code or must I replace the certificate? My HTTPS (Apache) server is on a Debian 9 machine.
Edit (10:20 PM): Working code
The following code works for now, but it is ugly, highly insecure, and just a shane for me as a student who's new to programming :|

    string scriptname = "MyRestAPI.php";
                    HttpFormUrlEncodedContent content = new HttpFormUrlEncodedContent(values);
                    HttpResponseMessage response = new HttpResponseMessage();
                    try
                    {
                        client = new HttpClient();
                        response = await client.PostAsync(new Uri("https://myserver.ddns.net/" + scriptname), content);
                    }
                    catch (Exception e)
                    {
                        HttpBaseProtocolFilter filter = new HttpBaseProtocolFilter();
                        ChainValidationResult[] results = new ChainValidationResult []
                        {
                            ChainValidationResult.Untrusted,                   ChainValidationResult.WrongUsage,
                            ChainValidationResult.BasicConstraintsError,      ChainValidationResult.Expired,
                          ChainValidationResult.IncompleteChain,       ChainValidationResult.InvalidCertificateAuthorityPolicy,
                            ChainValidationResult.InvalidName,                  ChainValidationResult.OtherErrors,
                            ChainValidationResult.RevocationFailure,            ChainValidationResult.RevocationInformationMissing,
                            ChainValidationResult.Revoked,                      ChainValidationResult.UnknownCriticalExtension
                        };

                        for(int i=0;i<results.Length;i++)
                        {
                                                     try
                            {
                     filter.IgnorableServerCertificateErrors.Add(results[i]);
                                client = new HttpClient(filter);
                                response = await client.PostAsync(new Uri("https://myserver.ddns.net/" + scriptname), content);
                            }

                            catch
                            {
                                continue;
                            }
                        }
                        client = new HttpClient(filter);
                        response = await client.PostAsync(new Uri("https://myserver.ddns.net/" + scriptname), content);
                    }
                    finally
                    {
                        client.Dispose();
                    }                          
                    messagedialog.Content = response.Content.ToString();
shanji97
  • 71
  • 1
  • 7
  • 19
  • 1
    Possible duplicate of [How to ignore the certificate check when ssl](https://stackoverflow.com/questions/12506575/how-to-ignore-the-certificate-check-when-ssl) – Dirk Feb 20 '18 at 10:37
  • Is the URL you show above accurate or should it be an HTTPS URL? – DavidG Feb 20 '18 at 10:42
  • Sorry, there should be HTTPS in the URL. Thanks for the link @Dirk, but I my server is an Apache server on Debian 9. – shanji97 Feb 20 '18 at 10:45
  • @S4NNY1 The server doesn't matter at all. You can ignore certificate validation errors in the client with the code linked in the comment above. I strongly recommend doing this only for testing, there's a reason certificates are validated. – Dirk Feb 20 '18 at 11:04
  • But in UWP there is no `ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };` I could use IgnorableServerCertificateErrors as described [here](https://stackoverflow.com/questions/32988839/windows-10-universal-app-ignore-ssl-certificate-valdiation), but then again Windows.Web.Http would have collision with System.Net.Http which is needed for the basic the code above, since Windows.Web doesn't include some functions / methods. – shanji97 Feb 20 '18 at 11:13
  • So i found out that the equivalent of this is chain validation result, but It is poorly documented. Some of the ChainValidation results give me an exeception that the provided value is not a chain validation result value. When accessing MS Edge it gives me: DLG_FLAGS_INVALID_CA, DLG_FLAGS_SEC_CERT_CN_INVALID errors. – shanji97 Feb 20 '18 at 21:17

1 Answers1

1

You can wither use a config to ignore this error in development environment or make your client to trust the certificate, i.e just add the certificate to your trusted root on your client.

Shetty
  • 1,792
  • 4
  • 22
  • 38