Introduction

With the ever increasing number of API interfaces becoming available to allow us to utilize data from the external platforms into our Web or software Applications, I decided to create a simple but very useful helper class that will allow for the consumption of any XML or JSON request for deserialization into a class object of your choosing.

ApiWebRequestHelper Methods

As you can see from the code, given below, ApiRequestHelper class contains two methods:
  • GetJsonRequest()
  • GetXmlRequest()
These methods will allow you to pass an unknown type as well as the URL to API endpoint, where you are getting your data from. This makes things very straight-forward, when you want to strongly-type the data with ease.
  1. public class ApiWebRequestHelper
  2. {
  3. /// <summary>
  4. /// Gets a request from an external JSON formatted API and returns a deserialized object of data.
  5. /// </summary>
  6. /// <typeparam name="T"></typeparam>
  7. /// <param name="requestUrl"></param>
  8. /// <returns></returns>
  9. public static T GetJsonRequest<T>(string requestUrl)
  10. {
  11. try
  12. {
  13. WebRequest apiRequest = WebRequest.Create(requestUrl);
  14. HttpWebResponse apiResponse = (HttpWebResponse)apiRequest.GetResponse();
  15. if (apiResponse.StatusCode == HttpStatusCode.OK)
  16. {
  17. string jsonOutput;
  18. using (StreamReader sr = new StreamReader(apiResponse.GetResponseStream()))
  19. jsonOutput = sr.ReadToEnd();
  20. var jsResult = JsonConvert.DeserializeObject<T>(jsonOutput);
  21. if (jsResult != null)
  22. return jsResult;
  23. else
  24. return default(T);
  25. }
  26. else
  27. {
  28. return default(T);
  29. }
  30. }
  31. catch (Exception ex)
  32. {
  33. // Log error here.
  34. return default(T);
  35. }
  36. }
  37. /// <summary>
  38. /// Gets a request from an external XML formatted API and returns a deserialized object of data.
  39. /// </summary>
  40. /// <typeparam name="T"></typeparam>
  41. /// <param name="requestUrl"></param>
  42. /// <returns></returns>
  43. public static T GetXmlRequest<T>(string requestUrl)
  44. {
  45. try
  46. {
  47. WebRequest apiRequest = WebRequest.Create(requestUrl);
  48. HttpWebResponse apiResponse = (HttpWebResponse)apiRequest.GetResponse();
  49. if (apiResponse.StatusCode == HttpStatusCode.OK)
  50. {
  51. string xmlOutput;
  52. using (StreamReader sr = new StreamReader(apiResponse.GetResponseStream()))
  53. xmlOutput = sr.ReadToEnd();
  54. XmlSerializer xmlSerialize = new XmlSerializer(typeof(T));
  55. var xmlResult = (T)xmlSerialize.Deserialize(new StringReader(xmlOutput));
  56. if (xmlResult != null)
  57. return xmlResult;
  58. else
  59. return default(T);
  60. }
  61. else
  62. {
  63. return default(T);
  64. }
  65. }
  66. catch (Exception ex)
  67. {
  68. // Log error here.
  69. return default(T);
  70. }
  71. }
  72. }
ApiWebRequestHelper class relies on the following namespaces:
  • Newtonsoft Json
  • System.Xml.Serialization
  • ​​System.IO
ApiWebRequestHelper can be used in the following way:
  1. // Get Json Request
  2. ApiWebRequestHelper.GetJsonRequest<MyJsonClassObject>("http://www.c-sharpcorner.com/api/result.json");
  3. // Get XML Request
  4. ApiWebRequestHelper.GetXmlRequest<MyXMLClassObject>("http://www.c-sharpcorner.com/api/result.xml");