在 Windows 10、1803 中,可以从符合标准的 C++ 调用 WinRT。不再需要 C++ /CX 语言扩展。
我已经知道如何使用 HttpRequestMessage 类向服务器发送发布请求:
#pragma comment(lib, "windowsapp")
#include "pch.h"
#include "winrt/Windows.Web.Http.Filters.h"
#include "winrt/Windows.Web.Http.Headers.h"
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <string_view>
using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::Web::Http;
using namespace Windows::Web::Http::Filters;
using namespace Windows::Web::Http::Headers;
using namespace Windows::Foundation::Collections;
using namespace std::literals;
using namespace std;
IAsyncAction HttpClientExample(Uri uri)
{
HttpBaseProtocolFilter filter;
HttpClient httpClient(filter);
HttpRequestMessage loginRequest(HttpMethod::Post(), uri);
try
{
auto response{ co_await httpClient.SendRequestAsync(loginRequest) };
HttpCookieCollection cookieCollection = filter.CookieManager().GetCookies(uri);
wcout << cookieCollection.Size() << L" cookies found." << endl;
for (HttpCookie cookie : cookieCollection) {
wcout << L"Name: " << cookie.Name().c_str() << endl;
wcout << L"Domain: " << cookie.Domain().c_str() << endl;
wcout << L"Path: " << cookie.Path().c_str() << endl;
wcout << L"Value: " << cookie.Value().c_str() << endl;
}
}
catch (winrt::hresult_error const& ex)
{
// Details in ex.message() and ex.to_abi().
}
}
int main() {
init_apartment();
Uri uri{ L"http://www.google.com/"sv };
HttpClientExample(uri);
Sleep(10000);
}
如何使用 HttpFormUrlEncodedContent 设置自定义标题以及如何在 POST 正文中设置自定义内容?
谢谢您的帮助!