Ejemplo n.º 1
0
 /// <summary>
 /// http调用接口
 /// </summary>
 /// <param name="api"></param>
 /// <param name="json"></param>
 /// <returns></returns>
 private static Response Post(string api, JObject json)
 {
     try
     {
         var handler = new HttpClientHandler
         {
             AllowAutoRedirect = true
         };
         var httpClient = new HttpClient(handler)
         {
             Timeout = TimeSpan.FromSeconds(15)
         };
         httpClient.DefaultRequestHeaders.Add("Accept", "application/json, text/javascript");
         var content = new ByteArrayContent(Encoding.UTF8.GetBytes(json.ToString()));
         content.Headers.Add("Content-Type", "application/json");
         var responseMessage = httpClient.PostAsync("https://api.xky.com/" + api, content).Result;
         var jsonResult      =
             JsonConvert.DeserializeObject <JObject>(responseMessage.Content.ReadAsStringAsync().Result);
         if (jsonResult == null || !jsonResult.ContainsKey("encrypt"))
         {
             return new Response
                    {
                        Result  = false,
                        Message = "通讯结果无法解析",
                        Json    = new JObject {
                            ["errcode"] = 1, ["msg"] = "通讯结果无法解析"
                        }
                    }
         }
         ;
         var resultJson =
             JsonConvert.DeserializeObject <JObject>(Rsa.DecrypteRsa(jsonResult["encrypt"].ToString()));
         return(new Response
         {
             Result = resultJson["errcode"] != null && Convert.ToInt32(resultJson["errcode"]) == 0,
             Message = resultJson["msg"]?.ToString(),
             Json = JsonConvert.DeserializeObject <JObject>(Rsa.DecrypteRsa(jsonResult["encrypt"].ToString()))
         });
     }
     catch (Exception e)
     {
         return(new Response
         {
             Result = false,
             Message = e.Message,
             Json = new JObject {
                 ["errcode"] = 1, ["msg"] = e.Message
             }
         });
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 调用平台接口
        /// </summary>
        /// <param name="api"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static Response CallApi(string api, JObject data)
        {
            var response = new Response
            {
                Result  = false,
                Message = "调用接口超时",
                Json    = new JObject {
                    ["errcode"] = 1, ["msg"] = "调用接口超时"
                }
            };
            var count = 10000;

            if (CoreSocket == null || !CoreConnected)
            {
                return new Response
                       {
                           Result  = false,
                           Message = "未连接核心服务器",
                           Json    = new JObject {
                               ["errcode"] = 1, ["msg"] = "未连接核心服务器"
                           }
                       }
            }
            ;

            CoreSocket.Emit("call", result =>
            {
                var jsonResult = (JObject)result;

                if (jsonResult == null || !jsonResult.ContainsKey("encrypt"))
                {
                    response = new Response
                    {
                        Result  = false,
                        Message = "通讯结果无法解析",
                        Json    = new JObject {
                            ["errcode"] = 1, ["msg"] = "通讯结果无法解析"
                        }
                    };
                }
                else
                {
                    var resultJson =
                        JsonConvert.DeserializeObject <JObject>(Rsa.DecrypteRsa(jsonResult["encrypt"].ToString()));
                    response = new Response
                    {
                        Result  = resultJson["errcode"] != null && Convert.ToInt32(resultJson["errcode"]) == 0,
                        Message = resultJson["msg"]?.ToString(),
                        Json    = JsonConvert.DeserializeObject <JObject>(Rsa.DecrypteRsa(jsonResult["encrypt"].ToString()))
                    };
                }

                //设置跳出循环条件
                count = 0;
            }, api, data);


            while (count > 0)
            {
                count -= 1;

                Thread.Sleep(1);
            }

            return(response);
        }