为了个人信息的安全,很多网站都有短信发送的功能,究竟是怎么实现的呢?对于个人站长来说的话,通过使用sms短信通api接口相对比较划算和简单。那怎么实现呢,步骤如下:
1. 从网上(http://sms.webchinese.cn/)申请账号,记住用户名,密码会发到手机上,这仅是登陆密码。注册后会送5条短信、和3条彩信的发送量。
2.查看sms短信通api下行接口(http://sms.webchinese.cn/api.shtml),然后获取秘钥,其实就是加密后的登录密码。开始敲代码,相关代码如下:
using system;
using system.collections.generic;
using system.io;
using system.linq;
using system.net;
using system.text;
using system.web;
namespace y_postsms
{
public class ymethod
{
private string the_uid = "" ; //用户名
private string the_key = "" ; //接口秘钥
/// <summary>返回utf-8编码发送接口地址</summary>
/// <param name="receivephonenumber">目的手机号码(多个手机号请用半角逗号隔开)</param>
/// <param name="receivesms">短信内容,最多支持400个字,普通短信70个字/条,长短信64个字/条计费</param>
/// <returns></returns>
public string getposturl( string smsmob, string smstext)
{
string posturl = "http://utf8.sms.webchinese.cn/?uid=" + the_uid + "&key=" + the_key + "&smsmob=" + smsmob + "&smstext=" + smstext;
return posturl;
}
/// <summary> 发送短信,得到返回值</summary>
public string postsmsinfo( string url)
{
//调用时只需要把拼成的url传给该函数即可。判断返回值即可
string strret = null ;
if (url == null || url.trim().tostring() == "" )
{
return strret;
}
string targeturl = url.trim().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;
webresponse hs = hr.getresponse();
stream sr = hs.getresponsestream();
streamreader ser = new streamreader(sr, encoding. default );
strret = ser.readtoend();
}
catch (exception ex)
{
strret = null ;
}
return strret;
}
/// <summary>确认返回信息 </summary>
public string getresult( string strret)
{
int result = 0;
try
{
result = int .parse(strret);
switch (result)
{
case -1:
strret = "没有该用户账户" ;
break ;
case -2:
strret = "接口密钥不正确,不是账户登陆密码" ;
break ;
case -21:
strret = "md5接口密钥加密不正确" ;
break ;
case -3:
strret = "短信数量不足" ;
break ;
case -11:
strret = "该用户被禁用" ;
break ;
case -14:
strret = "短信内容出现非法字符" ;
break ;
case -4:
strret = "手机号格式不正确" ;
break ;
case -41:
strret = "手机号码为空" ;
break ;
case -42:
strret = "短信内容为空" ;
break ;
case -51:
strret = "短信签名格式不正确,接口签名格式为:【签名内容】" ;
break ;
case -6:
strret = "ip限制" ;
break ;
default :
strret = "发送短信数量:" + result;
break ;
}
}
catch (exception ex)
{
strret = ex.message;
}
return strret;
}
}
}
3. 找在线客服开通发送权限,填写好签名,效果图如下:
通过以上图文并茂的方式给大家介绍了c#怎么实现手机短信发送功能,希望大家喜欢。
dy("nrwz");