Winrt | C++ -HTTP Post File-证书授权无效或不正确

发布于 2025-01-25 19:04:37 字数 3582 浏览 0 评论 0原文

基本上,我有两个问题,但是重点是如何获得认证。

我很难理解如何在Winrt | C ++中提出HTTP POST请求。我有一个ASP.NET-6-WEB-API项目,我已经能够通过Python项目和C ++项目(通过Curl)与该项目进行通信。我还通过Postman测试了API。每次这样做,我不得不忽略认证的验证,而且效果很好。

但是现在我有了一个Winrt/C ++项目,并将以下代码列入了以下代码。我希望能够上传文件。就我而言,这是一个。

我的担忧:

  1. 在Winrt中,我获得了无效/不信任认证的预期错误,因此我查看了该怎么做,最终使用nighorableservercervercertificateArtificateErrors>,就像您在代码末尾看到的那样。但这并没有解决我的错误。我想念什么?我仍然会遇到错误:
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. 从熟悉WINRT的开发人员的角度来看,实现了HTTP POST请求吗?特别是线条
    binaryContent.headers()。append(l“ content-type”,l“ image/jpeg”); and

    httpcontentDispositionheadEderValeheadEdervalue disposition {l“ form-data”};
    以及关注行是用什么?这只是为了分配任意名称吗?
disposition.Name(L"fileForUpload");
disposition.FileName(L"test.ply");

代码:

    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;
    }

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;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文