Calling a service method example

First you will need to construct an object to hold your data

public class EbookFeedCountInfo
 {
 public int ErrorCode { get; set; }
 public string ErrorMessage { get; set; }
 public int Count { get; set; }
 }

Next will be to construct your class and Static method

 
// Main usings
using System.Web;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;

public class OnTheDotController
 {
 #region Private Constant Members

 private const string API_KEY = "YOUR_API_KEY";
 private const string API_PASSWORD = "PASSWORD";
 private const string URI = "YourUrl.com/v1"//Example: "http://retailer.api.staging.otd.co.za/v1";

 #endregion

 #region Service Methods

public static EbookFeedCountInfo GetFeedCount()
 {
 // Build URL
 string url = string.Format("{0}/CountBookFeed/{1}/{2}", URI, API_KEY, API_PASSWORD);
 
 // Create request
 var request = WebRequest.Create(url) as HttpWebRequest;
 request.Method = "GET";

 // Get response
 using (var response = request.GetResponse() as HttpWebResponse)
 {
 // Get response data
 Stream responseStream = response.GetResponseStream();
 var oStreamReader = new StreamReader(responseStream);

 // Deserialize into Object
 var oSerialiser = new JavaScriptSerializer();
 string data = oStreamReader.ReadToEnd();

 // Return hydrated object
 return oSerialiser.Deserialize<EbookFeedCountInfo>(data);
 }
 } 
#endregion
}

Lastly, call your method

int feedCount = OnTheDotController.GetFeedCount();

Comment below if you have any questions. This very basic implementation taken from code I recently have to write, the parameters and pattern may differ from other services.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s