PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/NetLib/RESTProcessor.cs

#
C# | 298 lines | 193 code | 76 blank | 29 comment | 11 complexity | 824e68a15b4162bda1b62074c2812998 MD5 | raw file
  1. /*
  2. * This code contained within HttpPost and HttpGet came
  3. * from Dr. M Elkstein's blog, at
  4. * http://rest.elkstein.org/2008/02/using-rest-in-c-sharp.html
  5. *
  6. * No copywrite was stated on the page.
  7. * Crash (Joshua) Collison added the second overload of HttpPost,
  8. * "public static string HttpPost(string url, string prebuiltParams)", and
  9. * modified the first HttpPost to include the optional argument "string extras".
  10. */
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Net;
  16. using System.Web;
  17. using System.IO;
  18. using Newtonsoft.Json;
  19. using System.Diagnostics;
  20. namespace NetLib
  21. {
  22. public static class RESTProcessor
  23. {
  24. #region Data and Properties
  25. #endregion //Data and Properties
  26. #region Contstructors
  27. //public RESTProcessor()
  28. //{
  29. //}
  30. #endregion //Contstructors
  31. #region Methods
  32. public static string HttpPost(string url, string[] paramNames, string[] paramValues, string extras = null)
  33. {
  34. //http://rest.elkstein.org/2008/02/using-rest-in-c-sharp.html
  35. string result;
  36. HttpWebRequest request = WebRequest.Create(new Uri(url)) as HttpWebRequest;
  37. request.Method = "POST";
  38. request.ContentType = "application/x-www-form-urlencoded";
  39. //build parameters string
  40. StringBuilder parameters = new StringBuilder();
  41. for (int i = 0; i < paramNames.Length; i++)
  42. {
  43. parameters.Append(paramNames[i]);
  44. parameters.Append("=");
  45. parameters.Append(HttpUtility.UrlEncode(paramValues[i]));
  46. parameters.Append("&");
  47. }
  48. if (extras != null)
  49. {
  50. parameters.Append((extras));
  51. }
  52. //convert parameter string to bytes
  53. byte[] formData = UTF8Encoding.UTF8.GetBytes(parameters.ToString());
  54. request.ContentLength = formData.Length;
  55. //send the request
  56. using (Stream post = request.GetRequestStream())
  57. {
  58. post.Write(formData, 0, formData.Length);
  59. }
  60. //Grab the responce
  61. using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
  62. {
  63. StreamReader reader = new StreamReader(response.GetResponseStream());
  64. result = reader.ReadToEnd();
  65. }
  66. return result;
  67. }
  68. public static string HttpPost(string url, string prebuiltParams)
  69. {
  70. string result;
  71. HttpWebRequest request = WebRequest.Create(new Uri(url)) as HttpWebRequest;
  72. request.Method = "POST";
  73. request.ContentType = "application/x-www-form-urlencoded";
  74. //build parameters string
  75. string parameters = prebuiltParams;// HttpUtility.UrlEncode(prebuiltParams);
  76. //convert parameter string to bytes
  77. byte[] formData = UTF8Encoding.UTF8.GetBytes(parameters);
  78. request.ContentLength = formData.Length;
  79. //send the request
  80. using (Stream post = request.GetRequestStream())
  81. {
  82. post.Write(formData, 0, formData.Length);
  83. }
  84. //Grab the response
  85. try
  86. {
  87. using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
  88. {
  89. StreamReader reader = new StreamReader(response.GetResponseStream());
  90. result = reader.ReadToEnd();
  91. }
  92. }
  93. catch (Exception ex)
  94. {
  95. Debug.WriteLine(ex.Message);
  96. throw ex;
  97. }
  98. return result;
  99. }
  100. public static string HttpPost(string url, List<string> header, bool useHeaderAuth)
  101. {
  102. string result;
  103. HttpWebRequest request = WebRequest.Create(new Uri(url)) as HttpWebRequest;
  104. request.Method = "POST";
  105. request.ContentType = "application/x-www-form-urlencoded";
  106. StringBuilder authorizationHeaderParams = new StringBuilder("OAuth ");
  107. //header.Sort();
  108. foreach (var item in header)
  109. {
  110. var param = item.Split('=');
  111. authorizationHeaderParams.Append(param[0]);
  112. authorizationHeaderParams.Append("=\"");
  113. authorizationHeaderParams.Append(Uri.EscapeDataString(param[1]));
  114. if (item != header.Last())
  115. {
  116. authorizationHeaderParams.Append("\",");
  117. }
  118. else
  119. {
  120. authorizationHeaderParams.Append("\"");
  121. }
  122. }
  123. request.Headers.Add("Authorization", authorizationHeaderParams.ToString());
  124. //convert parameter string to bytes
  125. byte[] formData = UTF8Encoding.UTF8.GetBytes(string.Empty);
  126. request.ContentLength = formData.Length;
  127. //send the request
  128. using (Stream post = request.GetRequestStream())
  129. {
  130. post.Write(formData, 0, formData.Length);
  131. }
  132. //Grab the response
  133. try
  134. {
  135. using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
  136. {
  137. StreamReader reader = new StreamReader(response.GetResponseStream());
  138. result = reader.ReadToEnd();
  139. }
  140. }
  141. catch (Exception ex)
  142. {
  143. Debug.WriteLine(ex.Message);
  144. throw ex;
  145. }
  146. return result;
  147. }
  148. public static string HttpGet(string url)
  149. {
  150. HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
  151. HttpWebResponse resp = null;
  152. string result = null;
  153. try
  154. {
  155. resp = (HttpWebResponse)req.GetResponse();
  156. StreamReader reader =
  157. new StreamReader(resp.GetResponseStream());
  158. result = reader.ReadToEnd();
  159. }
  160. catch (WebException ex)
  161. {
  162. //var response = (HttpWebResponse)ex.Response;
  163. Debug.WriteLine("### ERROR: " + ex.ToString() + " " + ex.Response);
  164. }
  165. finally
  166. {
  167. if (resp != null)
  168. {
  169. resp.Close();
  170. }
  171. }
  172. return result;
  173. }
  174. public static string HttpGet(string url, List<string> header)
  175. {
  176. HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
  177. HttpWebResponse resp = null;
  178. string result = null;
  179. StringBuilder authorizationHeaderParams = new StringBuilder(string.Format("OAuth realm=\"{0}\",",
  180. Uri.EscapeDataString("api.fitbit.com")));
  181. //header.Sort();
  182. foreach (var item in header)
  183. {
  184. var param = item.Split('=');
  185. authorizationHeaderParams.Append(param[0]);
  186. authorizationHeaderParams.Append("=\"");
  187. if (item != header.Last())
  188. {
  189. authorizationHeaderParams.Append(Uri.EscapeDataString(param[1]));
  190. authorizationHeaderParams.Append("\", ");
  191. }
  192. else
  193. {
  194. authorizationHeaderParams.Append(param[1]);
  195. authorizationHeaderParams.Append("=\"");
  196. }
  197. }
  198. req.Headers.Add("Authorization", authorizationHeaderParams.ToString());
  199. try
  200. {
  201. resp = (HttpWebResponse)req.GetResponse();
  202. StreamReader reader =
  203. new StreamReader(resp.GetResponseStream());
  204. result = reader.ReadToEnd();
  205. }
  206. catch (WebException ex)
  207. {
  208. //var response = (HttpWebResponse)ex.Response;
  209. Debug.WriteLine("### ERROR: " + ex.ToString() + " " + ex.Response);
  210. }
  211. finally
  212. {
  213. if (resp != null)
  214. {
  215. resp.Close();
  216. }
  217. }
  218. return result;
  219. }
  220. #endregion //Methods
  221. }
  222. }