using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Net; using System.Net.Http; using System.Security.Cryptography.X509Certificates; using System.Text; using Newtonsoft.Json; namespace Sandbox { public class Sandbox { private static HttpClient _client; public static void Main(string[] args) { Uri uri = new Uri("https://dhmcesb1.gnb.ca:443/Medicare/MCE/api/WebServiceClaim"); string certFileName = "C:\\Projects\\MCE\\certs\\dhmcesb1\\MCE_DMZ_TEST_EMR_CERT.pfx"; string certPassword = "certPassword"; string claimFileName = "C:\\Projects\\MCE\\claims.txt"; // tsl setup ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; WebRequestHandler handler = new WebRequestHandler(); X509Certificate2 certificate = new X509Certificate2(certFileName, certPassword); handler.ClientCertificates.Add(certificate); ServicePointManager.ServerCertificateValidationCallback = X509CertificateValidationCallBack; _client = new HttpClient(handler); // default headers _client.DefaultRequestHeaders.Add("emr_id", "MCESandbox1"); _client.DefaultRequestHeaders.Add("token", "c36c2cf9-77c0-467f-a5cb-0ee17a03d575"); Process(uri, claimFileName); } private static void Process(Uri uri, string claimFileName) { // send each claim line as separate request IEnumerable claimFile = File.ReadLines(claimFileName); foreach (var claimRow in claimFile) { StringContent content = new StringContent(claimRow, Encoding.UTF8, "application/json"); HttpResponseMessage response = _client.PostAsync(uri, content).Result; // valid response from web service if (response.IsSuccessStatusCode) { string result = response.Content.ReadAsStringAsync().Result; string claimResult = JsonConvert.DeserializeObject(result).ToString(); Console.Out.Write(claimResult); Debug.Write(claimResult); } // non mce error response from web service else { Console.Out.Write(response.StatusCode); Debug.Write(response.StatusCode); } } } private static bool X509CertificateValidationCallBack(object sender, X509Certificate certificate, X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors) { // return true if cert is valid and signed (by authority) - this excludes self signed certs if (sslPolicyErrors == System.Net.Security.SslPolicyErrors.None) { return true; } // look through errors if ((sslPolicyErrors & System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors) != 0) { if (chain == null) return true; foreach (X509ChainStatus status in chain.ChainStatus) { if (certificate.Subject == certificate.Issuer && status.Status == X509ChainStatusFlags.UntrustedRoot) { // valid self signed cert } else { if (status.Status != X509ChainStatusFlags.NoError) { // all other errors are considered invalid return false; } } } return true; } return false; } } }