1 static string url = "https://api.netease.im/sms/sendcode.action"; 2 static string appKey = ""; 3 static string appSecret = ""; 4 5 public static string Send(string mobile) 6 { 7 string nonce = new Random().Next(100000, 999999).ToString(); 8 string curTime = DateTime.Now.ToString("yyyyMMddhhmmss"); 9 string checkSum = SHA1_Hash(appSecret+ nonce+ curTime); 10 11 string post = string.Format("mobile={0}", mobile); 12 byte[] btBodys = Encoding.UTF8.GetBytes(post); 13 14 System.Net.WebRequest wReq = System.Net.WebRequest.Create(url); 15 wReq.Method = "POST"; 16 wReq.Headers.Add("AppKey", appKey); 17 wReq.Headers.Add("Nonce", nonce); 18 wReq.Headers.Add("CurTime", curTime); 19 wReq.Headers.Add("CheckSum", checkSum); 20 wReq.ContentLength = btBodys.Length; 21 wReq.ContentType = "application/x-www-form-urlencoded;charset=utf-8"; 22 23 using (var wsr = wReq.GetRequestStream()) 24 { 25 wsr.Write(btBodys, 0, btBodys.Length); 26 } 27 28 System.Net.WebResponse wResp = wReq.GetResponse(); 29 System.IO.Stream respStream = wResp.GetResponseStream(); 30 31 string result; 32 using (System.IO.StreamReader reader = new System.IO.StreamReader(respStream,System.Text.Encoding.UTF8)) 33 { 34 result = reader.ReadToEnd(); 35 } 36 //Json数据,obj是网易生成的验证码 37 return result; 38 } 39 40 private static string SHA1_Hash(string str_sha1_in) 41 { 42 SHA1 sha1 = new SHA1CryptoServiceProvider(); 43 byte[] bytes_sha1_in = UTF8Encoding.Default.GetBytes(str_sha1_in); 44 byte[] bytes_sha1_out = sha1.ComputeHash(bytes_sha1_in); 45 string str_sha1_out = BitConverter.ToString(bytes_sha1_out); 46 str_sha1_out = str_sha1_out.Replace("-", "").ToLower(); 47 return str_sha1_out; 48 } 49 50 private static string getFormattedText(byte[] bytes) 51 { 52 int len = bytes.Length; 53 StringBuilder buf = new StringBuilder(len * 2); 54 for (int j = 0; j < len; j++) 55 { 56 buf.Append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]); 57 buf.Append(HEX_DIGITS[bytes[j] & 0x0f]); 58 } 59 return buf.ToString(); 60 } 61 62 private static char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };