阿里云API 网关前端直接调用

阿里云API网关官网文档

https://help.aliyun.com/zh/api-gateway/user-guide/examples-on-calling-apis?spm=a2c4g.11186623.0.i19

https://help.aliyun.com/zh/api-gateway/user-guide/use-digest-authentication-to-call-an-api?spm=a2c4g.11186623.0.0.4e7436fbpdR0Jr

需要前端直接调API网关的js代码请直接联系我

通过 咸鱼APP 搜索用户: 御心星辰 ,点击第一条点我想要可快速联系到我(id: tb755868803)。

有对接过复兴商业等网关API
源码:

https://gitee.com/wy333/xmlPro/blob/master/fuxingshangye/http.js

以下为java 请求实例

/// <summary>
        /// 注册会员
        /// </summary>
        public static void Post()
        {
            //====================================参数准备
            string httpMethod = "POST";                           //请求方法
            string domain = "";                                   //请求域名
            string path = "/Register";                //请求地址,网关注册接口 
            string stage = "TEST";                                //请求环境 
            string gateWayAppKey = "";                            //网关Appkey
            string gateWayAppSercert = "";                        //网关AppSercert
            string fengGaoAppKey = "";                            //AppKey
            string fengGaoAppSercert = "";                        //AppSercert 
            var timestamp = (long)(DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;//时间戳    

            //请求参数 
            //请注意参数大小写,某些接口会大小写敏感
            var postData = new
            { 
                MobileNumber = "15922433136",
                BirthDay = "1997-01-01 00:00:00",
                Name = "XX",
                Gender = 0,
                MallCode = "110", 
            };   



            //=====================================headers准备
            //请求header准备
            Dictionary<string, string> headers = new Dictionary<string, string>(); 
            //网关验签头
            headers.Add("Content-Type", "application/json; charset=utf-8");
            headers.Add("Accept", "application/json; charset=utf-8");
            headers.Add("Content-MD5", MessageDigestUtil.Base64AndMD5(Encoding.UTF8.GetBytes(ConvertJson(postData))));
            headers.Add("X-Ca-Stage",stage);
            headers.Add("X-Ca-Timestamp",timestamp.ToString()); 
            headers.Add("X-Ca-Nonce",Guid.NewGuid().ToString());
            headers.Add("X-Ca-Key", gateWayAppKey);
            headers.Add("X-Ca-Signature-Headers", "X-Ca-Key,X-Ca-Nonce,X-Ca-Stage,X-Ca-Timestamp");
            //峰高验签头
            headers.Add("appkey", fengGaoAppKey);
            headers.Add("timestamp", timestamp.ToString());



            //======================================签名准备 
            //网关签名
            StringBuilder singstr = new StringBuilder();
            singstr.Append(httpMethod);singstr.Append("\n");
            singstr.Append(headers["Accept"]); singstr.Append("\n");
            if(headers.ContainsKey("Content-MD5")) singstr.Append(headers["Content-MD5"]);
            singstr.Append("\n");
            singstr.Append(headers["Content-Type"]); singstr.Append("\n");
            if(headers.ContainsKey("Date"))  singstr.Append(headers["Date"]); 
            singstr.Append("\n");
            //headers拼接
                StringBuilder headerstr = new StringBuilder();
                headerstr.Append("X-Ca-Key:"); headerstr.Append(headers["X-Ca-Key"]); headerstr.Append("\n");
                headerstr.Append("X-Ca-Nonce:"); headerstr.Append(headers["X-Ca-Nonce"]); headerstr.Append("\n");
                headerstr.Append("X-Ca-Stage:"); headerstr.Append(headers["X-Ca-Stage"]); headerstr.Append("\n");
                headerstr.Append("X-Ca-Timestamp:"); headerstr.Append(headers["X-Ca-Timestamp"]);
            singstr.Append(headerstr.ToString()); singstr.Append("\n");
            //如果有query参数 或者 form参数,添加path?param=param,具体见文档
            singstr.Append(path);
            headers.Add("X-Ca-Signature",CreateGateWaySign(gateWayAppSercert, singstr.ToString()));
            //签名
            headers.Add("sign", MD5(fengGaoAppKey + timestamp + fengGaoAppSercert));


            //========================================请求
            var httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(domain+path));
            httpRequest.Method = httpMethod;
            foreach (var header in headers)
            {
                if (header.Key== "Accept") httpRequest.Accept = header.Value;
                else if (header.Key == "Content-Type") httpRequest.ContentType = header.Value;
                else if (header.Key == "Date") httpRequest.Date =Convert.ToDateTime(header.Value);
                else  httpRequest.Headers.Add(header.Key, header.Value); 
            }
            byte[] data = Encoding.UTF8.GetBytes(ConvertJson(postData));
            Stream stream = httpRequest.GetRequestStream();
            stream.Write(data, 0, data.Length); 
            using (var response = GetResponse(httpRequest))
            {
                Console.WriteLine(response.StatusCode);
                Console.WriteLine(response.Method);
                Console.WriteLine(response.Headers);
                Stream st = response.GetResponseStream();
                StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
                Console.WriteLine(reader.ReadToEnd());
                Console.WriteLine(Constants.LF);
            } 

        }

         



        public static string CreateGateWaySign(string appSercert,string signStr)
        {
            
            using (var algorithm = KeyedHashAlgorithm.Create("HMACSHA256"))
            {
                algorithm.Key = Encoding.UTF8.GetBytes(appSercert.ToCharArray());
                return Convert.ToBase64String(algorithm.ComputeHash(Encoding.UTF8.GetBytes(signStr.ToCharArray())));
            }
        }



       
        public static string MD5(string Str, bool isUpper = false)
        {
            var bytes = new MD5CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(Str));
            string ret = "";
            foreach (byte bb in bytes) { ret += Convert.ToString(bb, 16).PadLeft(2, '0'); }
            var result = ret.PadLeft(32, '0');
            return isUpper ? result.ToUpper() : result.ToLower();
        }

        


        public static string ConvertJson(object o)
        {
           return  JsonConvert.SerializeObject(o); 
        }



        private static HttpWebResponse GetResponse(HttpWebRequest httpRequest)
        {
            HttpWebResponse httpResponse = null;
            try
            {
                WebResponse response = httpRequest.GetResponse();
                httpResponse = (HttpWebResponse)response;

            }
            catch (WebException ex)
            {
                httpResponse = (HttpWebResponse)ex.Response;
            }
            return httpResponse;
        }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值