您的位置:首页 > 其它

用web程序发送短信的方法

2011-01-25 16:23 141 查看
很简单,往网址post参数即可,写好了方法,测试没有问题。最后与短信公司签了协议后,给了我们一个操作平台,这个就用不上了,这里把代码保存下来,也许今后能借鉴。

/// <summary>
/// 发送短信,并接收返回信息
/// </summary>
/// <returns></returns>
string ResponseValue(string mobilenumber)
{
string str;
//测试帐号
string username = "Testuser";
//测试密码
string password = "TestPassword";
//手机号码
string mobile = mobilenumber;
//发送的内容
string content = TextBox8.Text.Trim(); //内容可以自己写
//密码做MD5加密处理
password = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password + mobile, "MD5").ToLower();
//内容做url编码处理
content = Server.UrlEncode(content);
//得到完整的post字符串参数
string param = "userName=" + username + "&password=" + password + "&mobile=" + mobile + "&content=" + content;
byte[] bs = Encoding.ASCII.GetBytes(param);
//生成Request对象
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url); //URL测试网址,需要完整全名显示。包括端口
//传参方法post或者 get
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = bs.Length;
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
}
using (WebResponse wr = req.GetResponse())
{
Stream data = wr.GetResponseStream();
StreamReader read = new StreamReader(data);
str= read.ReadToEnd(); //接收返回参数,判断是否发送成功
read.Close();
data.Close();
}
return str;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: