利用Web Service实现短信发送(整理)

新浪网Web Service,这个Service的地址是http://smsinter.sina.com.cn/ws/smswebservice0101.wsdl,Service中提供了一个发送短消息的方法:

string sendXml (carrier , userid , password , mobilenumber , content , msgtype )
  sendXml方法中的六个参数均为string类型,并且sendXml方法的返回值也是string类型。
  以下是sendXml方法中的六个参数的具体说明:
  ·carrier:运营商名称,具体使用时此参数并没有什么具体要求,即这里面可以随便输,输入的字符串也不会在对方手机中有任何显示。
  ·userid:在新浪网上注册的手机号,注册手机所用的地址是:http://sms.sina.com.cn,具体注册方法下面会具体介绍。
  ·password:您在新浪网成功注册手机后,新浪网所反馈来的密码。
  ·mobilenumber:要发送短信到对方的手机号码。
  ·content:所要发送短消息的内容。
  ·msgtype:发送短消息的类型,由于本文发送的不是彩信,所以输入"Text"。

  调用此Web Service只需在Visual C#开发环境中直接添加Web引用,把该地址输入即可,就可以使用此Web Service中的sendXml方法发送短消息了。当然发送短消息的机器必须接入互联网。

在新浪网上注册手机,新浪无线的地址:http://sms.sina.com.cn.,如果注册成功,移动梦网则会向你注册的手机上发送一个8位长度的口令,这个口令要保留,下面发送短信就要使用这个口令。

 

把Visual Studio .Net的当前窗口切换到Form1.vb的代码编辑窗口,并用下列代码替换Form1.cs中的InitializeComponent过程对应的代码,下列代码作用是初始化窗体中加入的组件:

View Code
private void InitializeComponent ( ) 
{
this.textBox1 = new System.Windows.Forms.TextBox ( ) ;
this.textBox2 = new System.Windows.Forms.TextBox ( ) ;
this.textBox3 = new System.Windows.Forms.TextBox ( ) ;
this.button1 = new System.Windows.Forms.Button ( ) ;
this.label1 = new System.Windows.Forms.Label ( ) ;
this.label2 = new System.Windows.Forms.Label ( ) ;
this.label3 = new System.Windows.Forms.Label ( ) ;
this.label4 = new System.Windows.Forms.Label ( ) ;
this.textBox4 = new System.Windows.Forms.TextBox ( ) ;
this.SuspendLayout ( ) ;
this.textBox1.Location = new System.Drawing.Point ( 144 , 16 ) ;
this.textBox1.Name = "textBox1" ;
this.textBox1.Size = new System.Drawing.Size ( 184 , 21 ) ;
this.textBox1.TabIndex = 0 ;
this.textBox1.Text = "" ;
this.textBox2.Location = new System.Drawing.Point ( 144 , 69 ) ;
this.textBox2.Name = "textBox2" ;
this.textBox2.PasswordChar = ''''''''*'''''''' ;
this.textBox2.Size = new System.Drawing.Size ( 184 , 21 ) ;
this.textBox2.TabIndex = 1 ;
this.textBox2.Text = "" ;
this.textBox3.Location = new System.Drawing.Point ( 144 , 122 ) ;
this.textBox3.Name = "textBox3" ;
this.textBox3.Size = new System.Drawing.Size ( 184 , 21 ) ;
this.textBox3.TabIndex = 2 ;
this.textBox3.Text = "" ;
this.button1.Location = new System.Drawing.Point ( 152 , 256 ) ;
this.button1.Name = "button1" ;
this.button1.Size = new System.Drawing.Size ( 80 , 32 ) ;
this.button1.TabIndex = 4 ;
this.button1.Text = "发送" ;
this.button1.Click += new System.EventHandler ( this.button1_Click ) ;
this.label1.Location = new System.Drawing.Point ( 56 , 24 ) ;
this.label1.Name = "label1" ;
this.label1.Size = new System.Drawing.Size ( 88 , 16 ) ;
this.label1.TabIndex = 5 ;
this.label1.Text = "注册手机号:" ;
this.label2.Location = new System.Drawing.Point ( 88 , 77 ) ;
this.label2.Name = "label2" ;
this.label2.Size = new System.Drawing.Size ( 72 , 16 ) ;
this.label2.TabIndex = 6 ;
this.label2.Text = "口令:" ;
this.label3.Location = new System.Drawing.Point ( 56 , 128 ) ;
this.label3.Name = "label3" ;
this.label3.Size = new System.Drawing.Size ( 96 , 16 ) ;
this.label3.TabIndex = 7 ;
this.label3.Text = "目标手机号:" ;
this.label4.Location = new System.Drawing.Point ( 96 , 176 ) ;
this.label4.Name = "label4" ;
this.label4.Size = new System.Drawing.Size ( 72 , 16 ) ;
this.label4.TabIndex = 8 ;
this.label4.Text = "内容:" ;
this.textBox4.Location = new System.Drawing.Point ( 144 , 175 ) ;
this.textBox4.Multiline = true ;
this.textBox4.Name = "textBox4" ;
this.textBox4.Size = new System.Drawing.Size ( 184 , 48 ) ;
this.textBox4.TabIndex = 3 ;
this.textBox4.Text = "" ;
this.AutoScaleBaseSize = new System.Drawing.Size ( 6 , 14 ) ;
this.ClientSize = new System.Drawing.Size ( 410 , 303 ) ;
this.Controls.Add ( this.button1 ) ;
this.Controls.Add ( this.textBox4 ) ;
this.Controls.Add ( this.textBox3 ) ;
this.Controls.Add ( this.textBox2 ) ;
this.Controls.Add ( this.textBox1 ) ;
this.Controls.Add ( this.label4 ) ;
this.Controls.Add ( this.label3 ) ;
this.Controls.Add ( this.label2 ) ;
this.Controls.Add ( this.label1 ) ;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle ;
this.MaximizeBox = false ;
this.Name = "Form1" ;
this.Text = "Visual C#实现短信发送" ;
this.ResumeLayout ( false ) ;
}

