/// <summary> /// SyncApiByJson Json /// </summary> /// <returns></returns> public string SyncApiByJson(Log4netUtil.LogAppendToForms logAppendToForms, Model.JobEntity jobInfo, string controllerType, string requestData) { string reqUrl = string.Format("{0}/{1}", ReqURL, controllerType); Newtonsoft.Json.Linq.JObject resultJObject = new Newtonsoft.Json.Linq.JObject(); Newtonsoft.Json.Linq.JArray jlistRequestData = Newtonsoft.Json.Linq.JArray.Parse(requestData); string logMessage = string.Empty; string result = string.Empty; string t = string.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now); //时间戳(格式:yyyy-MM-ddHH:mm:ss) string token = BuildToken(); //"nDWKKyRDMrS1M9zxIbTAQQ=="; // if (string.IsNullOrEmpty(token)) { logMessage = string.Format(" controllerType:{0} ;SendPost SyncApiByJson 接口调用 BuildToken token生成失败:token为空 !", controllerType); Newtonsoft.Json.Linq.JObject requestDataObj = Newtonsoft.Json.Linq.JObject.Parse(requestData); resultJObject.Add("errMsg", new Newtonsoft.Json.Linq.JValue(logMessage)); resultJObject.Add("rstFlag", new Newtonsoft.Json.Linq.JValue(10)); resultJObject.Add("reqUrl", new Newtonsoft.Json.Linq.JValue(reqUrl)); resultJObject.Add("t", new Newtonsoft.Json.Linq.JValue(t)); resultJObject.Add("token", new Newtonsoft.Json.Linq.JValue(token)); resultJObject.Add("items", new Newtonsoft.Json.Linq.JArray(jlistRequestData)); result = NewtonsoftCommon.SerializeObjToJson(resultJObject); logMessage = string.Format("【{0}_{1}】 {2}", jobInfo.JobCode, jobInfo.JobName.ToString(), result); Log4netUtil.Log4NetHelper.Error(logMessage, @"PrepurchApi"); Log4netUtil.LogDisplayHelper.LogError(logAppendToForms, logMessage); return(result); } resultJObject = new Newtonsoft.Json.Linq.JObject(); Dictionary <string, string> param = new Dictionary <string, string>(); param.Add("t", t); param.Add("token", token); param.Add("items", HttpUtility.UrlEncode(requestData, Encoding.UTF8));//请求内容为 JSON 格式 //param.Add("RequestData", HttpUtility.UrlEncode(requestJson, Encoding.UTF8));//请求内容为 JSON 格式 result = sendPost(reqUrl, param); resultJObject = new Newtonsoft.Json.Linq.JObject(); //logMessage = string.Format("SendPost SyncApiByJson 接口调用! controllerType:{0} ", controllerType); logMessage = string.Format(" controllerType:{0} ;SendPost SyncApiByJson 接口调用!", controllerType); resultJObject.Add("msg", new Newtonsoft.Json.Linq.JValue(logMessage)); resultJObject.Add("reqUrl", new Newtonsoft.Json.Linq.JValue(reqUrl)); resultJObject.Add("t", new Newtonsoft.Json.Linq.JValue(t)); resultJObject.Add("token", new Newtonsoft.Json.Linq.JValue(token)); //resultJObject.Add("order_code", new Newtonsoft.Json.Linq.JValue(orderCode)); resultJObject.Add("items", new Newtonsoft.Json.Linq.JArray(jlistRequestData)); resultJObject.Add("result", new Newtonsoft.Json.Linq.JObject(Newtonsoft.Json.Linq.JObject.Parse(result))); logMessage = string.Format("【{0}_{1}】 {2}", jobInfo.JobCode, jobInfo.JobName.ToString(), NewtonsoftCommon.SerializeObjToJson(resultJObject)); Log4netUtil.Log4NetHelper.Info(logMessage, @"PrepurchApi"); Log4netUtil.LogDisplayHelper.LogMessage(logAppendToForms, logMessage); return(result); }
//token生成方式如下: private string BuildToken() { string token = string.Empty; try { string curDay = string.Format("{0:yyyyMMdd}", DateTime.Now); //DateUtil.formatDate(new Date(), "yyyyMMdd"); string content = "YK0001-" + curDay; token = Util.DESEncryptHelper.EncryptString(content); } catch (Exception ex) { Newtonsoft.Json.Linq.JObject resultJObject = new Newtonsoft.Json.Linq.JObject(); string logMessage = string.Format("BuildToken token生成失败{0}", ex.Message); resultJObject.Add("errMsg", logMessage); resultJObject.Add("rstFlag", 10); string result = NewtonsoftCommon.SerializeObjToJson(resultJObject); Log4netUtil.Log4NetHelper.Error(result, @"PrepurchApi"); return(string.Empty); } return(token); }