using System.Collections.Generic; namespace TProduct.HttpClient { /// /// HttpClient辅助类 /// public static class HttpClientExtensions { #region Headers /// /// 获取 /// public static T Get(this string url ) { var headers = new Dictionary { { "DbType", TProduct.CorpConfig.Instance.RealTimeRemoteService.PortName } }; return Get(url, headers); } /// /// 插入 /// public static T Post(this string url, object rhs ) { var headers = new Dictionary { { "DbType", TProduct.CorpConfig.Instance.RealTimeRemoteService.PortName } }; return Post(url, headers, rhs); } /// /// 修改 /// public static T Put(this string url, object rhs ) { var headers = new Dictionary { { "DbType", TProduct.CorpConfig.Instance.RealTimeRemoteService.PortName } }; return Put(url, headers, rhs); } /// /// 删除 /// public static T Delete(this string url ) { var headers = new Dictionary { { "DbType", TProduct.CorpConfig.Instance.RealTimeRemoteService.PortName } }; return Delete(url, headers); } /// /// 删除 /// public static bool Delete(this string url ) { var headers = new Dictionary { { "DbType", TProduct.CorpConfig.Instance.RealTimeRemoteService.PortName } }; return Delete(url, headers); } #endregion #region Default /// /// 获取 /// public static T Get(this string url, Dictionary headers) { if (string.IsNullOrEmpty(url)) return default; try { var responseText = HttpRequestHelper.Get(url, headers); var result = JsonHelper.Json2Object>(responseText); if (result.Code != ApiResultCode.Success) { throw new ApiException(result.Message); } return result.Data; } catch (ApiException ex) { throw ex; } catch (System.Exception ex) { throw new ApiException(ex.Message, ex); } } /// /// 插入 /// public static T Post(this string url, Dictionary headers, object rhs) { if (string.IsNullOrEmpty(url)) return default; if (rhs == null) return default; try { var data = JsonHelper.Object2Json(rhs); var responseText = HttpRequestHelper.Post(url, headers, data); var result = JsonHelper.Json2Object>(responseText); if (result.Code != ApiResultCode.Success) { throw new ApiException(result.Message); } return result.Data; } catch (ApiException ex) { throw ex; } catch (System.Exception ex) { throw new ApiException(ex.Message, ex); } } /// /// 修改 /// public static T Put(this string url, Dictionary headers, object rhs) { if (string.IsNullOrEmpty(url)) return default; if (rhs == null) return default; try { var data = JsonHelper.Object2Json(rhs); var responseText = HttpRequestHelper.Put(url, headers, data); var result = JsonHelper.Json2Object>(responseText); if (result.Code != ApiResultCode.Success) { throw new ApiException(result.Message); } return result.Data; } catch (ApiException ex) { throw ex; } catch (System.Exception ex) { throw new ApiException(ex.Message, ex); } } /// /// 删除 /// public static T Delete(this string url, Dictionary headers) { if (string.IsNullOrEmpty(url)) return default; try { var responseText = HttpRequestHelper.Delete(url, headers); var result = JsonHelper.Json2Object>(responseText); if (result.Code != ApiResultCode.Success) { throw new ApiException(result.Message); } return result.Data; } catch (ApiException ex) { throw ex; } catch (System.Exception ex) { throw new ApiException(ex.Message, ex); } } /// /// 删除 /// public static bool Delete(this string url, Dictionary headers) { if (string.IsNullOrEmpty(url)) return default; try { var responseText = HttpRequestHelper.Delete(url, headers); var result = JsonHelper.Json2Object>(responseText); if (result.Code != ApiResultCode.Success) { throw new ApiException(result.Message); } return result.Data; } catch (ApiException ex) { throw ex; } catch (System.Exception ex) { throw new ApiException(ex.Message, ex); } } #endregion } }