分享一个学习到的短信接口。
首先创建一个类PostSms,这里我运用的接口是SMS短信通
/// <summary>
/// 发送验证码的函数
/// </summary>
/// <param name="UidName">SMS平台的用户名(CSY918)</param>
/// <param name="KeyMD5">接口密钥(d41d8cd98f00b204e980)</param>
/// <param name="Phone">手机号码</param>
/// <param name="TextContent">短信内容</param>
/// <returns></returns>
public static string SendSMSVeriCode(string UidName, string KeyMD5, string Phone, string TextContent)
{
string strUrl = "http://utf8.sms.webchinese.cn/?";//UTF-8编码Url
string strUidName = "Uid="; //SMS平台的用户名
string strKeyMD5 = "&key="; //SMS平台的接口密匙;strKeyMD5=接口密钥32位MD5加密(大写)
string strPhone = "&smsMob=";//目的手机号码
string strTextContent = "&smsText="; //短信内容;普通短信70个字/条
strUrl = strUrl + strUidName + UidName + strKeyMD5 + KeyMD5 + strPhone + Phone + strTextContent + TextContent;
string SMSResult = GetFromUrl(strUrl);
return SMSResult;
}
/// <summary>
/// 发送验证码获得返回值
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private static string GetFromUrl(string url)
{
string strReturn = null;
if (url == null || url.Trim().ToString() == "")
{
return strReturn;
}
else //手机号码正确
{
string targeturl = url.ToString();
try
{
HttpWebRequest hr = (HttpWebRequest)WebRequest.Create(targeturl);
hr.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
hr.Method = "GET";
hr.Timeout = 30 * 60 * 1000; //30分钟超时
WebResponse hs = hr.GetResponse();
Stream sr = hs.GetResponseStream();
StreamReader ser = new StreamReader(sr, Encoding.Default);
strReturn = ser.ReadToEnd();
}
catch (Exception ex)
{
strReturn = null;
//MessageBox.Show(ex.Message);
}
}
return strReturn;
}
/// <summary>
/// 发送验证码获得返回值
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private static string GetFromUrl(string url)
{
string strReturn = null;
if (url == null || url.Trim().ToString() == "")
{
return strReturn;
}
else //手机号码正确
{
string targeturl = url.ToString();
try
{
HttpWebRequest hr = (HttpWebRequest)WebRequest.Create(targeturl);
hr.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
hr.Method = "GET";
hr.Timeout = 30 * 60 * 1000; //30分钟超时
WebResponse hs = hr.GetResponse();
Stream sr = hs.GetResponseStream();
StreamReader ser = new StreamReader(sr, Encoding.Default);
strReturn = ser.ReadToEnd();
}
catch (Exception ex)
{
strReturn = null;
//MessageBox.Show(ex.Message);
}
}
return strReturn;
}
接下来就可以在事件里面写代码啦
string text = "尊敬的先生/女士,你好。";
//获取文本框中的数据
string phone = userphone.Trim();//手机号码
string smsTextContent = text;//SMS短信内容
string UidName = "wuyan2";//
string KeyMD5 = "d41d8cd98f00b204e980";//SMS平台的接口密钥
if (PostSms.CheckMobilePhone(phone) == false)//判断手机号码格式是否正确
{
Page.RegisterStartupScript("", "<script>alert('请将数据填写完整!')</script>");
}
else
{
string SMSResult = PostSms.SendSMSVeriCode(UidName, KeyMD5, phone, smsTextContent);
string JudetResult = PostSms.GetResult(SMSResult);
Page.RegisterStartupScript("", "<script>alert('消息发送成功!')</script>");
}