Пример #1
0
        /// <summary>
        /// 客户端请求路由端
        /// </summary>
        /// <returns></returns>
        public string ClientRequest()
        {
            //报文头
            dynamic head = new ExpandoObject();

            //流水号(必填)
            head.SerialNumber = Guid.NewGuid().ToString();
            //请求地址(必填)
            head.RequestHost = "127.0.0.1";
            //请求时间(必填)
            head.RequestTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            //命名名(必填)
            head.Command = "Finance.FinanceList";
            //版本号(选填)
            head.Version = "1.2.0";
            //系统类型【pc,android,ios】(选填)
            head.System = "pc";
            //渠道【web,wap,app,cache】(选填)
            head.Channel = "web";
            //用户ID(选填)
            head.UserId = "";
            //是否使用缓存(选填)
            head.UseCache = "false";
            string headData = JsonConvert.SerializeObject(head);

            headData = JsonWebToken.Base64UrlEncode(Encoding.UTF8.GetBytes(headData));

            //Body参数,不同接口传递不同参数,可空
            Dictionary <string, object> bodyDic = new Dictionary <string, object>();

            bodyDic.Add("Duration", -1);
            bodyDic.Add("Interest", -1);
            bodyDic.Add("PageIndex", 1);
            bodyDic.Add("PageSize", 10);

            //Body使用DES加密,key参考GetDesKey方法
            string bodyData = JsonConvert.SerializeObject(bodyDic);

            bodyData = JsonWebToken.Base64UrlEncode(Encoding.UTF8.GetBytes(bodyData));
            string encryptBody = EncryptHelper.DESEncrypt(bodyData, GetDesKey(head.Channel));

            string url = "http://localhost:6854/Api";
            //if (url.ToLower().Contains("https"))
            //{
            //    //忽略https校验
            //    ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
            //}

            //-- Post
            RestClient client = new RestClient(url);

            client.Proxy        = null;
            client.Timeout      = 60000;
            client.MaxRedirects = 1;
            RestRequest request = new RestRequest(Method.POST);

            request.AddHeader("head", headData);
            request.AddParameter("body", encryptBody);
            string result = client.Execute(request).Content;

            return(result);
        }