using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
namespace Utils
{
///
/// 手机短信发送
///
public class Sms
{
#region 私有函数
//创建一个随机数实例
private static Random ro = new Random();
private static string getDictionaryData(Dictionary data)
{
string ret = null;
foreach (KeyValuePair item in data)
{
if (item.Value != null && item.Value.GetType() == typeof(Dictionary))
{
ret += item.Key.ToString() + "={";
ret += getDictionaryData((Dictionary)item.Value);
ret += "};";
}
else
{
ret += item.Key.ToString() + "=" + (item.Value == null ? "null" : item.Value.ToString()) + ";";
}
}
return ret;
}
#endregion
///
/// 发送短信
///
/// 用户身份标识
/// 要发送的手机号-暂不支持“,”分隔发送
///
/// 模板:1-您的验证码是:【变量】。请不要把验证码泄露给其他人。如非本人操作,可不用理会!
/// 模板2-您的注册验证码是:【变量】。请勿泄露他人。如非本人操作,可不用理会!欢迎使用融资通。
///
/// 要发送的验证码
///
public static string sMsSend(string token, string sPhone, string sTempIndex, string sCode)
{
Encoding encoding = Encoding.GetEncoding("utf-8");
string postData = "token=" + token + "&sSphone=" + sPhone + "&sTempIndex=" + sTempIndex + "&sCode=" + sCode;
byte[] data = encoding.GetBytes(postData);
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://apix.nbbuy.com/sms/sms.ashx");
myRequest.Method = "POST";
myRequest.Timeout = 10000;
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse();
StreamReader sreader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string read = sreader.ReadToEnd();
return read;
}
///
/// 互亿短信
///
/// 模板:1-您的验证码是:【变量】。请不要把验证码泄露给其他人。如非本人操作,可不用理会!
/// 模板2-您的注册验证码是:【变量】。请勿泄露他人。如非本人操作,可不用理会!欢迎使用融资通。
///
///
///
public static string SMSSendByhuyi(string mobile, string content)
{
string account = "cf_380568887";
string password = StringHelper.EncryptMD5("888888889");//密码可以使用明文密码或使用32位MD5加密
string postUrl = "http://106.ihuyi.cn/webservice/sms.php?method=Submit"; //短信接口地址
string postStrTpl = "account={0}&password={1}&mobile={2}&content={3}";
UTF8Encoding encoding = new UTF8Encoding();
byte[] postData = encoding.GetBytes(string.Format(postStrTpl, account, password, mobile, content));
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(postUrl);
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = postData.Length;
Stream newStream = myRequest.GetRequestStream();
// Send the data.
newStream.Write(postData, 0, postData.Length);
newStream.Flush();
newStream.Close();
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
if (myResponse.StatusCode == HttpStatusCode.OK)
{
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
//Response.Write(reader.ReadToEnd());
string res = reader.ReadToEnd();
int len1 = res.IndexOf("");
int len2 = res.IndexOf("");
string code = res.Substring((len2 + 6), (len1 - len2 - 6));
//Response.Write(code);
int len3 = res.IndexOf("");
int len4 = res.IndexOf("");
string msg = res.Substring((len4 + 5), (len3 - len4 - 5));
return msg;
}
else
{
return "发送失败";
//访问失败
}
}
///
/// 获取6位短信验证码
///
///
public static string GetSMSCode()
{
//获取一个6位的随机数
string rnd = ro.Next(100000, 999999).ToString();
//EventLog.WriteLog(rnd);
//赋值session
//HttpContext.Current.Session["SMSCode"] = rnd;
return rnd;
}
}
}