C#怎樣完成手機短信發送功效。本站提示廣大學習愛好者:(C#怎樣完成手機短信發送功效)文章只能為提供參考,不一定能成為您想要的結果。以下是C#怎樣完成手機短信發送功效正文
為了小我信息的平安,許多網站都有短信發送的功效,畢竟是怎樣完成的呢?關於小我站長來講的話,經由過程應用SMS短信通API接口絕對比擬劃算和簡略。那怎樣完成呢,步調以下:
1. 從網上(http://sms.webchinese.cn/)請求賬號,記住用戶名,暗碼會發得手機上,這僅是上岸暗碼。注冊後會送5條短信、和3條彩信的發送量。
2.檢查SMS短信通API下行接口(http://sms.webchinese.cn/api.shtml),然後獲得秘鑰,其實就是加密後的登錄暗碼。開端敲代碼,相干代碼以下:
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Web; namespace Y_PostSms { public class YMethod { private string THE_UID = ""; //用戶名 private string THE_KEY = ""; //接口秘鑰 /// <summary>前往UTF-8編碼發送接口地址</summary> /// <param name="receivePhoneNumber">目標手機號碼(多個手機號請用半角逗號離隔)</param> /// <param name="receiveSms">短信內容,最多支撐400個字,通俗短信70個字/條,長短信64個字/條計費</param> /// <returns></returns> public string GetPostUrl(string smsMob, string smsText) { string postUrl = "http://utf8.sms.webchinese.cn/?Uid=" + THE_UID + "&key=" + THE_KEY + "&smsMob=" + smsMob + "&smsText=" + smsText; return postUrl; } /// <summary> 發送短信,獲得前往值</summary> public string PostSmsInfo(string url) { //挪用時只須要把拼成的URL傳給該函數便可。斷定前往值便可 string strRet = null; if (url == null || url.Trim().ToString() == "") { return strRet; } string targeturl = url.Trim().ToString(); try { HttpWebRequest hr = (HttpWebRequest)WebRequest.Create(targeturl); hr.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"; hr.Method = "GET"; hr.Timeout = 30 * 60 * 1000; WebResponse hs = hr.GetResponse(); Stream sr = hs.GetResponseStream(); StreamReader ser = new StreamReader(sr, Encoding.Default); strRet = ser.ReadToEnd(); } catch (Exception ex) { strRet = null; } return strRet; } /// <summary>確認前往信息 </summary> public string GetResult(string strRet) { int result = 0; try { result = int.Parse(strRet); switch (result) { case -1: strRet = "沒有該用戶賬戶"; break; case -2: strRet = "接口密鑰不准確,不是賬戶上岸暗碼"; break; case -21: strRet = "MD5接口密鑰加密不准確"; break; case -3: strRet = "短信數目缺乏"; break; case -11: strRet = "該用戶被禁用"; break; case -14: strRet = "短信內容湧現不法字符"; break; case -4: strRet = "手機號格局不准確"; break; case -41: strRet = "手機號碼為空"; break; case -42: strRet = "短信內容為空"; break; case -51: strRet = "短信簽名格局不准確,接口簽名格局為:【簽名內容】"; break; case -6: strRet = "IP限制"; break; default: strRet = "發送短信數目:" + result; break; } } catch (Exception ex) { strRet = ex.Message; } return strRet; } } }
3. 找在線客服守舊發送權限,填寫好簽名,後果圖以下:
經由過程以上圖文並茂的方法給年夜家引見了C#怎樣完成手機短信發送功效,願望年夜家愛好。