融云发送手机短信验证短信(.net版本)
2018-06-22 06:09:28来源:未知 阅读 ()
首先本次需求是在MVC5中进行的,通过收费的融云服务来验证手机号码,而且本次的项目也是前后台分离,所以添加了WEBAPI2,那么先添加WEBAPI的接口
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 } } }
然后添加一个UserController接口文档
using BlackNails.CommonClass; using BlackNails.Controllers; using BlackNails.DAL; using System; 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, 99999); 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(); //得到哈希值 byte[] result = sha1.ComputeHash(data); //转换成为字符串的显示 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 + "®ion=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 = "发送验证码成功!"; context.Cache.Insert(mobile, sessionId); } else { response.Code = 1; response.Message = "发送验证码失败!"; 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, 99999); 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(); //得到哈希值 byte[] result = sha1.ComputeHash(data); //转换成为字符串的显示 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 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 rtnCode = _ReturnJson.code; var rtnSuccess = _ReturnJson.success; if (rtnCode == 200) { response.Code = 0; response.Message = "手机号验证成功!"; } else { response.Code = 1; response.Message = "手机号验证失败!"; } reader.Close(); dataStream.Close(); resp.Close(); response.Data = rtnSuccess; return toJson(response); } } }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- .net微信服务号发送红包 2020-02-27
- 利用.net代码实现发送邮件 2020-02-17
- .NET发送邮件遇到问题及解决方法 2020-01-18
- ASP.NET Core发送邮件的方法 2019-10-30
- ASP.NET Core 1.0实现邮件发送功能 2019-08-09
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash