C#利用Web Service實現短信發送

 通過編程方式實現短信息的發送對很多人來說是一件比較煩雜的事情,目前一般的解決方法是通過計算機和手機的連線,通過可對手機編程的語言編寫相關的手機短信息程序來實現,而這種方法對於一般人來說是很難達到的,因爲此種方法不僅要有很多必備的硬件設備,也還需懂得手機編程的相關知識。本文就來探討一下另外一種通過Visual C#發送短信息的簡單實現方法,這種方法在實現過程中比我們想象的要簡單許多,只需擁有Visual C#一般編程基礎,並確定您的計算機接入互聯網即可。下面就來詳細介紹一下Visual C#發送短信息的具體實現過程。

  一. Visual C#發送短信息的原理:

  我想當很多讀者一看到本文的題目一定會想本文內容一定非常深奧,並且作者一定知道了電信的發送短信的網關地址,其實並非如此。本文其實是利用一個現成的資源,一個可發送短信的Web Serviec。這個Web Service就是新浪網就提供的一個,可供用戶直接調用的發送短消息的Web Service。這個Service的地址是http://smsinter.sina.com.cn/ws/smswebservice0101.wsdl。在這個Service中提供了一個發送短消息的方法"sendXml"。此方法的語法格式如下:

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方法發送短消息了。當然發送短消息的機器必須接入互聯網。

  二.在新浪網上註冊手機

  按照如下步驟就可在新浪網上註冊手機:

  1. 打開瀏覽器,並在地址欄中輸入新浪無線的地址:http://sms.sina.com.cn.。在瀏覽器德左上角,可見如圖01界面:


圖01:新浪網註冊手機界面之一

  輸入手機號後,單擊圖01中的"登陸",如果你的手機沒有在新浪,則提示如圖02所示信息。


圖02:新浪網註冊手機界面之二


  2. 按照圖02中的選項輸入相應的信息後,單擊"登陸移動夢網"按鈕,如果註冊成功,移動夢網則會向你註冊的手機上發送一個8位長度的口令就可以得到圖03所示界面。


圖03:新浪網註冊手機界面之三

  這個口令要保留,下面發送短信就要使用這個口令。

  三.本文程序設計、調試和運行的環境:

  (1).微軟公司視窗2000服務器版。

  (2).Visual Studio .Net 2003企業構建版,.Net FrameWork SDK版本號4322。
  四.Visual C#實現短信息發送的具體實現步驟:

  Visual C#發送短信息的關鍵就是通過Web引用新浪網提供的發送短信息的Web Service,並在引用完成後。調用此Service的sendXml方法即可。以下就是Visual C#引用Web Service發送短信息的具體實現步驟:

  1. 啓動Visual Studio .Net。

  2. 選擇菜單【文件】|【新建】|【項目】後,彈出【新建項目】對話框。

  3. 將【項目類型】設置爲【Visual Basic項目】。

  4. 將【模板】設置爲【Windows應用程序】。

  5. 在【名稱】文本框中輸入【短信】。

  6. 在【位置】的文本框中輸入【E:/VS.NET項目】,然後單擊【確定】按鈕,這樣在"E:/VS.NET項目"目錄中就產生了名稱爲"短信"的文件夾,並在裏面創建了名稱爲"短信"的項目文件。

  7. 把Visual Studio .Net的當前窗口切換到【Form1.cs(設計)】窗口,並從【工具箱】中的【Windows窗體組件】選項卡中往Form1窗體中拖入下列組件,並執行相應的操作:

  四個Label組件。
  四個TextBox組件。

  一個Button組件,其作用是發送短信息。並在這個Button組件拖入Form1的設計窗體後,雙擊它,則系統會在Form1.cs文件分別產生這個組件的Click事件對應的處理代碼。

  8. 把Visual Studio .Net的當前窗口切換到Form1.vb的代碼編輯窗口,並用下列代碼替換Form1.cs中的InitializeComponent過程對應的代碼,下列代碼作用是初始化窗體中加入的組件:

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所示:


圖04:【短信】項目的設計界面

  9. 選中【短信】的【解決方法資源管理器】,右擊【引用】,彈出【添加Web引用】對話框。在此對話框中的【URL】文本框中輸入"http://smsinter.sina.com.cn/ws/smswebservice0101.wsdl"後,單擊【轉到】按鈕,則會得到圖05所示界面:


圖05:在【短信】項目中添加Web引用的界面

  此時單擊圖04中的【添加引用】按鈕,則成功實現在【短信】項目中添加可發送短信息的新浪網提供的Web Service。

  10. 把Visual Stuido .Net的當前窗口切換到Form1.cs的代碼編輯窗口,並用下列代碼替換From1.cs中的button1的Click事件對應的代碼,下列代碼的作用是調用引入的Web Service中提供的sendXml方法向指定手機發送短信息:

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 ) ;
}

  11. 至此,在上述步驟都正確執行後,【短信】項目的全部工作就完成了。單擊快捷鍵F5運行程序,在輸入【註冊手機號】、【口令】、【目標手機號】和【內容】後,單擊【發送】按鈕後,程序就會向指定的手機號發送短信息了。

  五.總結:

  本文介紹的這種Visual C#發送短信息方法,其解決關鍵是引用Web Service,調用此Web Service中的方法。通觀全文,此種方法是不是很簡單。最後還要提醒諸位,利用這種發送短信息並不是免費的午餐,其資費標準可參看新浪無線網站上的相關說明,每發送一條收費一角錢,從在新浪網註冊的手機上收費。在使用本文介紹的方法發送短信息時,在發送完成後,一般會有一個延遲。這是因爲後臺採用了了消息隊列機制,不過這種延遲一般只會有幾秒鐘的時間。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章