PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/Code/Platform/Framework/Web/HttpServiceClient.cs

http://github.com/waseems/inbox2_desktop
C# | 267 lines | 209 code | 56 blank | 2 comment | 19 complexity | 88064557a2a0cfbe920047545360acc3 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Security.Cryptography.X509Certificates;
  7. using System.Text;
  8. using System.Web;
  9. using Inbox2.Platform.Channels.Text;
  10. using Inbox2.Platform.Framework.Extensions;
  11. using Inbox2.Platform.Framework.Web.Upload;
  12. using Inbox2.Platform.Logging;
  13. namespace Inbox2.Platform.Framework.Web
  14. {
  15. public static class HttpServiceRequest
  16. {
  17. public static string FormUrlEncoded = "application/x-www-form-urlencoded";
  18. public static string Get(string url)
  19. {
  20. return Get(url, false);
  21. }
  22. public static string Get(string url, bool throwOnError)
  23. {
  24. int statusCode;
  25. return Get(url, throwOnError, out statusCode);
  26. }
  27. public static string Get(string url, bool throwOnError, out int statusCode)
  28. {
  29. try
  30. {
  31. var request = (HttpWebRequest)WebRequest.Create(url);
  32. AttachCertificate(request);
  33. var response = (HttpWebResponse)request.GetResponse();
  34. statusCode = (int)response.StatusCode;
  35. using (var result = response.GetResponseStream())
  36. return result.ReadString();
  37. }
  38. catch (Exception ex)
  39. {
  40. statusCode = -1;
  41. Logger.Error("An error has occured while calling url. url = {0} Exception = {1}", LogSource.ServiceCall, url, ex);
  42. if (ex is WebException)
  43. {
  44. var wex = (WebException) ex;
  45. var response = (HttpWebResponse) wex.Response;
  46. if (response != null)
  47. statusCode = (int)response.StatusCode;
  48. }
  49. if (throwOnError)
  50. throw;
  51. return null;
  52. }
  53. }
  54. public static Stream GetStream(string url)
  55. {
  56. return GetStream(url, false);
  57. }
  58. public static Stream GetStream(string url, bool throwOnError)
  59. {
  60. try
  61. {
  62. var request = (HttpWebRequest)WebRequest.Create(url);
  63. AttachCertificate(request);
  64. var response = request.GetResponse();
  65. return response.GetResponseStream();
  66. }
  67. catch (Exception ex)
  68. {
  69. Logger.Error("An error has occured while calling url. url = {0} Exception = {1}", LogSource.ServiceCall, url, ex);
  70. if (throwOnError)
  71. throw;
  72. return null;
  73. }
  74. }
  75. public static string Post(string url, string data)
  76. {
  77. return Post(url, data, FormUrlEncoded);
  78. }
  79. public static string Post(string url, string data, bool throwOnError)
  80. {
  81. return Post(url, data, FormUrlEncoded, throwOnError);
  82. }
  83. public static string Post(string url, string data, string contentType)
  84. {
  85. return Post(url, data, contentType, false);
  86. }
  87. public static string Post(string url, string data, string contentType, bool throwOnError)
  88. {
  89. return Send("POST", url, data, contentType, throwOnError);
  90. }
  91. public static string Put(string url, string data)
  92. {
  93. return Put(url, data, FormUrlEncoded);
  94. }
  95. public static string Put(string url, string data, bool throwOnError)
  96. {
  97. return Put(url, data, FormUrlEncoded, throwOnError);
  98. }
  99. public static string Put(string url, string data, string contentType)
  100. {
  101. return Put(url, data, contentType, false);
  102. }
  103. public static string Put(string url, string data, string contentType, bool throwOnError)
  104. {
  105. return Send("PUT", url, data, contentType, throwOnError);
  106. }
  107. internal static string Send(string method, string url, string data, string contentType, bool throwOnError)
  108. {
  109. try
  110. {
  111. var uri = new Uri(url);
  112. var request = (HttpWebRequest)WebRequest.Create(
  113. String.Format("{0}://{1}:{2}{3}", uri.Scheme, uri.Host, uri.Port, uri.AbsolutePath));
  114. AttachCertificate(request);
  115. request.Method = method;
  116. request.ContentType = contentType;
  117. byte[] bytes = Encoding.UTF8.GetBytes(data);
  118. request.ContentLength = bytes.Length;
  119. // Write post data to request stream
  120. using (Stream requestStream = request.GetRequestStream())
  121. requestStream.Write(bytes, 0, bytes.Length);
  122. using (var response = request.GetResponse())
  123. using (var result = response.GetResponseStream())
  124. return result.ReadString();
  125. }
  126. catch (Exception ex)
  127. {
  128. string message = String.Empty;
  129. if (ex is WebException)
  130. {
  131. var wex = (WebException)ex;
  132. var response = (HttpWebResponse)wex.Response;
  133. if (response != null)
  134. {
  135. using (var stream = response.GetResponseStream())
  136. message = stream.ReadString();
  137. }
  138. }
  139. Logger.Error("An error has occured while calling url. Url = {0}, Data = {1}, Exception = {2}, Message = {3}", LogSource.ServiceCall, url, data, ex, message);
  140. if (throwOnError)
  141. throw;
  142. return null;
  143. }
  144. }
  145. public static string Post(string url, string data, List<UploadFile> files)
  146. {
  147. return Post(url, data, files, false);
  148. }
  149. public static string Post(string url, string data, List<UploadFile> files, bool throwOnError)
  150. {
  151. try
  152. {
  153. var uri = new Uri(url);
  154. var request = (HttpWebRequest)WebRequest.Create(
  155. String.Format("{0}://{1}:{2}{3}", uri.Scheme, uri.Host, uri.Port, uri.AbsolutePath));
  156. AttachCertificate(request);
  157. using (var response = HttpUploadHelper.Upload(request, files.ToArray(), NameValueParser.GetCollection(data, "&")))
  158. using (var result = response.GetResponseStream())
  159. return result.ReadString();
  160. }
  161. catch (Exception ex)
  162. {
  163. string message = String.Empty;
  164. if (ex is WebException)
  165. {
  166. var wex = (WebException)ex;
  167. var response = (HttpWebResponse)wex.Response;
  168. if (response != null)
  169. {
  170. using (var stream = response.GetResponseStream())
  171. message = stream.ReadString();
  172. }
  173. }
  174. Logger.Error("An error has occured while calling url. Url = {0}, Data = {1}, Exception = {2}, Message = {3}", LogSource.ServiceCall, url, data, ex, message);
  175. if (throwOnError)
  176. throw;
  177. return null;
  178. }
  179. }
  180. public static string GetServiceUrl()
  181. {
  182. var env = "/Settings/Application/Environment".AsKey(String.Empty);
  183. var scheme = String.IsNullOrEmpty(env) ? "https://" : "http://";
  184. // When request signing is disabled, always use http
  185. if ("/Settings/Application/DisableServiceRequestSigning".AsKey(false))
  186. scheme = "http://";
  187. if (String.IsNullOrEmpty(env))
  188. return scheme + "services.inbox2.com";
  189. else
  190. return String.Format(scheme + "services.{0}.inbox2.com", env);
  191. }
  192. static void AttachCertificate(HttpWebRequest request)
  193. {
  194. var env = "/Settings/Application/Environment".AsKey(String.Empty);
  195. var disable = "/Settings/Application/DisableServiceRequestSigning".AsKey(false);
  196. if (env == String.Empty && disable == false)
  197. {
  198. X509Certificate2 cert = GetCertificate();
  199. request.ClientCertificates.Add(cert);
  200. }
  201. }
  202. public static X509Certificate2 GetCertificate()
  203. {
  204. X509Store certStore = new X509Store(StoreName.My, StoreLocation.LocalMachine);
  205. certStore.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
  206. X509Certificate2 cert = certStore.Certificates.Find(X509FindType.FindByIssuerName, "Inbox2.com CA", false)[0];
  207. certStore.Close();
  208. return cert;
  209. }
  210. }
  211. }