The following code examples show two different methods of posting a request to the Hash Auger API using C#. Both examples post the CommandsAllowed command to the HA API service listening at port 4059 of the localhost. However, one example uses a .Net HttpClient object while the other uses a HttpWebRequest object instead. Although both examples are written in C#, they are relatively straightforward to translate to VB .Net.

httpclient example

 using (HttpClient client = new HttpClient())
 {
     var response = string.Empty;

     using (StringContent content = new StringContent("{\"command\": \"commandsallowed\",\"key\":\"mykey\" }", Encoding.UTF8, "application/json"))
     {
         content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
         using (HttpResponseMessage result = await client.PostAsync("http://localhost:4059/api", content))
         {
             if (result != null)
                            response = await result.Content.ReadAsStringAsync();
         }
     }
 }

httpwebrequest example

 var request = (HttpWebRequest)WebRequest.Create("http://localhost:4059/api");
 request.ContentType = "application/json";
 byte[] byteArray = Encoding.UTF8.GetBytes("{\"command\": \"commandsallowed\",\"key\":\"testme\" }");
 request.Method = "POST";

 using (System.IO.Stream dataStream = request.GetRequestStream())
 {
     // Write the data to the request stream.  
     dataStream.Write(byteArray, 0, byteArray.Length);  
     dataStream.Close();

     using (var response = await request.GetResponseAsync())
     {
          if (response != null)
          {
               if ((response as HttpWebResponse).StatusCode == HttpStatusCode.OK)
               {
                   using (var sr = new System.IO.StreamReader(response.GetResponseStream()))
                   {
                        string result = sr.ReadToEnd();
                   }
               }
          }
     }