mes集成增加MesCallResult
This commit is contained in:
+45
-4
@@ -1,6 +1,10 @@
|
||||
using JY.MES.Entity;
|
||||
using JY.MES.Enum;
|
||||
using JY.MES.Exceptions;
|
||||
using JY.MES.MES;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.CodeDom;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
@@ -31,11 +35,48 @@ namespace JY.MES
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
mesResponse.success = false;
|
||||
mesResponse.message = $"massage:[{ex.InnerException}],mes: [MES反馈超时或无法访间MES服务器]";
|
||||
mesResponse.error = 99;
|
||||
return JsonConvert.SerializeObject(mesResponse);
|
||||
throw new MesNetworkException($"massage:[{ex.InnerException}],mes: [MES反馈超时或无法访间MES服务器]");
|
||||
}
|
||||
}
|
||||
|
||||
public static MesCallResult<T> HttpPostJsonAPI<T>(string url, string strJosnData, double MesRequestTime)
|
||||
{
|
||||
var result = new MesCallResult<T>() { IsSuccess = false };
|
||||
|
||||
try
|
||||
{
|
||||
StringContent stringContent = new StringContent(strJosnData, Encoding.UTF8, "application/json");
|
||||
string respJsonStr = new HttpClient()
|
||||
{
|
||||
Timeout = TimeSpan.FromSeconds(MesRequestTime)
|
||||
}.PostAsync(url, (HttpContent)stringContent).Result.Content.ReadAsStringAsync().Result;
|
||||
|
||||
var mesResp = JsonConvert.DeserializeObject<T>(respJsonStr);
|
||||
|
||||
result.IsSuccess = true;
|
||||
result.OriResponse = mesResp;
|
||||
result.OriRespJsonStr = respJsonStr;
|
||||
}
|
||||
catch (TimeoutException ex)
|
||||
{
|
||||
result.ErrorType = MesErrorType.NetworkTimeout;
|
||||
result.ErrorMessage = $"MES请求超时: {ex.Message}";
|
||||
return result;
|
||||
}
|
||||
catch (HttpRequestException ex)
|
||||
{
|
||||
result.ErrorType = MesErrorType.NetworkError;
|
||||
result.ErrorMessage = $"MES网络请求失败: {ex.Message}";
|
||||
return result;
|
||||
}
|
||||
catch (JsonException ex)
|
||||
{
|
||||
result.ErrorType = MesErrorType.ParseError;
|
||||
result.ErrorMessage = $"MES响应解析失败: {ex.Message}";
|
||||
return result;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user