NetEase Cloud Letter Send SMS Code (C # Version) .... Note SHA1 string has conversion lowercase!!!!
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Security.Cryptography;usingSystem.Text;usingSystem.Threading.Tasks;namespacehaimou.common.sms{ Public classneteasesms {Static stringURL ="https://api.netease.im/sms/sendcode.action"; Static stringAppKey ="************************************"; Static stringAppsecret ="******"; Public Static stringSend (stringmobile) { stringNonce =NewRandom (). Next (100000,999999). ToString (); stringCurtime =DateTime.Now.ToUnixStamp (). ToString (); stringCheckSum = Sha1_hash (appsecret+ nonce+curtime); stringPost = $"Mobile={mobile}"; byte[] Btbodys =Encoding.UTF8.GetBytes (POST); System.Net.WebRequest WReq=System.Net.WebRequest.Create (URL); Wreq.method="POST"; WREQ.HEADERS.ADD ("AppKey", AppKey); WREQ.HEADERS.ADD ("Nonce", nonce); WREQ.HEADERS.ADD ("Curtime", Curtime); WREQ.HEADERS.ADD ("CheckSum", CheckSum); Wreq.contentlength=btbodys.length; Wreq.contenttype="Application/x-www-form-urlencoded;charset=utf-8"; using(varWSR =Wreq.getrequeststream ()) {WSR. Write (Btbodys,0, btbodys.length); } System.Net.WebResponse Wresp=Wreq.getresponse (); System.IO.Stream Respstream=Wresp.getresponsestream (); stringresult; using(System.IO.StreamReader reader =NewSystem.IO.StreamReader (Respstream,system.text.encoding.utf8)) {Result=Reader. ReadToEnd (); } //JSON data, obj is the verification code generated by NetEase returnresult; } Private Static stringSha1_hash (stringstr_sha1_in) {SHA1 SHA1=NewSHA1CryptoServiceProvider (); byte[] bytes_sha1_in =UTF8Encoding.Default.GetBytes (str_sha1_in); byte[] Bytes_sha1_out =Sha1.computehash (bytes_sha1_in); stringStr_sha1_out =bitconverter.tostring (bytes_sha1_out); Str_sha1_out= Str_sha1_out. Replace ("-",""). ToLower (); returnstr_sha1_out; } Private Static stringGetformattedtext (byte[] bytes) { intLen =bytes. Length; StringBuilder buf=NewStringBuilder (len *2); for(intj =0; J < Len; J + +) {buf. Append (hex_digits[(bytes[j)>>4) &0x0f]); Buf. Append (Hex_digits[bytes[j]&0x0f]); } returnbuf. ToString (); } Private Static Char[] Hex_digits = {'0','1','2','3','4','5','6','7','8','9','a','b','C','D','e','F' }; }}
NetEase cloud Letter, send verification code SMS C # version code