主页 > 知识库 > 阿里大鱼简单发送短信功能.net core版

阿里大鱼简单发送短信功能.net core版

热门标签:旅游地图标注线路 地图标注位置怎么弄图 漯河外呼调研线路 电销专用外呼线路 400电话唐山办理 广西房产智能外呼系统推荐 威力最大的电销机器人 电销外呼系统是违法的吗 电话机器人钥匙扣

阿里大鱼还未提供 .net core 版SDK,但提供了相关API,下面是.net core版实现,只是简单发送短信功能: 

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;
        // 忽略参数名或参数值为空的参数
        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响应
        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);

      //把所有参数名和参数值串在一起
      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; }
  }
}

发送短信时:          

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);

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
  • 通过第三方接口发送短信验证码/短信通知(推荐)
  • NodeJS实现阿里大鱼短信通知发送
  • asp.net调用飞信免费发短信(测试有效)
  • ASP.NET(VB)写的后台发送短信实现代码
  • C#代码实现短信验证码接口示例
  • C#怎么实现手机短信发送功能
  • java、php、C#、asp实现短信群发功能的方法
  • CDMA 猫用AT命令发中文短信(C#)
  • ASP.NET MVC阿里大于短信接口开发短信群发能

标签:湖北 无锡 湘西 试驾邀约 铜陵 焦作 钦州 绥化

巨人网络通讯声明:本文标题《阿里大鱼简单发送短信功能.net core版》,本文关键词  阿里,大鱼,简单,发送,短信,;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 下面列出与本文章《阿里大鱼简单发送短信功能.net core版》相关的同类信息!
  • 本页收集关于阿里大鱼简单发送短信功能.net core版的相关信息资讯供网民参考!
  • 推荐文章