Skip to content

A Simple C# Hello World Example

As with any new technology, the first thing to do is to create a Hello World! application to get to grips with the basics.

This assumes the following:

  • ColonyCam is connected to the same network as the development machine.
  • API Mode is enabled.
  • You have a note of the server address from the API Mode of the ColonyCam Software.

Create a new Console Application in Visual Studio.

The following code should be written in the Main method of the Program.cs class.

First we'll need a HTTPClient to send the requests.

var client = new HttpClient();

Clear any pre-existing request headers from the client.

client.DefaultRequestHeaders.Clear();

Add an accept header to the client and give it the value "text/plain" or "octet-stream" if you are expecting an image from the endpoint.

client.DefaultRequestHeaders.Add("Accept", "text/plain");

We will also need the address of the ColonyCam, prefixed with "http://" and suffixed with a forward slash and the name of the endpoint. For the purposes of this example, we will use the address 192.168.1.0:8080 and invoke the initialise endpoint.

var address = "http://192.168.1.0:8080/v1/initialise";

Send an asynchronous HTTP GET request to the address and wait for the response.

var response = await client.GetAsync(address);

Print the status code to the console.

Console.WriteLine($"Initialise status code: {response.StatusCode}");

Print the response contents to the console.

Console.WriteLine($"Initialise result: {response.Content.ReadAsStringAsync().Result}");

The complete code should look like this

static async Task Main()
{
    var client = new HttpClient();
    var address = "http://127.0.0.1:8080/v1/initialise";
    client.DefaultRequestHeaders.Clear();
    client.DefaultRequestHeaders.Add("Accept", "text/plain");
    var response = await client.GetAsync(address);
    Console.WriteLine($"Initialise status code: {response.StatusCode}");
    Console.WriteLine($"Initialise result: {response.Content.ReadAsStringAsync().Result}");
}