簡單實現發送手機短信

C#簡單實現發送手機短信

 

偶然想起,像編寫一個從電腦向手機發送短信的程序,從網上查找到有三種方式:(1)使用webservice接口發送手機短信,這個能夠使用sina提供的webservice進行發送,可是須要進行註冊;(2)使用短信mao的方式進行短信的發送,這種方式應該是比較的經常使用,前提是須要購買硬件設備,這個就不考慮了(3)使用中國網建提供的SMS短信平臺,可是,用完幾條免費的後,就要收費了。html

首先,我用C#實現第一種方法,發現老是錯誤,這個不解,後來從網上查找緣由,有的說,新浪這個功能已經不用了,我也不太清楚,就放棄了這種方法,java

後來實現了第三種方法。web

具體實現以下:編程

1. 從網上(http://sms.webchinese.cn/)申請帳號,記住用戶名,密碼會發到手機上,這僅是登錄密碼。裏面還有短信祕鑰,這個要獲得,這是後面要用到的,要在裏面寫好籤名,還有,具體實現,要參考SMS短信通API下行接口參數(http://sms.webchinese.cn/api.shtml),這個網頁上就有各類語言的實現方式,我用C#實現,熟悉java的能夠用java。
api

2. 如今就能夠編程實現了,這個也很簡單,參考接口參數網頁的C#實現便可,下面給出個人例子!post

其界面以下:url

因爲祕鑰有點長,這裏就不輸入了。spa

代碼以下:code

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 using System.Net;
10 using System.IO;
11 
12 namespace SendMsgSMS
13 {
14     public partial class Form1 : Form
15     {
16         private string url = "http://utf8.sms.webchinese.cn/?";
17         private string strUid = "Uid=";
18         private string strKey = "&key=*******************"; //這裏*表明祕鑰,因爲從長有點麻煩,就不在窗口上輸入了
19         private string strMob = "&smsMob=";
20         private string strContent = "&smsText=";
21         public Form1()
22         {
23             InitializeComponent();
24         }
25 
26         private void button1_Click(object sender, EventArgs e)
27         {
28             if (txtUerName.Text.ToString().Trim() != "" && txtAttnNum.Text.ToString().Trim() != "" &&txtContent.Text.ToString() != null) 
29             {
30                 url = url + strUid + txtUerName.Text + strKey + strMob + txtAttnNum.Text + strContent + txtContent.Text;
31                 string Result = GetHtmlFromUrl(url);
32 
33                 MessageBox.Show(Result);
34             }
35         }
36 
37         public string GetHtmlFromUrl(string url)
38         {
39             string strRet = null;
40             if (url == null || url.Trim().ToString() == "")
41             {
42                 return strRet;
43             }
44             string targeturl = url.Trim().ToString();
45             try
46             {
47                 HttpWebRequest hr = (HttpWebRequest)WebRequest.Create(targeturl);
48                 hr.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
49                 hr.Method = "GET";
50                 hr.Timeout = 30 * 60 * 1000;
51                 WebResponse hs = hr.GetResponse();
52                 Stream sr = hs.GetResponseStream();
53                 StreamReader ser = new StreamReader(sr, Encoding.Default);
54                 strRet = ser.ReadToEnd();
55             }
56             catch (Exception ex)
57             {
58                 strRet = null;
59             }
60             return strRet;
61         }
62     }
63 }
相關文章
相關標籤/搜索