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}®ion=bj&secretid={1}×tamp={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}®ion=bj&secretid={1}&signature={2}×tamp={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(); } } }
上一篇: 大连首个银行服务机器人“上岗”