c#调用腾讯云API的实例

       //获取时间戳 .net framework
            /*
            DateTime dt = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); 
            int time = (int)(DateTime.Now - dt).TotalSeconds;
            String Timestamp = time.ToString();
            */
            // .net core 获取时间戳
            DateTime dt = new DateTime(1970, 1, 1,0, 0, 0, DateTimeKind.Utc);
            int time = (int)(DateTime.Now.AddHours(-8) - dt).TotalSeconds;
            String Timestamp = time.ToString();
            //随机正整数,用于防止重放攻击
            Random rd = new Random();
            int rd_i = rd.Next();
            String nonce = Convert.ToString(rd_i);
            //SecretId
            String SecretId = "";
            //参数(用于编码)
            String PostStr = string.Format("Action=DescribeLVBChannelList&Nonce={0}&Region=bj&SecretId={1}&Timestamp={2}", nonce, SecretId, Timestamp);
            //地址
            String url = "https://live.api.qcloud.com/v2/index.php";
            //编码
            UTF8Encoding enc = new UTF8Encoding();
            String qm = "POSTlive.api.qcloud.com/v2/index.php"  + "?" + PostStr;
            byte[] dataToHash = enc.GetBytes(qm);
            /*
            var sha1 = SHA1.Create();
            var result = sha1.ComputeHash(dataToHash);
            */  
            HMACSHA1 hmac = new HMACSHA1()
            {
                Key = enc.GetBytes("")//SecretKey
            };
            var result = hmac.ComputeHash(dataToHash);
            string Signature = Convert.ToBase64String(result);
            //完整参数
            var completeUrl = string.Format("Action=DescribeLVBChannelList&Nonce={0}&Region=bj&SecretId={1}&Signature={2}&Timestamp={3}", nonce, SecretId, Signature, Timestamp);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ProtocolVersion = HttpVersion.Version10;
            byte[] data = Encoding.UTF8.GetBytes(completeUrl);
            request.ContentLength = data.Length;
            Stream newStream = request.GetRequestStream();
            newStream.Write(data, 0, data.Length);
            newStream.Close();
            HttpWebResponse response = null;
            int httpStatus = 200;
            string content;
            try
            {
                response = (HttpWebResponse)request.GetResponse();
                httpStatus = (int)response.StatusCode;
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                content = reader.ReadToEnd();
            }
            catch (WebException e)
            {
                response = (HttpWebResponse)e.Response;
                httpStatus = (int)response.StatusCode;
                using (Stream errData = response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(errData))
                    {
                        content = reader.ReadToEnd();
                    }
                }
            }

 

转载于:https://www.cnblogs.com/cc-net/p/9590246.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值