RangerMSP REST

Below are examples of connecting to Jupiter Server RangerMSP methods via REST instead of SOAP

Example of connecting to SelectAccount using REST without SSL or Authentication. Note the service endpoint is now RangerMSP_REST.
string jsonReq = new JavaScriptSerializer().Serialize(selectAccount);            
HttpWebRequest request = (HttpWebRequest)WebRequest.Create($"http://localhost:11111/RangerMSP_REST/SelectAccount?select={jsonReq}");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader readerStream = new StreamReader(responseStream, System.Text.Encoding.GetEncoding("utf-8"));

string jsonRes = readerStream.ReadToEnd();
string errstr = "";
try
{
     AccountRecord[] accRecs = new AccountRecord[0];
     accRecs = JsonConvert.DeserializeObject<AccountRecord[]>(jsonRes);
     if (accRecs.Length > 0)
     {
          acctREC_ID = accRecs[0].AccountREC_ID;
          testResult = "PASS";
     }
}
catch (Exception e)
{
      errstr = e.ToString();
}          

To connect to Jupiter Server via REST with SSL, we need two subroutines to assist. The first one, AcceptAllCertificates, were found here https://stackoverflow.com/questions/5713071/problem-ssl-certificate-c-sharp , and the second, GetAuthHeader, was found here https://blog.tonysneed.com/2012/05/28/secure-wcf-rest-services-with-a-custom-usernamepasswordvalidator/

AcceptAllCertificates is only needed to support SelfSigned SSL certificates. If Jupiter Server is configured with a CA certificate then the AcceptAllCertificates subroutine should not be needed. 

 

public static bool AcceptAllCertifications(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{
     // Refer https://stackoverflow.com/questions/5713071/problem-ssl-certificate-c-sharp
     return true;
}
private static string GetAuthHeader(string userName, string password)
{
     // Refer https://blog.tonysneed.com/2012/05/28/secure-wcf-rest-services-with-a-custom-usernamepasswordvalidator/
     string userNamePassword = Convert.ToBase64String(new UTF8Encoding().GetBytes(string.Format("{0}:{1}", userName, password)));
     return string.Format("{0}", userNamePassword);
}
Using those two subroutines above, we can now connect to Jupiter Server RangerMSP using REST. Assumes the SSL port is set to 22222 and that we have declared and setup the variables username and password
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
string jsonReq = JsonConvert.SerializeObject(select);            
HttpWebRequest request = (HttpWebRequest)WebRequest.Create($"https://localhost:22222/RangerMSP_REST/SelectAccount?select={jsonReq}");
request.Headers.Add("Authorization", "Basic " + GetAuthHeader(username, password));
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();