C# webserver實現短信發送(移動)

近端時間接了個需求在原來的OA辦公系統中添加一個發送短信功能。(既然須要發送短信那確定要申請一個發送短信的帳號,我這裏是以移動mas爲列子)web

c#的weserver須要選協議WS。其餘的基本不用怎麼填寫直接提交就能夠了。c#

在vs項目中添加服務器引用數組

其中須要選擇高級中的web引用服務器

添加完成後樣式ui

調用就簡單了編碼

  CmsLsMac.WsSmsServiceService cms=new CmsLsMac.WsSmsServiceService ();//短信接口
          
            myContent mycontent = new myContent();
            string md5= UserMd5("加密信息");

            mycontent.ecName = "企業名稱";
            mycontent.apId = "帳號";
            mycontent.secretKey = "密碼";
            //mycontent.mobiles = "電話";
            mycontent.content = "今天有雷陣雨";
            mycontent.sign = "簽名";
            mycontent.addSerial = "";//能夠爲null
            List<string> list = new List<string>();
            list.Add("電話");//具體有哪些電話號碼
            string mobezz = "";
            foreach (var item in list)
            {
                mobezz += item;
            }
            //md5加密
            mycontent.mac = UserMd5(mycontent.ecName + mycontent.apId + mycontent.secretKey + mobezz + mycontent.content + mycontent.sign);
           
            string str="<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<WsSubmitReq>" + "  <apId>" + mycontent.apId + "</apId>" + "  <secretKey>" + mycontent.secretKey + "</secretKey>"
             + "  <ecName>" + mycontent.ecName + "</ecName>" + "  <mobiles>" + GetMobiles(list)
             + "  </mobiles>" + "  <content>" + mycontent.content + "</content>" + "  <sign>" + mycontent.sign + "</sign>" + "  <addSerial>" + mycontent.addSerial + "</addSerial>"
             + "  <mac>" + mobezz  + "</mac>" + "</WsSubmitReq>";
       var returnz= cms.sendSms(str);//發送短信方法 根據返回值查看是否發送成功。

 

最主要的是須要知道接口那個方法是發送短信的。加密

 

MD5加密spa

public  string UserMd5(string str)
        {
            string cl = str;
            string pwd = "";
            MD5 md5 = MD5.Create();//實例化一個md5對像
            // 加密後是一個字節類型的數組,這裏要注意編碼UTF8/Unicode等的選擇 
            byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
            // 經過使用循環,將字節類型的數組轉換爲字符串,此字符串是常規字符格式化所得
            for (int i = 0; i < s.Length; i++)
            {
                // 將獲得的字符串使用十六進制類型格式。格式後的字符是小寫的字母,若是使用大寫(X)則格式後的字符是大寫字符 
                 pwd = pwd + s[i].ToString("x2");
             }
             return pwd;
        }     

 電話號碼循環的---可羣發code

        public string GetMobiles(List<string> list)
        {
            StringBuilder soap = new StringBuilder();
            foreach (var item in list)
	        {
                soap.Append("<string>" + item + "</string>");
	        }
            return soap.ToString();

        }
相關文章
相關標籤/搜索