至此【短信】项目的界面设计和功能实现的准备工作就完成了,具体如图04所示:

9. 选中【短信】的【解决方法资源管理器】,右击【引用】,弹出【添加Web引用】对话框。在此对话框中的【URL】文本框中输入"http://smsinter.sina.com.cn/ws/smswebservice0101.wsdl"后,单击【转到】按钮,则会得到图05所示界面:

此时单击图04中的【添加引用】按钮,则成功实现在【短信】项目中添加可发送短信息的新浪网提供的Web Service。

把Visual Stuido .Net的当前窗口切换到Form1.cs的代码编辑窗口,并用下列代码替换From1.cs中的button1的Click事件对应的代码,下列代码的作用是调用引入的Web Service中提供的sendXml方法向指定手机发送短信息:

View Code
private void button1_Click ( object sender , System.EventArgs e ) 
{
 短信.cn.com.sina.smsinter.SMSWS ws = new 短信.cn.com.sina.smsinter.SMSWS ( ) ;
 string result = ws.sendXml ( "Sina" ,textBox1.Text ,textBox2.Text ,textBox3.Text ,textBox4.Text ,"new" ) ;
 MessageBox.Show ( result ) ;
} 

至此,在上述步骤都正确执行后,【短信】项目的全部工作就完成了。单击快捷键F5运行程序,在输入【注册手机号】、【口令】、【目标手机号】和【内容】后,单击【发送】按钮后,程序就会向指定的手机号发送短信息了。

提示:利用这种发送短信息并不是免费的午餐,其资费标准可参看新浪无线网站上的相关说明,每发送一条收费一角钱,从在新浪网注册的手机上收费。在使用本文介绍的方法发送短信息时,在发送完成后,一般会有一个延迟。这是因为后台采用了了消息队列机制,不过这种延迟一般只会有几秒钟的时间。

正好在网上看到了就简单的整理了一下,以便以后使用起来更加方便.

posted @ 2012-05-09 11:55  Aaronguo  阅读(1083)  评论(0编辑  收藏  举报