赞
踩
#include <string> #include <iostream> #include <winrt/Windows.Foundation.h> #include <winrt/Windows.Foundation.Collections.h> #include <winrt/Windows.Web.Http.h> #include <winrt/Windows.Storage.h> #include <winrt/Windows.Storage.Streams.h> using namespace winrt; using namespace Windows::Foundation; using namespace Windows::Web::Http; using namespace Windows::Storage; using namespace Windows::Storage::Streams; IAsyncAction DownloadToFile(std::wstring &url, std::wstring &dirPath) { HttpClient client; auto response = co_await client.GetAsync(Uri(url)); response.EnsureSuccessStatusCode(); auto buffer = co_await response.Content().ReadAsBufferAsync(); auto localFolder = co_await StorageFolder::GetFolderFromPathAsync(dirPath); auto file = co_await localFolder.CreateFileAsync(L"downloaded.html", CreationCollisionOption::ReplaceExisting); co_await FileIO::WriteBufferAsync(file, buffer); co_return; } IAsyncOperation<int> Main() { std::wstring url = __argc > 1 ? __wargv[1] : L"https://aka.ms/cppwinrt"; std::wstring dir_path = __argc > 2 ? __wargv[2] : L"C:\\Temp"; try { co_await DownloadToFile(url, dir_path); std::wcout << "Done." << std::endl; co_return 0; } catch (hresult_error const &ex) { std::wcout << "Error: " << std::wstring_view(ex.message()) << std::endl; co_return 1; } } int main() { init_apartment(); return Main().get(); }
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。