发送短信
using System;
using System.Configuration;
using System.IO;
using System.Net;
using System.Text;
using Newtonsoft.Json.Linq;
using OSharp.Utility.Secutiry;
namespace Len.CarManager.Utility
{
public static class SmsHelper
{
/// <summary>
/// 发送短信
/// </summary>
/// <param name="phone"></param>
/// <param name="message"></param>
/// <returns></returns>
public static bool SendMessage(string phone, string message)
{
var appSet = ConfigurationManager.AppSettings;
var flag = appSet["Verification-apiChange"];
if (flag == "0")
{
string url = appSet["Verification-smsUrl"],
username = appSet["Verification-smsUserName"],
password = appSet["Verification-smsPassword"],
postfix = appSet["Verification-smsPostfix"];
string auth = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(username + ":" + password)),
uploadString = string.Format("mobile={0}&message={1}", phone, message + postfix),
str;
byte[] byteArray = Encoding.UTF8.GetBytes(uploadString);
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));
webRequest.Headers.Add("Authorization", auth);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = byteArray.Length;
using (Stream newStream = webRequest.GetRequestStream())
{
newStream.Write(byteArray, 0, byteArray.Length);
}
using (var response = (HttpWebResponse)webRequest.GetResponse())
{
var stream = response.GetResponseStream();
if (stream == null) return false;
using (var sr = new StreamReader(stream, Encoding.Default))
{
str = sr.ReadToEnd();
}
stream.Dispose();
}
JObject jo = JObject.Parse(str);
int err = Convert.ToInt32(jo["error"]);
return err == 0;
}
else
{
//接口请求地址
var url = appSet["Verification-eumsUrl"];
//签名
var postfix = appSet["Verification-eumsPostfix"];
//帐号,由网关分配
var name = appSet["Verification-eumsUserName"];
//密码
var password = appSet["Verification-eumsPassword"];
//当前时间,格式:YYYYMMDD HHMISS,例如:20130806102030。客户时间早于或晚于网关时间超过30分钟,则网关拒绝提交。
var seed = DateTime.Now.ToString("yyyyMMddHHmmss");
//md5(md5(password) + seed) )
//其中“+”表示字符串连接。即:先对密码进行md5加密,将结果与seed值合并,再进行一次md5加密。
//两次md5加密后字符串都需转为小写。
////key
//手机号码(多个号码用“半角逗号”分开),GET方式每次最多100个号码,POST方式号码个数不限制,但建议不超过3万个
var dest = phone;
//短信内容。最多500个字符。
var content = message + postfix;
//扩展号码(视通道是否支持扩展,可以为空或不填)
//var ext = "shcry";
var keyMd5 = HashHelper.GetMd5(password, Encoding.GetEncoding("GBK"));
var key = HashHelper.GetMd5(keyMd5 + seed, Encoding.GetEncoding("GBK"));
var postStr = string.Format("name={0}&seed={1}&key={2}&dest={3}&content={4}", name, seed, key, dest, content);
var result = HttpHelper.PostData(url, postStr, Encoding.GetEncoding("GBK"));
if (result.Contains("success"))
return true;
return false;
}
}
/// <summary>
/// 发送语音验证码
/// </summary>
/// <param name="phone"></param>
/// <param name="code">验证码(4位数字)</param>
/// <returns></returns>
public static bool SendVoiceMessage(string phone, string code)
{
var appSet = ConfigurationManager.AppSettings;
string url = appSet["Verification-voiceUrl"],
username = appSet["Verification-smsUserName"],
password = appSet["Verification-voicePassword"];
string auth = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(username + ":" + password)),
uploadString = string.Format("mobile={0}&code={1}", phone, code), str;
byte[] byteArray = Encoding.UTF8.GetBytes(uploadString);
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));
webRequest.Headers.Add("Authorization", auth);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = byteArray.Length;
using (Stream newStream = webRequest.GetRequestStream())
{
newStream.Write(byteArray, 0, byteArray.Length);
}
using (var response = (HttpWebResponse)webRequest.GetResponse())
{
var stream = response.GetResponseStream();
if (stream == null) return false;
using (var sr = new StreamReader(stream, Encoding.Default))
{
str = sr.ReadToEnd();
}
stream.Dispose();
}
JObject jo = JObject.Parse(str);
int err = Convert.ToInt32(jo["error"].ToString());
return err == 0;
}
}
}