public class HttpClient2 { //发起Http请求 public static string HttpPost(string url, Stream data, IDictionary<object, string> headers = null) { System.Net.WebRequest request = HttpWebRequest.Create(url); request.Method = "POST"; if (data != null) request.ContentLength = data.Length; //request.ContentType = "application/x-www-form-urlencoded;charset=utf-8"; if (headers != null) { foreach (var v in headers) { if (v.Key is HttpRequestHeader) request.Headers[(HttpRequestHeader)v.Key] = v.Value; else request.Headers[v.Key.ToString()] = v.Value; } } HttpWebResponse response = null; try { // 得到响应 response = (HttpWebResponse)request.GetResponse(); // 显示状态 Console.WriteLine(response.StatusDescription); // 获取包含服务器返回的内容的流 Stream dataStream = response.GetResponseStream(); // 使用StreamReader打开该流,以方便访问 StreamReader reader = new StreamReader(dataStream); // 阅读的内容 string responseFromServer = reader.ReadToEnd(); //清理流和响应 reader.Close(); dataStream.Close(); response.Close(); return responseFromServer; } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(response.StatusDescription); return "0"; } } }
public class CheckSumBuilder { //发送短信验证码需要的类 public static String getCheckSum(String appSecret, String nonce, String curTime) { byte[] data = Encoding.Default.GetBytes(appSecret + nonce + curTime); byte[] result; SHA1 sha = new SHA1CryptoServiceProvider(); // 这是抽象类SHA1的一个实现 result = sha.ComputeHash(data); return getFormattedText(result); } // 计算并获取md5值 public static String getMD5(String requestBody) { if (requestBody == null) return null; // 创建MD5CryptoServiceProvider对象的新实例 MD5 md5Hasher = MD5.Create(); //将输入字符串转换为字节数组并计算散列 byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(requestBody)); // 创建一个新的Stringbuilder来收集字节并创建一个字符串 StringBuilder sBuilder = new StringBuilder(); //循环遍历散列数据的每个字节,并将每个字节格式化为十六进制字符串 for (int i = 0; i < data.Length; i++) { sBuilder.Append(data[i].ToString("x2")); } // 返回十六进制字符串 return getFormattedText(Encoding.Default.GetBytes(sBuilder.ToString())); } private static String getFormattedText(byte[] bytes) { char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; 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(); } }
public static string phoneyanzheng(string chaxunzhi) { String url = "https://api.netease.im/sms/sendcode.action"; url += "?templateid=3892478&mobile=" + chaxunzhi;//请输入正确的手机号 //网易云信分配的账号,请替换你在管理后台应用下申请的Appkey String appKey = "*"; //网易云信分配的密钥,请替换你在管理后台应用下申请的appSecret String appSecret = "*"; //随机数(最大长度128个字符) String nonce = "12345"; 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(); //SHA1(AppSecret + Nonce + CurTime),三个参数拼接的字符串,进行SHA1哈希计算,转化成16进制字符(String,小写) String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce, curTime); 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 Mess = HttpClient2.HttpPost(url, null, headers); var serializer = new JavaScriptSerializer();//这种方式则会很好的转化为 字典类型,如果JSON中不存在,则相应的值为 NULL var MessageInfo = serializer.Deserialize<Dictionary<string, object>>(Mess); if (MessageInfo["obj"] != null) { return "1"; } else { return "0"; } }
public static string phoneyanzheng2(string chaxunzhi,string code) { String url = "https://api.netease.im/sms/sendcode.action"; url += "?mobile=" + chaxunzhi + "&code="+code;//请输入正确的手机号 //网易云信分配的账号,请替换你在管理后台应用下申请的Appkey String appKey = "*"; //网易云信分配的密钥,请替换你在管理后台应用下申请的appSecret String appSecret = "*"; //随机数(最大长度128个字符) String nonce = "12345"; 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(); //SHA1(AppSecret + Nonce + CurTime),三个参数拼接的字符串,进行SHA1哈希计算,转化成16进制字符(String,小写) String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce, curTime); 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 Mess = HttpClient2.HttpPost(url, null, headers); var serializer = new JavaScriptSerializer();//这种方式则会很好的转化为 字典类型,如果JSON中不存在,则相应的值为 NULL var MessageInfo = serializer.Deserialize<Dictionary<string, object>>(Mess); if (MessageInfo["obj"] != null) { return "1"; } else { return "0"; } }