RangerMSP REST
Below are examples of connecting to Jupiter Server RangerMSP methods via REST instead of SOAP
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);
}
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();