using System.Collections.Generic;
|
|
namespace TProduct.HttpClient
|
{
|
/// <summary>
|
/// HttpClient辅助类
|
/// </summary>
|
public static class HttpClientExtensions
|
{
|
#region Headers
|
|
/// <summary>
|
/// 获取
|
/// </summary>
|
public static T Get<T>(this string url )
|
{
|
var headers = new Dictionary<string, string>
|
{
|
{ "DbType", TProduct.CorpConfig.Instance.RealTimeRemoteService.PortName }
|
};
|
|
|
return Get<T>(url, headers);
|
}
|
|
|
/// <summary>
|
/// 插入
|
/// </summary>
|
public static T Post<T>(this string url, object rhs )
|
{
|
var headers = new Dictionary<string, string>
|
{
|
{ "DbType", TProduct.CorpConfig.Instance.RealTimeRemoteService.PortName }
|
};
|
|
return Post<T>(url, headers, rhs);
|
}
|
|
/// <summary>
|
/// 修改
|
/// </summary>
|
public static T Put<T>(this string url, object rhs )
|
{
|
var headers = new Dictionary<string, string>
|
{
|
{ "DbType", TProduct.CorpConfig.Instance.RealTimeRemoteService.PortName }
|
};
|
|
return Put<T>(url, headers, rhs);
|
}
|
|
|
/// <summary>
|
/// 删除
|
/// </summary>
|
public static T Delete<T>(this string url )
|
{
|
var headers = new Dictionary<string, string>
|
{
|
{ "DbType", TProduct.CorpConfig.Instance.RealTimeRemoteService.PortName }
|
};
|
|
|
return Delete<T>(url, headers);
|
}
|
|
/// <summary>
|
/// 删除
|
/// </summary>
|
public static bool Delete(this string url )
|
{
|
var headers = new Dictionary<string, string>
|
{
|
{ "DbType", TProduct.CorpConfig.Instance.RealTimeRemoteService.PortName }
|
};
|
|
|
return Delete(url, headers);
|
}
|
|
#endregion
|
|
#region Default
|
|
/// <summary>
|
/// 获取
|
/// </summary>
|
public static T Get<T>(this string url, Dictionary<string, string> headers)
|
{
|
if (string.IsNullOrEmpty(url))
|
return default;
|
|
try
|
{
|
var responseText = HttpRequestHelper.Get(url, headers);
|
var result = JsonHelper.Json2Object<ApiResult<T>>(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);
|
}
|
}
|
|
|
/// <summary>
|
/// 插入
|
/// </summary>
|
public static T Post<T>(this string url, Dictionary<string, string> 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<ApiResult<T>>(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);
|
}
|
}
|
|
/// <summary>
|
/// 修改
|
/// </summary>
|
public static T Put<T>(this string url, Dictionary<string, string> 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<ApiResult<T>>(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);
|
}
|
}
|
|
/// <summary>
|
/// 删除
|
/// </summary>
|
public static T Delete<T>(this string url, Dictionary<string, string> headers)
|
{
|
if (string.IsNullOrEmpty(url))
|
return default;
|
|
try
|
{
|
var responseText = HttpRequestHelper.Delete(url, headers);
|
var result = JsonHelper.Json2Object<ApiResult<T>>(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);
|
}
|
}
|
|
/// <summary>
|
/// 删除
|
/// </summary>
|
public static bool Delete(this string url, Dictionary<string, string> headers)
|
{
|
if (string.IsNullOrEmpty(url))
|
return default;
|
|
try
|
{
|
var responseText = HttpRequestHelper.Delete(url, headers);
|
var result = JsonHelper.Json2Object<ApiResult<bool>>(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
|
|
|
}
|
}
|