用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;
}
/// <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;
}
相关文章推荐
- .NET 2.0 下的邮件发送程序 Web.Mail
- Android基础教程之----SMS简单发送短信程序(两个模拟器之间的通信)!
- Android发送短信方法总结
- 发送短信的2种方法
- 发送短信方法
- android 几种发送短信的方法
- android发送短信并监听插入收件箱的方法
- 关于各种文件用Editplus的方式打开出现“向程序发送命令时出现问题”的解决方法
- 在vs2008中使用AJAX开发.net 2.0下的Web程序的方法
- WIN7中OFFICE2007出现“向程序发送命令时出现问题”的解决方法
- C#编写的邮件(Mail)发送发送人的信息从web.config或者从Appp.congfig中获取的方法
- 使用客户端软件向服务端php程序发送post数据,php接受三种方法
- 短信发送程序
- android之发送短信的方法研究
- 微信开发之移动手机WEB页面(HTML5)Javascript实现一键拨号及短信发送
- WinForm 程序Post GEt web程序方法
- WEB程序打包的详细方法,并自动创建数据库
- 自己改的AT89S51单片机控制GSM模块(M22)发送短信程序,在Keil上编译没错,但还未试过烧入片内运行。。
- php天翼开放平台短信发送接口实现方法
- Android开发(一)发送短信程序