|
@@ -7,6 +7,7 @@ using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
using Com.FirmLib.Common;
|
|
|
using FirmHttpDao;
|
|
|
+using FWindSoft;
|
|
|
using FWindSoft.Net.Http;
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
@@ -19,12 +20,16 @@ namespace Com.FirmLib
|
|
|
{
|
|
|
protected string m_ExtensionUri;
|
|
|
protected HttpClient m_HttpClient;
|
|
|
+
|
|
|
#region 错误机制
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 当前错误信息
|
|
|
/// </summary>
|
|
|
public string ErrorMessage { get; set; }
|
|
|
+
|
|
|
#endregion
|
|
|
+
|
|
|
#region 地址信息相关
|
|
|
|
|
|
/// <summary>
|
|
@@ -35,6 +40,7 @@ namespace Com.FirmLib
|
|
|
{
|
|
|
return BllHttpSetting.Instance.EndUri;
|
|
|
}
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 获取创建组
|
|
|
/// </summary>
|
|
@@ -43,6 +49,7 @@ namespace Com.FirmLib
|
|
|
{
|
|
|
return BllHttpSetting.Instance.InsertKey();
|
|
|
}
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 获取删除组
|
|
|
/// </summary>
|
|
@@ -51,6 +58,7 @@ namespace Com.FirmLib
|
|
|
{
|
|
|
return BllHttpSetting.Instance.DeleteKey();
|
|
|
}
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 获取更新组
|
|
|
/// </summary>
|
|
@@ -59,6 +67,7 @@ namespace Com.FirmLib
|
|
|
{
|
|
|
return BllHttpSetting.Instance.UpdateKey();
|
|
|
}
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 获取查询组
|
|
|
/// </summary>
|
|
@@ -67,7 +76,9 @@ namespace Com.FirmLib
|
|
|
{
|
|
|
return BllHttpSetting.Instance.SearchKey();
|
|
|
}
|
|
|
+
|
|
|
#endregion
|
|
|
+
|
|
|
public BaseHttpBll(string extensionUri)
|
|
|
{
|
|
|
Init(extensionUri);
|
|
@@ -78,35 +89,12 @@ namespace Com.FirmLib
|
|
|
this.m_ExtensionUri = extensionUri;
|
|
|
}
|
|
|
|
|
|
- protected static void HandlerError(HttpResponseMessage responseMessage)
|
|
|
- {
|
|
|
- if (responseMessage.StatusCode != HttpStatusCode.OK)
|
|
|
- {
|
|
|
- StringBuilder builder = new StringBuilder();
|
|
|
- builder.Append(string.Format("StatusCode:{0}", (int)responseMessage.StatusCode));
|
|
|
- builder.AppendLine();
|
|
|
- builder.Append(string.Format("Url:{0}", responseMessage.RequestMessage.RequestUri));
|
|
|
- //var content = responseMessage.RequestMessage.Content;
|
|
|
- //var input = content.ReadAsStringAsync();
|
|
|
-
|
|
|
- //builder.Append(string.Format("input:{0}", input.Result));
|
|
|
- throw new ApplicationException(builder.ToString());
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- protected void HandlerError(Task<HttpResponseMessage> result)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- HandlerError(result.Result);
|
|
|
|
|
|
- }
|
|
|
- catch (Exception ex)
|
|
|
- {
|
|
|
+ //protected void HandlerError(Task<HttpResponseMessage> result)
|
|
|
+ //{
|
|
|
+ // HandlerError(result.Result);
|
|
|
+ //}
|
|
|
|
|
|
- //throw new ApplicationException(string.Format("失败:{0}", ex.InnerException));
|
|
|
- }
|
|
|
- }
|
|
|
protected virtual HttpClient CreateHttpClient()
|
|
|
{
|
|
|
//if (m_HttpClient != null)
|
|
@@ -114,6 +102,7 @@ namespace Com.FirmLib
|
|
|
//return m_HttpClient = HttpClientManager.CreateClient();// new HttpClient() { BaseAddress = new Uri(GetEndUri()) };
|
|
|
return HttpClientManager.CreateClient();
|
|
|
}
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 获取操作的api
|
|
|
/// </summary>
|
|
@@ -123,86 +112,176 @@ namespace Com.FirmLib
|
|
|
return this.BaseEndUri() + this.m_ExtensionUri;
|
|
|
}
|
|
|
|
|
|
+ #region 捕获业务处理返回消息
|
|
|
+
|
|
|
+ protected void CatchErrorMessage(JObject jobject)
|
|
|
+ {
|
|
|
+ PostResult p = PostResult.CreatePostResult(jobject);
|
|
|
+ this.ErrorMessage = p.ResultMessage;
|
|
|
+ }
|
|
|
+
|
|
|
+ #endregion
|
|
|
+
|
|
|
#region 基础命令封装
|
|
|
|
|
|
- public Task<HttpResponseMessage> PostJsonAsync(Uri requeUri, JObject jobject)
|
|
|
+ public string PostJsonAsync(Uri requeUri, JObject jobject)
|
|
|
{
|
|
|
- var client = CreateHttpClient();
|
|
|
- return client.PostJosnAsync(requeUri, jobject);
|
|
|
+ RequestInfo request = new RequestInfo(requeUri.ToString(), jobject);
|
|
|
+ try
|
|
|
+ {
|
|
|
+ #region 发送请求
|
|
|
+ var client = CreateHttpClient();
|
|
|
+ var response = client.PostJosnAsync(requeUri, jobject);
|
|
|
+ #endregion
|
|
|
+ return HandlerTaskResponse(response,request);
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ throw;
|
|
|
+ }
|
|
|
}
|
|
|
- public Task<HttpResponseMessage> PostJsonAdd(JObject jobject)
|
|
|
+
|
|
|
+ public string PostJsonAdd(JObject jobject)
|
|
|
{
|
|
|
-
|
|
|
- return PostJsonAsync(new Uri(GetEndUri()+CreateKey(),UriKind.RelativeOrAbsolute) , jobject);
|
|
|
+ return PostJsonAsync(new Uri(GetEndUri() + CreateKey(), UriKind.RelativeOrAbsolute), jobject);
|
|
|
}
|
|
|
- public Task<HttpResponseMessage> PostJsonUpdate( JObject jobject)
|
|
|
+
|
|
|
+ public string PostJsonUpdate(JObject jobject)
|
|
|
{
|
|
|
return PostJsonAsync(new Uri(GetEndUri() + UpdateKey(), UriKind.RelativeOrAbsolute), jobject);
|
|
|
}
|
|
|
- public Task<HttpResponseMessage> PostJsonDelete(JObject jobject)
|
|
|
+
|
|
|
+ public string PostJsonDelete(JObject jobject)
|
|
|
{
|
|
|
return PostJsonAsync(new Uri(GetEndUri() + DeleteKey(), UriKind.RelativeOrAbsolute), jobject);
|
|
|
}
|
|
|
- public Task<HttpResponseMessage> PostJsonQuery( JObject jobject)
|
|
|
+
|
|
|
+ public string PostJsonQuery(JObject jobject)
|
|
|
{
|
|
|
return PostJsonAsync(new Uri(GetEndUri() + QueryKey(), UriKind.RelativeOrAbsolute), jobject);
|
|
|
}
|
|
|
- public Task<HttpResponseMessage> PostJsonQuery()
|
|
|
+
|
|
|
+ public string PostJsonQuery()
|
|
|
{
|
|
|
return PostJsonAsync(new Uri(GetEndUri() + QueryKey(), UriKind.RelativeOrAbsolute), new JObject());
|
|
|
}
|
|
|
+
|
|
|
+ #endregion
|
|
|
+
|
|
|
+
|
|
|
+ #region 业务结果处理
|
|
|
+ /// <summary>
|
|
|
+ /// 业务结果处理
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="responseResult"></param>
|
|
|
+ /// <param name="success"></param>
|
|
|
+ /// <param name="fail"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static bool HandlerResult(string responseResult, Action<JObject> success, Action<JObject> fail)
|
|
|
+ {
|
|
|
+ JObject result = JObject.Parse(responseResult);
|
|
|
+ PostResult p = PostResult.CreatePostResult(result);
|
|
|
+ if (p.IsSuccess)
|
|
|
+ {
|
|
|
+ success?.Invoke(result);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ fail?.Invoke(result);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static bool HandlerResult(string responseResult, Action<JObject> success)
|
|
|
+ {
|
|
|
+ return HandlerResult(responseResult, success, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static bool HandlerResult(string responseResult)
|
|
|
+ {
|
|
|
+ return HandlerResult(responseResult, null, null);
|
|
|
+ }
|
|
|
+
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
- #region 静态关联方法
|
|
|
- public static bool HandlerResult(Task<HttpResponseMessage> taskResponse,Action<JObject> success,Action<JObject> fail)
|
|
|
+
|
|
|
+ #region 网络异常错误处理
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 处理错误返回消息
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="responseMessage"></param>
|
|
|
+ /// <param name="requestInfo"></param>
|
|
|
+ public static void HandlerError(HttpResponseMessage responseMessage, RequestInfo requestInfo)
|
|
|
+ {
|
|
|
+ if (responseMessage.StatusCode != HttpStatusCode.OK)
|
|
|
+ {
|
|
|
+ StringBuilder builder = new StringBuilder();
|
|
|
+ builder.Append(string.Format("StatusCode:{0}", (int) responseMessage.StatusCode));
|
|
|
+ builder.AppendLine();
|
|
|
+ builder.Append(string.Format("Url:{0}", responseMessage.RequestMessage.RequestUri));
|
|
|
+ HttpApiException httpEx = new HttpApiException(builder.ToString());
|
|
|
+ if (requestInfo != null)
|
|
|
+ {
|
|
|
+ httpEx.RequestUrl = requestInfo.Head;
|
|
|
+ httpEx.RequestContent = requestInfo.Content;
|
|
|
+ }
|
|
|
+
|
|
|
+ throw httpEx;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 处理返回消息任务
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="ts"></param>
|
|
|
+ /// <param name="requestInfo"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static string HandlerTaskResponse(Task<HttpResponseMessage> ts, RequestInfo requestInfo)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
- if (taskResponse.Result.IsSuccessStatusCode)
|
|
|
+ var responseResult = ts.Result;
|
|
|
+ if (responseResult.IsSuccessStatusCode)
|
|
|
{
|
|
|
- var strContent = taskResponse.Result.Content.ReadAsStringAsync();
|
|
|
- JObject result = JObject.Parse(strContent.Result);
|
|
|
- PostResult p = PostResult.CreatePostResult(result);
|
|
|
- if (p.IsSuccess)
|
|
|
- {
|
|
|
- success?.Invoke(result);
|
|
|
- return true;
|
|
|
- }
|
|
|
- fail?.Invoke(result);
|
|
|
- return false;
|
|
|
+ var strContent = responseResult.Content.ReadAsStringAsync();
|
|
|
+ return strContent.Result;
|
|
|
}
|
|
|
- HandlerError(taskResponse.Result);
|
|
|
- return false;
|
|
|
+ else
|
|
|
+ {
|
|
|
+ HandlerError(responseResult, requestInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ return string.Empty;
|
|
|
}
|
|
|
- catch(Exception e)
|
|
|
+ catch (Exception e)
|
|
|
{
|
|
|
- var resultE = e.InnerException;
|
|
|
- var temp = e.GetBaseException();
|
|
|
- if (temp != null)
|
|
|
+ #region 异常处理
|
|
|
+ var useE = e;
|
|
|
+ while (useE.InnerException != null)
|
|
|
+ {
|
|
|
+ useE = useE.InnerException;
|
|
|
+ }
|
|
|
+
|
|
|
+ HttpApiException httpEx = new HttpApiException(useE.Message, useE);
|
|
|
+ var info = requestInfo;
|
|
|
+ if (info != null)
|
|
|
{
|
|
|
- resultE = temp.InnerException??temp;
|
|
|
+ httpEx.RequestUrl = info.Head;
|
|
|
+ httpEx.RequestContent = info.Content;
|
|
|
}
|
|
|
- //HttpClientManager.Clear();
|
|
|
- throw resultE;
|
|
|
- //return false;
|
|
|
+
|
|
|
+ throw httpEx;
|
|
|
+
|
|
|
+ #endregion
|
|
|
}
|
|
|
finally
|
|
|
{
|
|
|
|
|
|
- //taskResponse.Result.Dispose();
|
|
|
}
|
|
|
-
|
|
|
- }
|
|
|
- public static bool HandlerResult(Task<HttpResponseMessage> taskResponse, Action<JObject> success)
|
|
|
- {
|
|
|
- return HandlerResult(taskResponse, success, null);
|
|
|
- }
|
|
|
- public static bool HandlerResult(Task<HttpResponseMessage> taskResponse)
|
|
|
- {
|
|
|
- return HandlerResult(taskResponse, null, null);
|
|
|
+ return string.Empty;
|
|
|
}
|
|
|
#endregion
|
|
|
-
|
|
|
}
|
|
|
}
|