'WinRT | C++ - HTTP Post File - The certificate authority is invalid or incorrect

Basically I have two concerns, however the focus is on how to get around the certification.

I am having a hard time understanding how to make an HTTP post request in WinRT|C++. I have an ASP.Net-6-Web-Api-Project, which I have already been able to communicate with via a Python project and a C++ project (via Curl). I also tested the api via Postman. Every time doing so I had to ignore the validation of certification and it worked fine.

But now I have a WinRT/C++ project and have thrown together the following code. I want to be able to upload a file. In my case it is a point cloud in a .ply syntax as a string.

My Concerns:

  1. In WinRT I got the expected error for invalid/untrusted certification, so I looked up what to do and ended up using IgnorableServerCertificateErrors from HttpBaseProtocolFilter, like you can see at the end of my code. But that did not fix my error. What am I missing? I still get the errors:
WinRT originate error - 0x80072F0D : 'The certificate authority is invalid or incorrect'.
WinRT originate error - 0x80190190 : 'The response status code does not indicate success: 400 ().'.
  1. From the point of view of a developer familiar with WinRT, is the implementation correct in terms of an HTTP post request? Especially the lines
    binaryContent.Headers().Append(L"Content-Type", L"image/jpeg"); and
    HttpContentDispositionHeaderValue disposition{ L"form-data" };
    And what are the follow lines for? Is this just about assigning arbitrary names?
disposition.Name(L"fileForUpload");
disposition.FileName(L"test.ply");

Code:

    void HL2ResearchMode::SendPLY(std::wstring const& pointCloud)
    {
        OutputDebugString(L"--- SendPLY()\n");

        if (pointCloud.size() == 0)
            return;

        init_apartment();

        auto buffer{
            winrt::Windows::Security::Cryptography::CryptographicBuffer::ConvertStringToBinary(
                pointCloud,
                winrt::Windows::Security::Cryptography::BinaryStringEncoding::Utf8
            )
        };

        winrt::Windows::Web::Http::HttpBufferContent binaryContent{ buffer };
        // binaryContent.Headers().Append(L"Content-Type", L"text/plain;charset=utf8");
        binaryContent.Headers().Append(L"Content-Type", L"image/jpeg");

        winrt::Windows::Web::Http::Headers::HttpContentDispositionHeaderValue disposition{ L"form-data" };
        //winrt::Windows::Web::Http::Headers::HttpContentDispositionHeaderValue disposition{ L"multipart/form-data" };
        binaryContent.Headers().ContentDisposition(disposition);
        disposition.Name(L"fileForUpload");
        disposition.FileName(L"test.ply");

        winrt::Windows::Web::Http::HttpMultipartFormDataContent postContent;
        postContent.Add(binaryContent);

        winrt::Windows::Web::Http::HttpResponseMessage httpResponseMessage;
        std::wstring httpResponseBody;

        try
        {
            // Send the POST request.
            winrt::Windows::Foundation::Uri requestUri{ L"https://192.168.178.41:5001/api/meshes/uploadPointCloud" };
            winrt::Windows::Web::Http::Filters::HttpBaseProtocolFilter myFilter;

            auto fu = myFilter.IgnorableServerCertificateErrors();
            fu.Append(ChainValidationResult::Expired);
            fu.Append(ChainValidationResult::Untrusted);
            fu.Append(ChainValidationResult::InvalidName);
            fu.Append(ChainValidationResult::InvalidSignature);
            fu.Append(ChainValidationResult::InvalidCertificateAuthorityPolicy);

            winrt::Windows::Web::Http::HttpClient httpClient(myFilter);
            httpResponseMessage = httpClient.PostAsync(requestUri, postContent).get();
            httpResponseMessage.EnsureSuccessStatusCode();
            httpResponseBody = httpResponseMessage.Content().ReadAsStringAsync().get();
        }
        catch (winrt::hresult_error const& ex)
        {
            httpResponseBody = ex.message();
        }
        std::wcout << httpResponseBody;
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source