2. 按照圖02中的選項輸入相應的資訊後,單擊"登陸移動夢網"按鈕,如果註冊成功,移動夢網則會向你註冊的手機上發送一個8位長度的口令就可以得到圖03所示介面。
連結:http://dev.yesky.com/imagelist/06/03/dvnl2q1z8y2h.jpg
圖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所示:
連結:http://dev.yesky.com/imagelist/06/03/wz785b4af6d0.jpg
圖04:【簡訊】項目的設計介面
9. 選中【簡訊】的【解決方案資源管理員】,右擊【引用】,彈出【添加Web引用】對話方塊。在此對話方塊中的【URL】文字框中輸入"http://smsinter.sina.com.cn/ws/smswebservice0101.wsdl"後,單擊【轉到】按鈕,則會得到圖05所示介面:
http://dev.yesky.com/imagelist/06/03/qa7cz8sq56n7.jpg
圖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中的方法。通觀全文,此種方法是不是很簡單。最後還要提醒諸位,利用這種傳送簡訊息並不是免費的午餐,其資費標準可參看新浪無線網站上的相關說明,每發送一條收費一角錢,從在新浪網註冊的手機上收費。在使用本文介紹的方法傳送簡訊息時,在發送完成後,一般會有一個延遲。這是因為後台採用了了訊息佇列機制,不過這種延遲一般只會有幾秒鐘的時間。
轉http://www.chinacs.net/archives/5/2006/3591.html