欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#调用腾讯云API的实例

程序员文章站 2022-05-13 22:45:47
//获取时间戳 .net framework /* DateTime dt = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); int time = (int)(DateTime.Now - dt).TotalSecon... ......
       //获取时间戳 .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("")
            };
            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();
                    }
                }
            }