最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
當前位置: 首頁 - 科技 - 知識百科 - 正文

阿里大魚簡單發(fā)送短信功能.net core版

來源:懂視網(wǎng) 責編:小采 時間:2020-11-27 22:36:48
文檔

阿里大魚簡單發(fā)送短信功能.net core版

阿里大魚簡單發(fā)送短信功能.net core版:阿里大魚還未提供 .net core 版SDK,但提供了相關(guān)API,下面是.net core版實現(xiàn),只是簡單發(fā)送短信功能: using System; using System.Collections.Generic; using System.IO; using System.Net; using System.S
推薦度:
導讀阿里大魚簡單發(fā)送短信功能.net core版:阿里大魚還未提供 .net core 版SDK,但提供了相關(guān)API,下面是.net core版實現(xiàn),只是簡單發(fā)送短信功能: using System; using System.Collections.Generic; using System.IO; using System.Net; using System.S

阿里大魚還未提供 .net core 版SDK,但提供了相關(guān)API,下面是.net core版實現(xiàn),只是簡單發(fā)送短信功能: 

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using Newtonsoft.Json;

namespace ConsoleApp1
{
 public class SmsHelper
 {
 public static string Post(string url, string data, Encoding encoding)
 {
 try
 {
 HttpWebRequest req = WebRequest.CreateHttp(new Uri(url));
 req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
 req.Method = "POST";
 req.Accept = "text/xml,text/javascript";
 req.ContinueTimeout = 60000;

 byte[] postData = encoding.GetBytes(data);
 Stream reqStream = req.GetRequestStreamAsync().Result;
 reqStream.Write(postData, 0, postData.Length);
 reqStream.Dispose();

 var rsp = (HttpWebResponse)req.GetResponseAsync().Result;
 var result = GetResponseAsString(rsp, encoding);
 return result;
 }
 catch (Exception ex)
 {
 throw;
 }
 }

 public static T Post<T>(string url, string data, Encoding encoding)
 {
 try
 {
 var result = Post(url, data, encoding);
 return JsonConvert.DeserializeObject<T>(result);
 }
 catch (Exception ex)
 {
 return default(T);
 }
 }

 public static string BuildQuery(IDictionary<string, string> parameters)
 {
 if (parameters == null || parameters.Count == 0)
 {
 return null;
 }

 StringBuilder query = new StringBuilder();
 bool hasParam = false;

 foreach (KeyValuePair<string, string> kv in parameters)
 {
 string name = kv.Key;
 string value = kv.Value;
 // 忽略參數(shù)名或參數(shù)值為空的參數(shù)
 if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(value))
 {
  if (hasParam)
  {
  query.Append("&");
  }

  query.Append(name);
  query.Append("=");
  query.Append(WebUtility.UrlEncode(value));
  hasParam = true;
 }
 }

 return query.ToString();
 }

 public static string GetResponseAsString(HttpWebResponse rsp, Encoding encoding)
 {
 Stream stream = null;
 StreamReader reader = null;

 try
 {
 // 以字符流的方式讀取HTTP響應(yīng)
 stream = rsp.GetResponseStream();
 reader = new StreamReader(stream, encoding);
 return reader.ReadToEnd();
 }
 finally
 {
 // 釋放資源
 if (reader != null) reader.Dispose();
 if (stream != null) stream.Dispose();
 if (rsp != null) rsp.Dispose();
 }
 }

 public static string GetAlidayuSign(IDictionary<string, string> parameters, string secret, string signMethod)
 {
 //把字典按Key的字母順序排序
 IDictionary<string, string> sortedParams = new SortedDictionary<string, string>(parameters, StringComparer.Ordinal);

 //把所有參數(shù)名和參數(shù)值串在一起
 StringBuilder query = new StringBuilder();
 if (Constants.SIGN_METHOD_MD5.Equals(signMethod))
 {
 query.Append(secret);
 }
 foreach (KeyValuePair<string, string> kv in sortedParams)
 {
 if (!string.IsNullOrEmpty(kv.Key) && !string.IsNullOrEmpty(kv.Value))
 {
  query.Append(kv.Key).Append(kv.Value);
 }
 }

 //使用MD5/HMAC加密
 if (Constants.SIGN_METHOD_HMAC.Equals(signMethod))
 {
 return Hmac(query.ToString(), secret);
 }
 else
 {
 query.Append(secret);
 return Md5(query.ToString());
 }
 }

 public static string Hmac(string value, string key)
 {
 byte[] bytes;
 using (var hmac = new HMACMD5(Encoding.UTF8.GetBytes(key)))
 {
 bytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(value));
 }
 StringBuilder result = new StringBuilder();
 foreach (byte t in bytes)
 {
 result.Append(t.ToString("X2"));

 }
 return result.ToString();
 }

 public static string Md5(string value)
 {
 byte[] bytes;
 using (var md5 = MD5.Create())
 {
 bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(value));
 }
 var result = new StringBuilder();
 foreach (byte t in bytes)
 {
 result.Append(t.ToString("X2"));
 }
 return result.ToString();
 }

 public static SmsResultAli SendSms(string url, string appKey, string appSecret, DateTime timestamp, Dictionary<string, string> parsms)
 {
 var txtParams = new SortedDictionary<string, string>();
 txtParams.Add(Constants.METHOD, "alibaba.aliqin.fc.sms.num.send");
 txtParams.Add(Constants.VERSION, "2.0");
 txtParams.Add(Constants.SIGN_METHOD, Constants.SIGN_METHOD_HMAC);
 txtParams.Add(Constants.APP_KEY, appKey);
 txtParams.Add(Constants.FORMAT, "json");
 txtParams.Add(Constants.TIMESTAMP, timestamp.ToString(Constants.DATE_TIME_FORMAT));
 txtParams.Add(Constants.SMS_TYPE, "normal");
 foreach (var item in parsms)
 {
 txtParams.Add(item.Key,item.Value);
 }

 txtParams.Add(Constants.SIGN, GetAlidayuSign(txtParams, appSecret, Constants.SIGN_METHOD_HMAC));
 var result = Post<SmsResultAli>(url, BuildQuery(txtParams), Encoding.UTF8);

 return result;
 }

 }

 public class SmsResultAli
 {
 public SmsResponseALi Alibaba_Aliqin_Fc_Sms_Num_Send_Response { get; set; }
 }

 public class SmsResponseALi
 {
 public string Request_Id { get; set; }
 public SmsResponseResultAli Result { get; set; }
 }

 public class SmsResponseResultAli
 {
 public string Err_Code { get; set; }

 public string Model { get; set; }

 public bool Success { get; set; }
 }
}

發(fā)送短信時:          

var parms = new Dictionary<string, string>();
 parms.Add(Constants.EXTEND, "123456");
 parms.Add(Constants.REC_NUM, "138********");
 parms.Add(Constants.SMS_FREE_SIGN_NAME, "阿里大魚");
 parms.Add(Constants.SMS_PARAM, "{\"code\":\"1234\",\"product\":\"阿里大魚\"}");
 parms.Add(Constants.SMS_TEMPLATE_CODE, "SMS_10000000");

var req = SmsHelper.SendSms("http://gw.api.taobao.com/router/rest", "appKey", "appSecret", DateTime.Now, parms);

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

阿里大魚簡單發(fā)送短信功能.net core版

阿里大魚簡單發(fā)送短信功能.net core版:阿里大魚還未提供 .net core 版SDK,但提供了相關(guān)API,下面是.net core版實現(xiàn),只是簡單發(fā)送短信功能: using System; using System.Collections.Generic; using System.IO; using System.Net; using System.S
推薦度:
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top