让您全面了解并上手亿速云产品
常见入门级使用教程
对外 API 开发文档中心
您历史提交的工单
您的每一条意见,我们都严谨处理
您的每一条建议,我们都认真对待
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
namespace SmsDemo
{
class SendSms
{
private static Object generateSign(string signatureString, string secretKey, bool raw_output = false)
{
var enc = Encoding.UTF8;
HMACSHA1 hmac = new HMACSHA1(enc.GetBytes(secretKey));
hmac.Initialize();
byte[] buffer = enc.GetBytes(signatureString);
if (raw_output)
{
return hmac.ComputeHash(buffer);
}
else
{
return BitConverter.ToString(hmac.ComputeHash(buffer)).Replace("-", "").ToLower();
}
}
public static void send()
{
String url = "https://api.yisu.com/sms/sendSms";
String accessId = "*****";
String accessSecret = "*****";
Random rnd = new Random();
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
List<KeyValuePair<String, String>> paramList = new List<KeyValuePair<String, String>>();
paramList.Add(new KeyValuePair<String, String>("accessId", accessId));
paramList.Add(new KeyValuePair<String, String>("templateCode", "100001"));
paramList.Add(new KeyValuePair<String, String>("phone", "13800000000"));
paramList.Add(new KeyValuePair<String, String>("timestamp", Convert.ToInt64(ts.TotalSeconds).ToString()));
paramList.Add(new KeyValuePair<String, String>("nonce", rnd.Next(1, 1000000).ToString()));
paramList.Add(new KeyValuePair<String, String>("templateVars", "{\"code\":\"123456\",\"min\":\"5\"}"));
paramList.Sort(
delegate (KeyValuePair<String, String> p1, KeyValuePair<String, String> p2)
{
return p1.Key.CompareTo(p2.Key);
}
);
var param_str = "";
foreach (var param in paramList)
{
param_str += param.Key.ToString() + "=" + param.Value.ToString() + "&";
}
param_str = param_str.Substring(0, param_str.Length - 1);
byte[] sign = (byte[])generateSign(param_str, accessSecret, true);
paramList.Add(new KeyValuePair<String, String>("signature", Convert.ToBase64String(sign)));
HttpClient client = null;
HttpResponseMessage response = null;
try
{
client = new HttpClient();
response = client.PostAsync(url, new FormUrlEncodedContent(paramList)).Result;
String result = response.Content.ReadAsStringAsync().Result;
Console.WriteLine("result:{0}", result);
}
catch (Exception e)
{
Console.WriteLine("\nException Caught!");
Console.WriteLine("Message :{0} ", e.Message);
}
finally
{
if (null != response)
{
response.Dispose();
}
if (null != client)
{
client.Dispose();
}
}
}
static void Main(string[] args)
{
send();
}
}
}