Rongyun sends text message verification text message (. net version) and text message. net
First, this requirement is carried out in MVC5, and the mobile phone number is verified through the paid cloud service, and the project is also separated from the front and back, so WEBAPI2 is added, then the WEBAPI interface is added first.
using System;using System.Web.Http;using System.Net.Http;using System.Web.Script.Serialization;using System.Text;using System.Web;namespace BlackNails.Controllers{ public abstract class WebAPI2BaseController : ApiController { public HttpContext context = HttpContext.Current; public static HttpResponseMessage toJson(Object obj) { String str; if (obj is String || obj is Char) { str = obj.ToString(); } else { JavaScriptSerializer serializer = new JavaScriptSerializer(); str = serializer.Serialize(obj); } HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(str, Encoding.GetEncoding("UTF-8"), "application/json") }; return result; } public string Options() { return null; // HTTP 200 response with empty body } }}
Then add a UserController interface document
Using BlackNails. commonClass; using BlackNails. controllers; using BlackNails. DAL; using System. IO; using System. net; using System. net. http; using System. text; using System. web. http; using System. web. script. serialization; namespace BlackNails. webAPI {public class UserController: WebAPI2BaseController {[HttpGet] public HttpResponseMessage sendCode (string mobile) {var response = new Response (); Random r = new Random (); int I = r. next (10000,999 99); string Random = I. toString (); TimeSpan ts = DateTime. now-DateTime. parse ("1970-1-1"); string Timestamp = Convert. toInt32 (ts. totalSeconds ). toString (); // byte [] data = Encoding. default. getBytes (Constant. APP_SECRET + Random + Timestamp); System. security. cryptography. SHA1 sha1 = new System. security. cryptography. SHA1CryptoServiceProvider (); // obtain the hash value byte [] result = sha1.ComputeHash (data); // convert it to string display string Signature = BitConverter. toString (result ). replace ("-", ""); WebRequest request = WebRequest. create (" http://api.sms.ronghub.com/sendCode.json "); Request. method = "POST"; string postData = "mobile =" + mobile + "& templateId =" + Constant. TEMPLATE_REGISTER + "& region = 86"; byte [] byteArray = Encoding. UTF8.GetBytes (postData); request. contentType = "application/x-www-form-urlencoded"; request. contentLength = byteArray. length; request. headers. add ("App-Key", Constant. APP_KEY); request. headers. add ("Nonce", Random); request. headers. add ("Timestamp", Timestamp); request. headers. add ("Signature", Signature); Stream dataStream = request. getRequestStream (); dataStream. write (byteArray, 0, byteArray. length); dataStream. close (); WebResponse resp = request. getResponse (); dataStream = resp. getResponseStream (); StreamReader reader = new StreamReader (dataStream); string responseFromServer = reader. readToEnd (); JavaScriptSerializer jsonSerializer = new JavaScriptSerializer (); ReturnJson _ ReturnJson = jsonSerializer. deserialize <ReturnJson> (responseFromServer); var code = _ ReturnJson. code; var sessionId = _ ReturnJson. sessionId; if (code = 200) {response. code = 0; response. message = "Verification Code sent successfully! "; Context. Cache. Insert (mobile, sessionId);} else {response. Code = 1; response. Message =" failed to send the verification Code! "; Context. cache. insert (mobile, "");} reader. close (); dataStream. close (); resp. close (); response. data = null; return toJson (response);} [HttpGet] public HttpResponseMessage verifyCode (string mobile, string code) {var response = new Response (); random r = new Random (); int I = r. next (10000,999 99); string Random = I. toString (); TimeSpan ts = DateTime. now-DateTime. parse ("1970-1-1"); string Timestamp = Convert. toInt32 (ts. totalSeconds ). toString (); // byte [] data = Encoding. default. getBytes (Constant. APP_SECRET + Random + Timestamp); System. security. cryptography. SHA1 sha1 = new System. security. cryptography. SHA1CryptoServiceProvider (); // obtain the hash value byte [] result = sha1.ComputeHash (data); // convert it to string display string Signature = BitConverter. toString (result ). replace ("-", ""); WebRequest request = WebRequest. create (" http://api.sms.ronghub.com/verifyCode.json "); Request. method = "POST"; string postData = "sessionId =" + context. cache [mobile] + "& code =" + code; byte [] byteArray = Encoding. UTF8.GetBytes (postData); request. contentType = "application/x-www-form-urlencoded"; request. headers. add ("App-Key", Constant. APP_KEY); request. headers. add ("Nonce", Random); request. headers. add ("Timestamp", Timestamp); request. headers. add ("Signature", Signature); Stream dat AStream = request. getRequestStream (); dataStream. write (byteArray, 0, byteArray. length); dataStream. close (); WebResponse resp = request. getResponse (); dataStream = resp. getResponseStream (); StreamReader reader = new StreamReader (dataStream); string responseFromServer = reader. readToEnd (); JavaScriptSerializer jsonSerializer = new JavaScriptSerializer (); ReturnJson _ ReturnJson = jsonSerializer. deser Ialize <ReturnJson> (responseFromServer); var rtnCode = _ ReturnJson. code; var rtnSuccess = _ ReturnJson. success; if (rtnCode = 200) {response. code = 0; response. message = "mobile phone number Verification Successful! ";}Else {response. Code = 1; response. Message =" mobile phone number verification failed! ";} Reader. Close (); dataStream. Close (); resp. Close (); response. Data = rtnSuccess; return toJson (response );}}}