Skip to content

Instantly share code, notes, and snippets.

@mojeld
Created August 29, 2018 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mojeld/735d29b16436287e8c654643384799b3 to your computer and use it in GitHub Desktop.
Save mojeld/735d29b16436287e8c654643384799b3 to your computer and use it in GitHub Desktop.
How to use the HttpClient class in Visual C++ 2017
void cpp_uwp::json_test::Button_Click_1(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
using namespace Windows::Web::Http;
using namespace Windows::Storage::Streams;
ActivityIndicator1->IsActive = true;
IHttpClient^ NetHttp1 = ref new HttpClient();
concurrency::create_task(NetHttp1->GetAsync(ref new Uri("https://うあーるえる"))).then([this](IHttpResponseMessage^ res) {
concurrency::create_task(res->Content->ReadAsBufferAsync()).then([this](IBuffer^ buf) {
std::function<Platform::String ^ (std::string)> Utf8ToPlatformString{ [](std::string stin) {
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t> > converter;
return ref new Platform::String(converter.from_bytes(stin.c_str()).c_str());
} };
DataReader^ BufRead = DataReader::FromBuffer(buf);
Platform::Array<unsigned char>^ arr_stream = ref new Platform::Array<unsigned char>(buf->Length);
BufRead->ReadBytes(arr_stream);
std::stringstream ss1;
for (int i = 0; i < buf->Length; ++i)
{
ss1 << static_cast<char>(arr_stream[i]);
}
Edit1->Text = Utf8ToPlatformString(ss1.str());
ActivityIndicator1->IsActive = false;
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment