主頁(yè) > 知識(shí)庫(kù) > 阿里大魚簡(jiǎn)單發(fā)送短信功能.net core版

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

熱門標(biāo)簽:旅游地圖標(biāo)注線路 地圖標(biāo)注位置怎么弄圖 漯河外呼調(diào)研線路 電銷專用外呼線路 400電話唐山辦理 廣西房產(chǎn)智能外呼系統(tǒng)推薦 威力最大的電銷機(jī)器人 電銷外呼系統(tǒng)是違法的嗎 電話機(jī)器人鑰匙扣

阿里大魚還未提供 .net core 版SDK,但提供了相關(guān)API,下面是.net core版實(shí)現(xiàn),只是簡(jiǎ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 PostT>(string url, string data, Encoding encoding)
    {
      try
      {
        var result = Post(url, data, encoding);
        return JsonConvert.DeserializeObjectT>(result);
      }
      catch (Exception ex)
      {
        return default(T);
      }
    }

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

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

      foreach (KeyValuePairstring, 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(IDictionarystring, string> parameters, string secret, string signMethod)
    {
      //把字典按Key的字母順序排序
      IDictionarystring, string> sortedParams = new SortedDictionarystring, string>(parameters, StringComparer.Ordinal);

      //把所有參數(shù)名和參數(shù)值串在一起
      StringBuilder query = new StringBuilder();
      if (Constants.SIGN_METHOD_MD5.Equals(signMethod))
      {
        query.Append(secret);
      }
      foreach (KeyValuePairstring, 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, Dictionarystring, string> parsms)
    {
      var txtParams = new SortedDictionarystring, 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 = PostSmsResultAli>(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ā)送短信時(shí):          

var parms = new Dictionarystring, 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);

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • 通過第三方接口發(fā)送短信驗(yàn)證碼/短信通知(推薦)
  • NodeJS實(shí)現(xiàn)阿里大魚短信通知發(fā)送
  • asp.net調(diào)用飛信免費(fèi)發(fā)短信(測(cè)試有效)
  • ASP.NET(VB)寫的后臺(tái)發(fā)送短信實(shí)現(xiàn)代碼
  • C#代碼實(shí)現(xiàn)短信驗(yàn)證碼接口示例
  • C#怎么實(shí)現(xiàn)手機(jī)短信發(fā)送功能
  • java、php、C#、asp實(shí)現(xiàn)短信群發(fā)功能的方法
  • CDMA 貓用AT命令發(fā)中文短信(C#)
  • ASP.NET MVC阿里大于短信接口開發(fā)短信群發(fā)能

標(biāo)簽:湖北 無(wú)錫 湘西 試駕邀約 銅陵 焦作 欽州 綏化

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《阿里大魚簡(jiǎn)單發(fā)送短信功能.net core版》,本文關(guān)鍵詞  阿里,大魚,簡(jiǎn)單,發(fā)送,短信,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《阿里大魚簡(jiǎn)單發(fā)送短信功能.net core版》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于阿里大魚簡(jiǎn)單發(fā)送短信功能.net core版的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章