服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - ASP.NET教程 - 阿里大鱼简单发送短信功能.net core版

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

2020-03-11 14:57韩天伟 ASP.NET教程

这篇文章主要介绍了阿里大鱼发送短信功能.net core版,.net core版实现,只是简单发送短信功能,供大家参考,感兴趣的小伙伴们可以参考一下

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

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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; }
  }
}

发送短信时:          

?
1
2
3
4
5
6
7
8
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);

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

延伸 · 阅读

精彩推荐