static void Main(string[] args) { TimeSpan ts = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1); Int32 ticks = System.Convert.ToInt32(ts.TotalSeconds); //当前UTC时间戳,从1970年1月1日0点0 分0 秒开始到现在的秒数(String) String curTime = ticks.ToString(); String appSecret = "ec4573c39376"; //随机数(最大长度128个字符) string nonce = "12345"; string random = new Random().ToString(); //SHA1(AppSecret + Nonce + CurTime),三个参数拼接的字符串,进行SHA1哈希计算,转化成16进制字符(String,小写) String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce, curTime); string url = "https://api.netease.im/sms/sendtemplate.action"; url += "?templateid=3063716&mobiles=[\"18611693213\"]¶ms=[\"7点15分\"]";//306371 var http = NetworkRequest.CreateHttp(url); http.Request.Headers["AppKey"] = "78d74476f41e6d9bf1429118bf5316bd"; http.Request.Headers["Nonce"] = nonce; http.Request.Headers["CurTime"] = curTime; http.Request.Headers["CheckSum"] = checkSum; http.Request.Headers["ContentType"] = "application/x-www-form-urlencoded;charset=utf-8"; var bytes = http.Post(); if (bytes != null) { string str = Encoding.UTF8.GetString(bytes); Console.WriteLine(str); } Console.ReadLine(); }
//curl -X POST -H "AppKey: go9dnk49bkd9jd9vmel1kglw0803mgq3" -H "CurTime: 1443592222" //-H "CheckSum: 9e9db3b6c9abb2e1962cf3e6f7316fcc55583f86" -H "Nonce: 4tgggergigwow323t23t" //-H "Content-Type: application/x-www-form-urlencoded" //-d 'mobile=13812345678' 'https://api.netease.im/sms/sendcode.action' public static string sendMsgYanZheng(string appKey, string appSecret, string mobile, string content) { if (string.IsNullOrEmpty(mobile) || mobile.Length != 11 || !mobile.StartsWith("1")) { return("-1"); } String url = "https://api.netease.im/nimserver/user/create.action"; url += "?accid=helloworld"; //TO开发者:accid填要创建的id appKey = "04aacac596f7783dbb6c3a26891826cc"; //TO开发者:假数据,具体参考开发文档 appSecret = "d86ce66940cd"; //TO开发者:假数据,具体参考开发文档 String nonce = "12345"; //TO开发者:具体参考开发文档 TimeSpan ts = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1); Int32 ticks = System.Convert.ToInt32(ts.TotalSeconds); String curTime = ticks.ToString(); String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce, curTime); // 设置请求的header IDictionary <object, String> headers = new Dictionary <object, String>(); headers["AppKey"] = appKey; headers["Nonce"] = nonce; headers["CurTime"] = curTime; headers["CheckSum"] = checkSum; headers["ContentType"] = "application/x-www-form-urlencoded;charset=utf-8"; string responseFromServer = ""; string code = ""; //responseFromServer = HttpClient.HttpPost(url, null, headers); //code = StringHelper.CutString(responseFromServer, "\"code\":", ","); //if (code == "200") { string token = StringHelper.CutString(responseFromServer, "\"token\":\"", "\""); string accid = StringHelper.CutString(responseFromServer, "\"accid\":\"", "\""); url = "https://api.netease.im/sms/sendcode.action?1=1"; url += "&mobile=" + mobile; //18605887655";//TO开发者:accid填要创建的id //url += "&templateid=1";//TO开发者:accid填要创建的id responseFromServer = HttpClient.HttpPost(url, null, headers); code = StringHelper.CutString(responseFromServer, "\"code\":", ","); string obj = StringHelper.CutString(responseFromServer, "\"obj\":\"", "\""); string msg = StringHelper.CutString(responseFromServer, "\"msg\":\"", "\""); return(obj); } return(""); }
/// <summary> /// 验证数据同步请求是否合法 /// </summary> /// <returns></returns> private bool ValidateRequest() { string _curTime = Request["CurTime"], _md5 = Request["MD5"], _checkSum = Request["CheckSum"]; _data = GetRequestBodyData(); if (string.IsNullOrEmpty(_curTime) || string.IsNullOrEmpty(_md5) || string.IsNullOrEmpty(_checkSum) || string.IsNullOrEmpty(_data)) { return(false); } string _expectedCheckSum = CheckSumBuilder.GetCheckSum(SdkProfile.AppSecret, _md5, _curTime), _expectedMd5 = CheckSumBuilder.GetMD5(_data); return(_md5.Equals(_expectedMd5) && _checkSum.Equals(_expectedCheckSum)); }