Created
August 29, 2018 10:48
-
-
Save mojeld/735d29b16436287e8c654643384799b3 to your computer and use it in GitHub Desktop.
How to use the HttpClient class in Visual C++ 2017
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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