短信接口

/// <summary>
/// 手机短信接口
/// </summary>
/// <param name="phone"></param>
/// <returns></returns>
public string yanzheng(string phone)
{
string code = "";
Random rd = new Random();


int num1 = rd.Next(0, 9);
int num2 = rd.Next(0, 9);
int num3 = rd.Next(0, 9);
int num4 = rd.Next(0, 9);

int[] nums = new int[4] { num1, num2, num3, num4 };
for (int j = 0; j < nums.Length; j++)
{
code += nums[j].ToString();
}
string num = code;
String url = System.Configuration.ConfigurationManager.AppSettings["duanxinurl"].ToString();
url += "?templateid=" + System.Configuration.ConfigurationManager.AppSettings["templateid"].ToString() + "&mobile=" + phone + "&authCode=" + num;//请输入正确的手机号


//网易云信分配的账号,请替换你在管理后台应用下申请的Appkey
String appKey = System.Configuration.ConfigurationManager.AppSettings["appKey"].ToString();
//网易云信分配的密钥,请替换你在管理后台应用下申请的appSecret
String appSecret = System.Configuration.ConfigurationManager.AppSettings["appSecret"].ToString();
//随机数(最大长度128个字符)
Random dd = new Random();
string _rm = dd.Next(10000).ToString();
String nonce = _rm;


TimeSpan ts = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1);
Int32 ticks = System.Convert.ToInt32(ts.TotalSeconds);
//当前UTC时间戳,从1970年1月1日0点0 分0 秒开始到现在的秒数(String)
String curTime = ticks.ToString();
//SHA1(AppSecret + Nonce + CurTime),三个参数拼接的字符串,进行SHA1哈希计算,转化成16进制字符(String,小写)
String checkSum = SMSHelper.CheckSumBuilder.getCheckSum(appSecret, nonce, curTime);


IDictionary<object, String> headers = new Dictionary<object, String>();
headers["AppKey"] = appKey;
headers["Nonce"] = nonce;
headers["CurTime"] = curTime;
headers["CheckSum"] = checkSum;
headers["ContentType"] = "application/x-www-form-urlencoded;charset=utf-8";
//执行Http请求
SMSHelper.HttpClient.HttpPost(url, null, headers);

return num;

}

     //帮助类

public static class SMSHelper
{
public static class CheckSumBuilder
{
// 计算并获取CheckSum
public static String getCheckSum(String appSecret, String nonce, String curTime)
{
byte[] data = Encoding.Default.GetBytes(appSecret + nonce + curTime);
byte[] result;

SHA1 sha = new SHA1CryptoServiceProvider();
// This is one implementation of the abstract class SHA1.
result = sha.ComputeHash(data);

return getFormattedText(result);
}

// 计算并获取md5值
public static String getMD5(String requestBody)
{
if (requestBody == null)
return null;

// Create a new instance of the MD5CryptoServiceProvider object.
MD5 md5Hasher = MD5.Create();

// Convert the input string to a byte array and compute the hash.
byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(requestBody));

// Create a new Stringbuilder to collect the bytes
// and create a string.
StringBuilder sBuilder = new StringBuilder();

// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}

// Return the hexadecimal string.
return getFormattedText(Encoding.Default.GetBytes(sBuilder.ToString()));
}

private static String getFormattedText(byte[] bytes)
{
char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
int len = bytes.Length;
StringBuilder buf = new StringBuilder(len * 2);
for (int j = 0; j < len; j++)
{
buf.Append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);
buf.Append(HEX_DIGITS[bytes[j] & 0x0f]);
}
return buf.ToString();
}
}

public class HttpClient
{
//发起Http请求
public static void HttpPost(string url, Stream data, IDictionary<object, string> headers = null)
{
System.Net.WebRequest request = HttpWebRequest.Create(url);
request.Method = "POST";
if (data != null)
request.ContentLength = data.Length;
//request.ContentType = "application/x-www-form-urlencoded;charset=utf-8";

if (headers != null)
{
foreach (var v in headers)
{
if (v.Key is HttpRequestHeader)
request.Headers[(HttpRequestHeader)v.Key] = v.Value;
else
request.Headers[v.Key.ToString()] = v.Value;
}
}
HttpWebResponse response = null;
try
{
// Get the response.
response = (HttpWebResponse)request.GetResponse();
// Display the status.
Console.WriteLine(response.StatusDescription);
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
// Cleanup the streams and the response.
reader.Close();
dataStream.Close();
response.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(response.StatusDescription);
}
}
}
}

猜你喜欢

转载自www.cnblogs.com/MenBe/p/10121724.html