网易云信,发送验证码短信C#版代码

网易云信发送短信代码(C# 版)。。。。需要注意SHA1 String有转换小写!!!!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace HaiMou.Common.SMS
{
    public class NeteaseSms
    {
        static string url = "https://api.netease.im/sms/sendcode.action";
        static string appKey = "************************************";
        static string appSecret = "******";

        public static string Send(string mobile)
        {
            string nonce = new Random().Next(100000, 999999).ToString();
            string curTime = DateTime.Now.ToUnixStamp().ToString();
            string checkSum = SHA1_Hash(appSecret+ nonce+ curTime);

            string post = $"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 (var wsr = wReq.GetRequestStream())
            {
                wsr.Write(btBodys, 0, btBodys.Length);
            }

            System.Net.WebResponse wResp = wReq.GetResponse();
            System.IO.Stream respStream = wResp.GetResponseStream();

            string result;
            using (System.IO.StreamReader reader = new System.IO.StreamReader(respStream,System.Text.Encoding.UTF8))
            {
                result = reader.ReadToEnd();
            }
            //Json数据,obj是网易生成的验证码
            return result;
        }

        private static string SHA1_Hash(string str_sha1_in)
        {
            SHA1 sha1 = new SHA1CryptoServiceProvider();
            byte[] bytes_sha1_in = UTF8Encoding.Default.GetBytes(str_sha1_in);
            byte[] bytes_sha1_out = sha1.ComputeHash(bytes_sha1_in);
            string str_sha1_out = BitConverter.ToString(bytes_sha1_out);
            str_sha1_out = str_sha1_out.Replace("-", "").ToLower();
            return str_sha1_out;
        }

        private static string getFormattedText(byte[] bytes)
        {
            int len = bytes.Length;
            StringBuilder buf = new StringBuilder(len * 2);
            for (int j = 0; j < len; j++)
            {
                buf.Append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);
                buf.Append(HEX_DIGITS[bytes[j] & 0x0f]);
            }
            return buf.ToString();
        }

        private static char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    }
}

 

posted on 2016-09-04 23:39 海眸1990 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/Zendic/p/5840786.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值