欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-10 22:24:16
阿里大鱼还未提供 .net core 版sdk,但提供了相关api,下面是.net core版实现,只是简单发送短信功能:  using syste...

阿里大鱼还未提供 .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 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;
        // 忽略参数名或参数值为空的参数
        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(idictionary<string, string> parameters, string secret, string signmethod)
    {
      //把字典按key的字母顺序排序
      idictionary<string, string> sortedparams = new sorteddictionary<string, string>(parameters, stringcomparer.ordinal);

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

发送短信时:          

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

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