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:

    •         1. The URL, in the case of starting packages it is “api/taskmanager/start”
    •          2. How to POST the app to start.  We do this with the parameter “appid” which requires a Base 64 string
    •         3. The name of the app – surprisingly this was the most challenging.  This required that we get the Relative Package ID, which is a very odd name for the app, not a user friendly name.

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