Ejemplo n.º 1
0
        /// <summary>
        /// 网络检测
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="action"></param>
        /// <param name="operatorEnum"></param>
        /// <returns></returns>
        public NetworkDetectionResultModel NetworkDetection(string accessToken, NetworkDetectionActionEnum action = NetworkDetectionActionEnum.All, NetworkDetectionOperatorEnum operatorEnum = NetworkDetectionOperatorEnum.Default)
        {
            var data = new WeChatDataModel();

            data.SetValue("access_token", accessToken);
            string url      = $"{Config.WeChatAPIUrl}cgi-bin/callback/check?{data.ToUrlParams()}";
            var    postData = new WeChatDataModel();

            postData.SetValue("action", action.ToString().ToLower());
            postData.SetValue("check_operator", operatorEnum.ToString().ToUpper());
            string   weChatResult = WeChatHttpManager.PostJson(url, postData.ToObject(), false, 3000, Config);
            JsonData jsonData     = JsonMapper.ToObject(weChatResult);

            if (WeChatPublicNumberErrorHelper.IsError(jsonData))
            {
                throw WeChatPublicNumberErrorHelper.GetWeChatException(jsonData);
            }
            var result = new NetworkDetectionResultModel();

            if (jsonData.ContainsKey("dns"))
            {
                result.DNS = new List <NetworkDetectionDNSModel>();
                JsonData dns = jsonData["dns"];
                for (var i = 0; i < dns.Count; i++)
                {
                    string operatorStr  = dns[i]["real_operator"].ToString();
                    var    realOperator = NetworkDetectionOperatorEnum.Default;
                    switch (operatorStr)
                    {
                    case "CAP":
                        realOperator = NetworkDetectionOperatorEnum.Cap;
                        break;

                    case "CHINANET":
                        realOperator = NetworkDetectionOperatorEnum.ChinaNet;
                        break;

                    case "UNICOM":
                        realOperator = NetworkDetectionOperatorEnum.UniCom;
                        break;
                    }
                    result.DNS.Add(new NetworkDetectionDNSModel
                    {
                        IP           = dns[i]["ip"].ToString(),
                        RealOperator = realOperator
                    });
                }
            }
            if (jsonData.ContainsKey("ping"))
            {
                result.Ping = new List <NetworkDetectionPingModel>();
                JsonData ping = jsonData["ping"];
                for (var i = 0; i < ping.Count; i++)
                {
                    string operatorStr  = ping[i]["from_operator"].ToString();
                    var    fromOperator = NetworkDetectionOperatorEnum.Default;
                    switch (operatorStr)
                    {
                    case "CAP":
                        fromOperator = NetworkDetectionOperatorEnum.Cap;
                        break;

                    case "CHINANET":
                        fromOperator = NetworkDetectionOperatorEnum.ChinaNet;
                        break;

                    case "UNICOM":
                        fromOperator = NetworkDetectionOperatorEnum.UniCom;
                        break;
                    }
                    result.Ping.Add(new NetworkDetectionPingModel
                    {
                        IP           = ping[i]["ip"].ToString(),
                        PackageLoss  = ping[i]["package_loss"].ToString(),
                        Time         = ping[i]["time"].ToString(),
                        FromOperator = fromOperator
                    });
                }
            }
            return(result);
        }