C#通用接口调用

项目场景:

提示:C#通用接口调用


解决方案:

提示:先获取令牌,然后调用数据接口:

/// 
        /// 获取Token值
        /// 
        /// token值
        public string InterfaceToken()
        {
            HttpWebRequest request = HttpWebRequest.CreateHttp("http://主机/token?client_id=xxxxxx";);
            request.ContentType = "application/json";//body数据类型
            request.Method = "GET";
            var response = (HttpWebResponse)request.GetResponse();
            var resuleJson = string.Empty;//返回的json数据
            using (Stream stream = response.GetResponseStream())
            {
                using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("UTF-8")))
                {
                    resuleJson = reader.ReadToEnd();
                }
            }
            TokenData tokenData = new TokenData();
            tokenData = JsonConvert.DeserializeObject(resuleJson);
            var aaa= JsonConvert.DeserializeObject(resuleJson);
            return tokenData.access_token;
        }
public class TokenData 
    { 
        public string access_token { get; set; }
    }

调用接口:

/// 
        /// 接口数据获取
        /// 
        /// 物料组
        /// 分类码
        /// 物料代码
        public void InterfaceData(string mitm = "")
        {

            string citgs = "[]";//citgs  物料组 false
            int citgsOperation = 0;//citgsOperation物料组过滤操作 true     0
            int cpclOperation = 0;//cpclOperation 分类码过滤操作 true      0
            string cpcls = "[]";//cpcls分类码 false  
            string level = "1";//level需要展开的BOM的层数 false
            //string mitm = "";//mitm物料代码 true
            string queryDate = "";//queryDate查询时间 false

            string token = InterfaceToken();
            string data = "{\"citgs\": " + citgs + ",\"citgsOperation\":" + citgsOperation + ",\"cpclOperation\": " + cpclOperation + ",\"cpcls\":" + cpcls + ",\"level\":" + level + ",\"mitm\":\" " + mitm + "\",\"queryDate\": \"" + queryDate + "\"}";
            
            HttpWebRequest request = HttpWebRequest.CreateHttp("http://主机";);
            request.ContentType = "application/json";//body数据类型
            request.Headers.Add("x-authorization", token);
            request.Method = "POST";
            using (var postStream = new StreamWriter(request.GetRequestStream()))
            {
                postStream.Write(data);
            }
            var response = (HttpWebResponse)request.GetResponse();
            var resuleJson = string.Empty;//返回的json数据
            using (Stream stream = response.GetResponseStream())
            {
                using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("UTF-8")))
                {
                    resuleJson = reader.ReadToEnd();
                }
            }
        }

你可能感兴趣的:(C#,c#,json,webview)