Harmony RightAddress c# example

This is an example of how to call Harmony RightAddress REST API using c#.

The following example invokes REST request for the address method with the single line address '220 George St' and the 'AUPAF' reference data set (Source of Truth):

			
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace HarmonyHostedClient
{
    class Program
    {
        // the end point for which environment to access
        private const string END_POINT 
                = "https://hosted.mastersoftgroup.com/harmony/rest/au/address";

        // Go to https://hosted.mastersoftgroup.com/console to get user/credential
        private const string USER = "user"; 
        private const string CREDENTIAL = "credential";

        // the reference data set (SOT): AUPAF, GNAF, and etc
        private const string SOT = "AUPAF";

        static void Main(string[] args)
        {
            string result = lookupAddress("220 George St");
            Console.WriteLine(result);
            System.Diagnostics.Debug.WriteLine(result);
        }

        static string lookupAddress(string escapedInput)
        {
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(END_POINT);
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = "POST";

            // authentication
            string encoded = Convert.ToBase64String(
                    Encoding.GetEncoding("ISO-8859-1").GetBytes(USER + ":" + CREDENTIAL));
            httpWebRequest.Headers.Add("Authorization", "Basic " + encoded);

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                // make sure input is escaped
                string json = "{ \"payload\": [{ \"fullAddress\": \"" +escapedInput+ "\"}], " +
                                  " \"sourceOfTruth\": \""+SOT+"\"}";

                streamWriter.Write(json);
            }

            // may need to handle exceptions
            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var responseText = streamReader.ReadToEnd();
                return responseText;
            }
        }
    }
}

See the Harmony RightAddress API documentation for details on invoking the other Harmony RightAddress service methods.

Last updated