当前位置: 移动技术网 > IT编程>开发语言>.net > c#调用腾讯云API的实例

c#调用腾讯云API的实例

2018年09月05日  | 移动技术网IT编程  | 我要评论

国库券收藏,遗情书在线阅读,黎梦恬的男朋友

       //获取时间戳 .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();
                    }
                }
            }

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网