Melt cloud Send SMS verification SMS (. NET version)

Source: Internet
Author: User

First of all, this requirement is in MVC5, then add Webapi interface first.

usingSystem;usingSystem.Web.Http;usingSystem.Net.Http;usingSystem.Web.Script.Serialization;usingSystem.Text;usingsystem.web;namespaceblacknails.controllers{ Public Abstract classWebapi2basecontroller:apicontroller { PublicHttpContext context =HttpContext.Current;  Public Statichttpresponsemessage ToJson (Object obj) {String str; if(obj isString | | Obj isChar) {STR=obj.            ToString (); }            Else{JavaScriptSerializer Serializer=NewJavaScriptSerializer (); STR=Serializer.            Serialize (obj); } httpresponsemessage result=Newhttpresponsemessage {Content =NewStringcontent (str, encoding.getencoding ("UTF-8"),"Application/json") }; returnresult; }         Public stringOptions () {return NULL;//HTTP response with empty body        }    }}
View Code

Then add a Usercontroller interface document

usingBlacknails.commonclass;usingblacknails.controllers;usingBlacknails.dal;usingSystem;usingSystem.IO;usingSystem.Net;usingSystem.Net.Http;usingSystem.Text;usingSystem.Web.Http;usingSystem.Web.Script.Serialization;namespaceblacknails.webapi{ Public classUsercontroller:webapi2basecontroller {[HttpGet] PublicHttpresponsemessage Sendcode (stringmobile) {            varResponse =NewResponse (); Random R=NewRandom (); inti = R.next (10000,99999); stringRandom =i.tostring (); TimeSpan TS= Datetime.now-datetime.parse ("1970-1-1"); stringTimestamp =Convert.ToInt32 (TS. totalseconds).            ToString (); //stored in byte mode            byte[] data = Encoding.Default.GetBytes (Constant.app_secret + Random +Timestamp); System.Security.Cryptography.SHA1 SHA1=NewSystem.Security.Cryptography.SHA1CryptoServiceProvider (); //Get hash value            byte[] result =Sha1.computehash (data); //convert to a string display            stringSignature = bitconverter.tostring (Result). Replace ("-",""); WebRequest Request= WebRequest.Create ("Http://api.sms.ronghub.com/sendCode.json"); Request. Method="POST"; stringPostData ="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=NewStreamReader (DataStream); stringResponsefromserver =Reader.            ReadToEnd (); //var responsefromserver = "{\" code\ ": 200,\" sessionid\ ": \" 7jvyzhqjav19w0p4e389tg\ "}";JavaScriptSerializer Jsonserializer =NewJavaScriptSerializer (); Returnjson _returnjson= jsonserializer.deserialize<returnjson>(Responsefromserver); varCode =_returnjson.code; varSessionId =_returnjson.sessionid; if(Code = = $) {Response. Code=0; Response. Message="Send verification code successfully! "; Context.            Cache.Insert (Mobile, sessionId); } Else{Response. Code=1; Response. Message="failed to send verification code! "; Context. Cache.Insert (Mobile,""); } reader.            Close ();            Datastream.close (); Resp.            Close (); Response. Data=NULL; returnToJson (response); } [HttpGet] PublicHttpresponsemessage Verifycode (stringMobilestringcode) {            varResponse =NewResponse (); Random R=NewRandom (); inti = R.next (10000,99999); stringRandom =i.tostring (); TimeSpan TS= Datetime.now-datetime.parse ("1970-1-1"); stringTimestamp =Convert.ToInt32 (TS. totalseconds).            ToString (); //stored in byte mode            byte[] data = Encoding.Default.GetBytes (Constant.app_secret + Random +Timestamp); System.Security.Cryptography.SHA1 SHA1=NewSystem.Security.Cryptography.SHA1CryptoServiceProvider (); //Get hash value            byte[] result =Sha1.computehash (data); //convert to a string display            stringSignature = bitconverter.tostring (Result). Replace ("-",""); WebRequest Request= WebRequest.Create ("Http://api.sms.ronghub.com/verifyCode.json"); Request. Method="POST"; stringPostData ="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 DataStream=request.            GetRequestStream (); Datastream.write (ByteArray,0, bytearray.length);            Datastream.close (); WebResponse resp=request.            GetResponse (); DataStream=resp.            GetResponseStream (); StreamReader Reader=NewStreamReader (DataStream); stringResponsefromserver =Reader.            ReadToEnd (); JavaScriptSerializer Jsonserializer=NewJavaScriptSerializer (); Returnjson _returnjson= jsonserializer.deserialize<returnjson>(Responsefromserver); varRtncode =_returnjson.code; varRtnsuccess =_returnjson.success; if(Rtncode = = $) {Response. Code=0; Response. Message="Mobile phone number verification success! "; }            Else{Response. Code=1; Response. Message="phone number verification failed! "; } reader.            Close ();            Datastream.close (); Resp.            Close (); Response. Data=rtnsuccess; returnToJson (response); }    }}
View Code

Melt cloud Send SMS verification SMS (. NET version)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.