BaseHttpBll.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Http;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using Com.FirmLib.Common;
  9. using FirmHttpDao;
  10. using FWindSoft;
  11. using FWindSoft.Net.Http;
  12. using Newtonsoft.Json.Linq;
  13. namespace Com.FirmLib
  14. {
  15. /// <summary>
  16. /// 基础逻辑操作类
  17. /// </summary>
  18. public class BaseHttpBll
  19. {
  20. protected string m_ExtensionUri;
  21. protected HttpClient m_HttpClient;
  22. #region 错误机制
  23. /// <summary>
  24. /// 当前错误信息
  25. /// </summary>
  26. public string ErrorMessage { get; set; }
  27. #endregion
  28. #region 地址信息相关
  29. /// <summary>
  30. /// 获取基础地址
  31. /// </summary>
  32. /// <returns></returns>
  33. protected virtual string BaseEndUri()
  34. {
  35. return BllHttpSetting.Instance.EndUri;
  36. }
  37. /// <summary>
  38. /// 获取创建组
  39. /// </summary>
  40. /// <returns></returns>
  41. protected virtual string CreateKey()
  42. {
  43. return BllHttpSetting.Instance.InsertKey();
  44. }
  45. /// <summary>
  46. /// 获取删除组
  47. /// </summary>
  48. /// <returns></returns>
  49. protected virtual string DeleteKey()
  50. {
  51. return BllHttpSetting.Instance.DeleteKey();
  52. }
  53. /// <summary>
  54. /// 获取更新组
  55. /// </summary>
  56. /// <returns></returns>
  57. protected virtual string UpdateKey()
  58. {
  59. return BllHttpSetting.Instance.UpdateKey();
  60. }
  61. /// <summary>
  62. /// 获取查询组
  63. /// </summary>
  64. /// <returns></returns>
  65. protected virtual string QueryKey()
  66. {
  67. return BllHttpSetting.Instance.SearchKey();
  68. }
  69. #endregion
  70. public BaseHttpBll(string extensionUri)
  71. {
  72. Init(extensionUri);
  73. }
  74. protected void Init(string extensionUri)
  75. {
  76. this.m_ExtensionUri = extensionUri;
  77. }
  78. //protected void HandlerError(Task<HttpResponseMessage> result)
  79. //{
  80. // HandlerError(result.Result);
  81. //}
  82. protected virtual HttpClient CreateHttpClient()
  83. {
  84. //if (m_HttpClient != null)
  85. // return m_HttpClient;
  86. //return m_HttpClient = HttpClientManager.CreateClient();// new HttpClient() { BaseAddress = new Uri(GetEndUri()) };
  87. return HttpClientManager.CreateClient();
  88. }
  89. /// <summary>
  90. /// 获取操作的api
  91. /// </summary>
  92. /// <returns></returns>
  93. protected virtual string GetEndUri()
  94. {
  95. return this.BaseEndUri() + this.m_ExtensionUri;
  96. }
  97. #region 捕获业务处理返回消息
  98. protected void CatchErrorMessage(JObject jobject)
  99. {
  100. PostResult p = PostResult.CreatePostResult(jobject);
  101. this.ErrorMessage = p.ResultMessage;
  102. }
  103. #endregion
  104. #region 基础命令封装
  105. public string PostJsonAsync(Uri requeUri, JObject jobject)
  106. {
  107. RequestInfo request = new RequestInfo(requeUri.ToString(), jobject);
  108. try
  109. {
  110. #region 发送请求
  111. var client = CreateHttpClient();
  112. var response = client.PostJosnAsync(requeUri, jobject);
  113. #endregion
  114. return HandlerTaskResponse(response,request);
  115. }
  116. catch (Exception e)
  117. {
  118. throw;
  119. }
  120. }
  121. public string PostJsonAdd(JObject jobject)
  122. {
  123. return PostJsonAsync(new Uri(GetEndUri() + CreateKey(), UriKind.RelativeOrAbsolute), jobject);
  124. }
  125. public string PostJsonUpdate(JObject jobject)
  126. {
  127. return PostJsonAsync(new Uri(GetEndUri() + UpdateKey(), UriKind.RelativeOrAbsolute), jobject);
  128. }
  129. public string PostJsonDelete(JObject jobject)
  130. {
  131. return PostJsonAsync(new Uri(GetEndUri() + DeleteKey(), UriKind.RelativeOrAbsolute), jobject);
  132. }
  133. public string PostJsonQuery(JObject jobject)
  134. {
  135. return PostJsonAsync(new Uri(GetEndUri() + QueryKey(), UriKind.RelativeOrAbsolute), jobject);
  136. }
  137. public string PostJsonQuery()
  138. {
  139. return PostJsonAsync(new Uri(GetEndUri() + QueryKey(), UriKind.RelativeOrAbsolute), new JObject());
  140. }
  141. #endregion
  142. #region 业务结果处理
  143. /// <summary>
  144. /// 业务结果处理
  145. /// </summary>
  146. /// <param name="responseResult"></param>
  147. /// <param name="success"></param>
  148. /// <param name="fail"></param>
  149. /// <returns></returns>
  150. public static bool HandlerResult(string responseResult, Action<JObject> success, Action<JObject> fail)
  151. {
  152. JObject result = JObject.Parse(responseResult);
  153. PostResult p = PostResult.CreatePostResult(result);
  154. if (p.IsSuccess)
  155. {
  156. success?.Invoke(result);
  157. return true;
  158. }
  159. fail?.Invoke(result);
  160. return false;
  161. }
  162. public static bool HandlerResult(string responseResult, Action<JObject> success)
  163. {
  164. return HandlerResult(responseResult, success, null);
  165. }
  166. public static bool HandlerResult(string responseResult)
  167. {
  168. return HandlerResult(responseResult, null, null);
  169. }
  170. #endregion
  171. #region 网络异常错误处理
  172. /// <summary>
  173. /// 处理错误返回消息
  174. /// </summary>
  175. /// <param name="responseMessage"></param>
  176. /// <param name="requestInfo"></param>
  177. public static void HandlerError(HttpResponseMessage responseMessage, RequestInfo requestInfo)
  178. {
  179. if (responseMessage.StatusCode != HttpStatusCode.OK)
  180. {
  181. StringBuilder builder = new StringBuilder();
  182. builder.Append(string.Format("StatusCode:{0}", (int) responseMessage.StatusCode));
  183. builder.AppendLine();
  184. builder.Append(string.Format("Url:{0}", responseMessage.RequestMessage.RequestUri));
  185. HttpApiException httpEx = new HttpApiException(builder.ToString());
  186. if (requestInfo != null)
  187. {
  188. httpEx.RequestUrl = requestInfo.Head;
  189. httpEx.RequestContent = requestInfo.Content;
  190. }
  191. throw httpEx;
  192. }
  193. }
  194. /// <summary>
  195. /// 处理返回消息任务
  196. /// </summary>
  197. /// <param name="ts"></param>
  198. /// <param name="requestInfo"></param>
  199. /// <returns></returns>
  200. public static string HandlerTaskResponse(Task<HttpResponseMessage> ts, RequestInfo requestInfo)
  201. {
  202. try
  203. {
  204. var responseResult = ts.Result;
  205. if (responseResult.IsSuccessStatusCode)
  206. {
  207. var strContent = responseResult.Content.ReadAsStringAsync();
  208. return strContent.Result;
  209. }
  210. else
  211. {
  212. HandlerError(responseResult, requestInfo);
  213. }
  214. return string.Empty;
  215. }
  216. catch (Exception e)
  217. {
  218. #region 异常处理
  219. var useE = e;
  220. while (useE.InnerException != null)
  221. {
  222. useE = useE.InnerException;
  223. }
  224. HttpApiException httpEx = new HttpApiException(useE.Message, useE);
  225. var info = requestInfo;
  226. if (info != null)
  227. {
  228. httpEx.RequestUrl = info.Head;
  229. httpEx.RequestContent = info.Content;
  230. }
  231. throw httpEx;
  232. #endregion
  233. }
  234. finally
  235. {
  236. }
  237. return string.Empty;
  238. }
  239. #endregion
  240. }
  241. }