Skip to content

Instantly share code, notes, and snippets.

@LanceMcCarthy
Last active January 30, 2020 21:27
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 LanceMcCarthy/b1eb812a954e378770bfa2753fc43cad to your computer and use it in GitHub Desktop.
Save LanceMcCarthy/b1eb812a954e378770bfa2753fc43cad to your computer and use it in GitHub Desktop.
Http POST using C++
<Page x:Class="FacePostProject.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<StackPanel VerticalAlignment="Center"
HorizontalAlignment="Center">
<Button Content="Upload"
x:Name="UploadButton"
Margin="0,0,10,0"
Click="Send_Click" />
<Button Content="Cancel"
x:Name="CancelButton"
IsEnabled="False"
Click="Cancel_Click" />
</StackPanel>
</Grid>
</Page>
#include "pch.h"
#include "MainPage.xaml.h"
#include <http.h>
#include <robuffer.h>
using namespace FacePostProject;
using namespace concurrency;
using namespace Microsoft::WRL;
using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;
using namespace Windows::Web::Http;
using namespace Windows::Web::Http::Filters;
using namespace Windows::Web::Http::Headers;
using namespace Windows::Storage::Streams;
using namespace Windows::UI::Core;
MainPage::MainPage()
{
InitializeComponent();
}
void MainPage::Send_Click(Object^ sender, RoutedEventArgs^ e)
{
// Load the image file a s a UWP StorageFile object
Windows::Storage::StorageFile^ imageFile;
imageFile = Windows::Storage::StorageFile::GetFileFromPathAsync("PATH TO SAVED FILE GOES HERE")->GetResults();
// Get the image's file stream
Windows::Storage::Streams::IRandomAccessStream^ stream;
stream = imageFile->OpenAsync(Windows::Storage::FileAccessMode::Read)->GetResults();
// New up an HttpClient and set the header with your API key
HttpClient^ httpClient = ref new Windows::Web::Http::HttpClient();
httpClient->DefaultRequestHeaders->Append("Ocp-Apim-Subscription-Key", "YOUR FACE API KEY GOES HERE");
// Create stream content from the image file to send
HttpStreamContent^ streamContent;
streamContent = ref new Windows::Web::Http::HttpStreamContent(stream);
streamContent->Headers->ContentLength = ref new Box<UINT64>(stream->Size);
// Send image to API endpoint
Windows::Web::Http::HttpRequestResult^ result;
result = httpClient->TryPostAsync(ref new Uri("https://EASTUS2.cognitive.microsoft.com/face/v1.0/detect"),streamContent)->GetResults();
if (result->Succeeded)
{
String^ json;
json = result->ResponseMessage->Content->ReadAsStringAsync()->GetResults();
}
}
void MainPage::Cancel_Click(Object^ sender, RoutedEventArgs^ e)
{
}
#pragma once
#include "MainPage.g.h"
namespace FacePostProject
{
public ref class MainPage sealed
{
public:
MainPage();
private:
void Send_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
void Cancel_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
Concurrency::task<Windows::Storage::Streams::IRandomAccessStream^> GetFileStreamAsync(
Windows::Storage::StorageFile^ file);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment