讓您全面了解并上手億速云產品
常見入門級使用教程
對外 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();
}
}
}