zoukankan      html  css  js  c++  java
  • 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();
                        }
                    }
                }
  • 相关阅读:
    修改jquery里的dialog对话框插件为框架页(iframe)
    实现滚动广告的几种方案
    dojo学习二 ajax异步请求之绑定列表
    用jquery的sortable做自定义网站模块
    封装自己的js提示信息jtip办法
    让你的网站下起雨(js特效)
    关于js左侧多级菜单动态的解决方案
    dojo学习三 grid表格扩展学习
    jquery最新插件Autocomplete搜索自动提示功能
    用javascript面向对象的方式制作弹出层
  • 原文地址:https://www.cnblogs.com/cc-net/p/9590246.html
Copyright © 2011-2022 走看看