mvc 短信验证发送
? ? ? ? 這個是短信驗證發送一個例子,你們可以找其他的短信平臺。這里只是告訴你們一個使用方法。
第零步:到個短信平臺注冊,我這個平臺是?http://www.ihuyi.com/? 先聲明沒有絲毫的打廣告的意思,因為它有免費的短信可以? ? ? ? ? ? ? ? ? 使用。
第一步:首先是先去下圖這?add? 開頭的2個鏈接 復制到你項目的Web.config文件中就行了。
?<configuration>
?? ?<appSettings>
?? ??? ?<add key="WebReference.Service.PostUrl" value="http://106.ihuyi.cn/webservice/sms.php?method=Submit"/>
?? ?<add key="WebReference.sms" value="http://106.ihuyi.cn/webservice/sms.php?smsService"/>
?? ?</appSettings>
?? ?<connectionStrings/>
?第二步:構建發送號碼的html,這里隨便發揮,只要把號碼mobile這個傳過來就行。
?第三步:邏輯層接收傳過來的參數(號碼),然后把? ?APIID? 和 APIKEY? 修改一下。
? ? ? ? ? ? ? ?驗證碼的隨機數也是可以限制它的范圍,我這里是100000到999999之間。
? //信息發送
? ? ? ? public static string PostUrl = ConfigurationManager.AppSettings["WebReference.Service.PostUrl"];
? ? ? ? public ActionResult send_new(string mobile)
? ? ? ? {
? ? ? ? ? ? string account = "APIID";//用戶名是登錄用戶中心->驗證碼、通知短信->帳戶及簽名設置->APIID
? ? ? ? ? ? string password = "APIKEY"; //密碼是請登錄用戶中心->驗證碼、通知短信->帳戶及簽名設置->APIKEY
? ? ? ? ? ?
? ? ? ? ? ? Random rad = new Random();
? ? ? ? ? ? int mobile_code = rad.Next(100000, 999999);//驗證碼的隨機數
? ? ? ? ? ? string content = "您的驗證碼是:" + mobile_code + " 。請不要把驗證碼泄露給其他人。";
? ? ? ? ? ? string postStrTpl = "account={0}&password={1}&mobile={2}&content={3}";
? ? ? ? ? ? UTF8Encoding encoding = new UTF8Encoding();
? ? ? ? ? ? byte[] postData = encoding.GetBytes(string.Format(postStrTpl, account, password, mobile, content));
? ? ? ? ? ? HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(PostUrl);
? ? ? ? ? ? myRequest.Method = "POST";
? ? ? ? ? ? myRequest.ContentType = "application/x-www-form-urlencoded";
? ? ? ? ? ? myRequest.ContentLength = postData.Length;
? ? ? ? ? ? Stream newStream = myRequest.GetRequestStream();
? ? ? ? ? ? // Send the data.
? ? ? ? ? ? newStream.Write(postData, 0, postData.Length);
? ? ? ? ? ? newStream.Flush();
? ? ? ? ? ? newStream.Close();
? ? ? ? ? ? HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
? ? ? ? ? ? if (myResponse.StatusCode == HttpStatusCode.OK)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
? ? ? ? ? ? ? ? //Response.Write(reader.ReadToEnd());
? ? ? ? ? ? ? ? string res = reader.ReadToEnd();
? ? ? ? ? ? ? ? int len1 = res.IndexOf("</code>");
? ? ? ? ? ? ? ? int len2 = res.IndexOf("<code>");
? ? ? ? ? ? ? ? string code = res.Substring((len2 + 6), (len1 - len2 - 6));
? ? ? ? ? ? ? ? //Response.Write(code);
? ? ? ? ? ? ? ? int len3 = res.IndexOf("</msg>");
? ? ? ? ? ? ? ? int len4 = res.IndexOf("<msg>");
? ? ? ? ? ? ? ? string msg = res.Substring((len4 + 5), (len3 - len4 - 5));
? ? ? ? ? ? ? ? Response.Write(msg);
? ? ? ? ? ? ? ? Response.End();
? ? ? ? ? ? ? ? return Json(msg, JsonRequestBehavior.AllowGet);
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //訪問失敗
? ? ? ? ? ? ? ? return Json("", JsonRequestBehavior.AllowGet);
? ? ? ? ? ? }
? ? ? ? }
?
? ? ? ?這個平臺也可以使用發送信息,你們也可以直接去平臺哪里下載一個說明書之類的文件里面有詳細的資料和其他的語言。
小編能力不足只是做了MVC的信息發送,這個是要連接網絡才可以發送消息,它只是一個代替你發送信息的平臺。
總結
以上是生活随笔為你收集整理的mvc 短信验证发送的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎么删除网站?
- 下一篇: 材料专业高薪酬方向,材料专业转行可以做什