For a more in depth and other programming language samples, download the Sample Client Install

C# Copy imageCopy
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Net.Http;
using System.Text;

namespace FaxWSConsoleApplication
{
    class CheckServiceJsonClass
    {
        const string faxWSUrl = "https://ws.concordfax.com/fax/v5";

        public void CheckServiceJson()
        {
            string methodName = "CheckService";

            Console.WriteLine($"Starting call to {methodName}");

            #region General Settings

            #endregion General Settings    

            try
            {
                #region Request Setup

                // Sample Request
                //{
                //    "CheckService": {
                //    }
                //}

                // Create JSON request
                var json = new 
                {
                    CheckService = new {

                    }
                };

                // Serialize to JSON string {MethodName:{}}
                string content = JsonConvert.SerializeObject(json);

                // Create HttpContent object from  JSON content and set content-type to application/json
                HttpContent httpContent = new StringContent(content, Encoding.UTF8, "application/json");

                #endregion Request Setup

                #region API Call

                using(var client = new HttpClient())
                {
                    // Clear any existing headers
                    client.DefaultRequestHeaders.Accept.Clear();

                    // Add Content-Type: application/json
                    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

                    Console.WriteLine($"Making API Call to {methodName}");

                    // Sent POST request
                    HttpResponseMessage httpResponseMessage = client.PostAsync(new Uri(faxWSUrl), httpContent).GetAwaiter().GetResult();

                    Console.WriteLine("Reading API Call Response content");

                    // Get JSON body content from response
                    string responseContent = httpResponseMessage.Content.ReadAsStringAsync().GetAwaiter().GetResult();

                    // Check to see if Http Status Code was successful
                    if(!httpResponseMessage.IsSuccessStatusCode)
                    {
                        Console.WriteLine($"Http Error Returned, Status Code: {httpResponseMessage.StatusCode}, Reason Phrase: {httpResponseMessage.ReasonPhrase}");
                        Console.WriteLine($"Response Content:{responseContent}");

                        return;
                    }

                    JObject jsonResult = null;

                    try
                    {
                        // Parse JSON string to Json Object that will help parse response
                        jsonResult = JObject.Parse(responseContent);
                    }
                    catch(JsonReaderException ex)
                    {
                        Console.WriteLine(ex.Message);
                    }

                    // Write JSON response out
                    Console.WriteLine($"Json Response: {jsonResult.ToString()}");

                    // SAMPLE RESPONSE
                    //{
                    //    "CheckServiceResponse": {
                    //        "return": ""
                    //    }
                    //}

                    // Get response portion
                    // Serialize to JSON string {CancelFaxResponse:{}}
                    var response = jsonResult["CheckServiceResponse"];

                    // Get API call status
                    string responseString = response["return"].Value<string>();

                    // API Call was successful, echo back JobId that was cancelled
                    Console.WriteLine($"Successfully returned checkService: {responseString}");
                }

                #endregion API Call

            }
            catch(Exception ex)
            {
                Console.WriteLine($"Error while calling {methodName} : {ex.Message}");
            }
            finally
            {
                Console.WriteLine($"Finished call to {methodName}");
            }
        }
    }
}

See Also