Just for fun, I thought that I would build on my previous article (Windows 10 IoT Core: Getting the MAC Address from Raspberry Pi) and this time use POST to send data to the web server. I decided on starting a package.
To start a package, we need a some information, this includes:
I started with an function that handles the items above.
private async void StartApp( string appName ) { String RelativeID = await GetPackageRelativeId(appName); byte toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(RelativeID); string appName64 = System.Convert.ToBase64String(toEncodeAsBytes);
System.Diagnostics.Debug.WriteLine("http://localhost:8080/api/taskmanager/start?appid=" + appName64);
StreamReader SR = await PostJsonStreamData("http://localhost:8080/api/taskmanager/start?appid=" + appName64); }
It starts by getting the Relative ID by calling GetPackageRelativeId() to get that add name. GetPackageRelativeId() looks like this:
private async Task<String> GetPackageRelativeId(String PackageName) { return await GetPackageNamedName(PackageName, "PackageRelativeId"); } private async Task<String> GetPackageNamedName(String PackageName, String NamedName) { String PackageRelativeId = null; StreamReader SR = await GetJsonStreamData("http://localhost:8080/api/appx/installed"); JsonObject ResultData = null; try { String JSONData;
JSONData = SR.ReadToEnd();
ResultData = (JsonObject)JsonObject.Parse(JSONData); JsonArray InstalledPackages = ResultData.GetNamedArray("InstalledPackages");
//foreach (JsonObject Package in InstalledPackages) for (uint index = 0; index < InstalledPackages.Count; index++) { JsonObject Package = InstalledPackages.GetObjectAt(index).GetObject(); String Name = Package.GetNamedString("Name"); if (Name.ToLower().CompareTo(PackageName.ToLower()) == 0) { PackageRelativeId = ((JsonObject)Package).GetNamedString(NamedName); break; } } } catch (Exception E) { System.Diagnostics.Debug.WriteLine(E.Message); }
return PackageRelativeId; }
Looks a lot like GetMAC() from Windows 10 IoT Core: Getting the MAC Address from Raspberry Pi, because, well, I copied my own code…
And then it POSTs the request to start the package – and this will also look like code from my previous article:
private async Task<StreamReader> PostJsonStreamData(String URL) { HttpWebRequest wrGETURL = null; Stream objStream = null; StreamReader objReader = null;
try { wrGETURL = (HttpWebRequest)WebRequest.Create(URL); wrGETURL.Method = "POST"; wrGETURL.Credentials = new NetworkCredential("Administrator", "p@ssw0rd");
HttpWebResponse Response = (HttpWebResponse)(await wrGETURL.GetResponseAsync()); if (Response.StatusCode == HttpStatusCode.OK) { objStream = Response.GetResponseStream(); objReader = new StreamReader(objStream); } } catch (Exception e) { System.Diagnostics.Debug.WriteLine("GetData " + e.Message); } return objReader; }
Copyright © 2015 – Bruce Eitman and Embedded101.com All Rights Reserved
Re: Windows 10 IoT Core: Shutdown and Reboot the Raspberry Pi
Bruce, I noticed that you are sending a REST web request for controlling the pi. I looked online a for...
-- Waleed Elmandouh
Re: Windows 10 IoT Core: Getting the MAC Address from Raspberry Pi
Hi. This no longer works for me in the iot-core custom OEM-build/FFU. They have changed the access levels...
-- GG
Hi, since I am having trouble with this intervace in an OEM-custom Image/FFU scenario, I wonder, how...
I like your project here, and I've used it, and at one time it worked, at least with an older version...
-- integral
Nifty, Bruce!
-- David Jones
Hi Bruce, Good to know you are playing with Windows 10 IoT, and thanks for sharing these great finding...
-- Samuel Phung