C# Examples

A Simple GET Request

In the following example, we will use a C# Console application and System.Net.HttpWebRequest to get information about a particular product.


    // "GET" request to Get a Product by ID
    Console.WriteLine("Get Product By ID");
    try
    {
        //set up request
        string url = "https://services.expertoms.com/api/product/id/12345";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "GET";
        request.Headers.Add(HttpRequestHeader.Authorization, "Basic username:password");
        request.Accept = "application/json";

        //get response
        HttpWebResponse response = request.GetResponse() as HttpWebResponse;
        using (StreamReader respReader = new StreamReader(response.GetResponseStream()))
        {
            Console.WriteLine(respReader.ReadToEnd());
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error: " + ex.Message);
    }
    Console.WriteLine("END Get Product By ID");
        

A couple things to notice here:

  • GET commands simply call a URL. There is no need to send additional data. Depending on the method, the argument would simply be the last part of the URL. If a method requires more arguments, they will be appended as query strings at the end of the url
  • Remember to set the request.Accept as "application/json" if you want to have JSON returned from the service. By default, the service will return XML. If you would prefer to hav XML returned, then set dataType to "application/xml".
  • All requests MUST be authenticated. Use request.Headers.Add(HttpRequestHeader.Authorization, "Basic username:password");

A Simple POST Request

In the following example, we will use a C# Console application and System.Net.HttpWebRequest to post an array of product IDs and recieve an array with Inventory information about those products.


    // "POST" request to return inventory for an array of product IDs
    Console.WriteLine("Get Inventory by ProductIDs");
    try
    {
        //set up request
        string url = "https://services.expertoms.com/api/product/inventorybyid";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "POST";
        request.ContentType = "application/json";
        string postData = "idType=DealerID&userID=0&productIDs=1234&productIDs=5678";
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = postData.Length;
        request.Headers.Add(HttpRequestHeader.Authorization, "Basic username:password");
        request.Accept = "application/json";

        //create request stream
        using (Stream dataStream = request.GetRequestStream())
        {
            dataStream.Write(byteArray, 0, byteArray.Length);
        }

        //get response
        HttpWebResponse response = request.GetResponse() as HttpWebResponse;
        using (StreamReader respReader = new StreamReader(response.GetResponseStream()))
        {
            Console.WriteLine(respReader.ReadToEnd());
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error: " + ex.Message);
    }
    Console.WriteLine("END Get Inventory by ProductIDs");
        

Some special considerations when using POST:

  • When sending arrays as post data, add a querystring with the same name for each member of the array. For example, the proper syntax would be "name=value1&name=value2&name-value3...."
  • All POST calls must have request.Method set to "POST"
  • The appropriate ContentType for a POST call is "application/x-www-form-urlencoded".
  • Don't forget to authenticate!