0 Replies Latest reply on Aug 20, 2015 8:29 PM by andylo

    Need some API help on C# please

    andylo

      Hi,

       

      I am a C# developer looking to import client information from our database to WHD.

       

      I am reading through the API document but I have hit the wall with my first attempt.

       

      My ideal design was:

       

      1) Login using API key and get a session key (not sure if that is the correct way?)

      2) Using the session key to get the all client list, put it in a DataTable object

      3) Loop through the client list DataTable and compare to the our database

      4) Per record basis, if not found in WHD client list, Insert! If found and detail change, Update! If found but identical, leave it.

      5) Once done, close the session and finish.

       

      It is a process we plan to run every midnight.

       

      I have created a WinForm app to do this (instead of opt for Console app), so it would be easier for me to add more features to it in the future when needed.

       

      Long story short, I can get a session key. But when I want to use other API call with the session key, it always return a 401 error (Unauthorised access?)

       

      It would be really nice I can get some tips and help or even sample code to assist me ASAP.

       

      So far my code are as following (please assuming it is all in Form_Load() event)

       

      string baseAddress = "http://OurAddres:<PortNumber>/helpdesk/WebObjects/Helpdesk.woa";

      string strAPIKey = "Our API Key";

      string mySessionKey;

      string myResult;

       

      strAuthenticationInfo = HttpGet(baseAddress + @"\/ra/Session\?username=admin&apiKey=" + strAPIKey); <-- that worked

      mySessionKey = Regex.Match(strAuthenticationInfo, @"sessionKey"":""(.+)"",""currentTechId""", RegexOptions.Singleline).Groups[1].Value; <-- that worked too

      myResult = HttpGet(baseAddress + @"\/ra/Assets/?style=short\?sessionKey=" + mySessionKey ) <-- for now I just want to see the Asset list just to test the water, but this one got me the 401 error

       

      For your information, the HttpGet function looks like the following:

      static string HttpGet(string url)

              {

                  HttpWebRequest req = WebRequest.Create(url)

                                       as HttpWebRequest;

                  string result = null;

                  using (HttpWebResponse resp = req.GetResponse()

                                                as HttpWebResponse)

                  {

                      StreamReader reader =

                          new StreamReader(resp.GetResponseStream());

                      result = reader.ReadToEnd();

                  }

                  return result;

              }

       

       

      Really looking forward to your reply.


      Kind regards,

      Andy