NetCore Alibaba Cloud sends text messages and netcore sends text messages
If you use Alibaba Cloud APIs to send text messages, but Alibaba does not provide the NetCore API, you can view the source code and rewrite the text message section.
Public class MessageSender {private readonly string _ appKey; private readonly string _ appSecret; private readonly string _ serverUrl; public MessageSender (string url, string appKey, string appSecret) {_ serverUrl = url; _ appKey = appKey; _ appSecret = appSecret;} public string SmsType {get; set ;}= "normal"; public string SmsFreeSignName {get; set ;}= "birthday reminder "; public string SmsParam {get; set;} Public string RecNum {get; set;} public string SmsTemplateCode {get; set ;}= "SMS_24695114"; public string GetApiName () {return "alibaba. aliqin. fc. sms. num. send ";} public string SendMessage () {HttpClient httpClient = new HttpClient (); httpClient. defaultRequestHeaders. add ("Accept-Encoding", "gzip"); // httpClient. defaultRequestHeaders. add ("KeepAlive", "true"); httpClient. defaultRequestHeader S. add ("user-agent", "top-sdk-net"); httpClient. defaultRequestHeaders. add ("Method", "Post"); httpClient. defaultRequestHeaders. add ("Accept", "text/xml, text/javascript"); httpClient. defaultRequestHeaders. add ("Host", "gw.api.taobao.com"); httpClient. timeout = new TimeSpan (0, 0,100); byte [] postData = GetPostData (); HttpRequestMessage request = new HttpRequestMessage (HttpMethod. post, _ serverUrl); requ Est. headers. add ("Accept-Encoding", "gzip"); request. headers. accept. add (new MediaTypeWithQualityHeaderValue ("application/x-www-form-urlencoded"); request. headers. acceptCharset. add (new StringWithQualityHeaderValue ("UTF-8"); request. content = new StreamContent (new MemoryStream (postData); request. content. headers. contentType = new MediaTypeWithQualityHeaderValue ("application/x-www-form-urlencoded "); HttpResponseMessage httpResponseMessage = httpClient. sendAsync (request ). getAwaiter (). getResult (); string result = httpResponseMessage. content. readAsStringAsync (). getAwaiter (). getResult (); return result;} public byte [] GetPostData () {Dictionary <string, string> dictionary = new Dictionary <string, string> (); dictionary. add ("rec_num", RecNum); dictionary. add ("sms_free_sign_name", SmsFreeSignName ); Dictionary. add ("sms_param", SmsParam); dictionary. add ("sms_template_code", SmsTemplateCode); dictionary. add ("sms_type", SmsType); dictionary. add ("method", "alibaba. aliqin. fc. sms. num. send "); dictionary. add ("v", "2.0"); dictionary. add ("sign_method", "hmac"); dictionary. add ("app_key", _ appKey); dictionary. add ("format", "xml"); dictionary. add ("partner_id", "top-sdk-net-20160607"); dictionary. add ("time Stamp ", DateTime. now. toString ("yyyy-MM-dd HH: mm: ss"); // dictionary. add ("target_app_key", null); // dictionary. add ("session", null); dictionary. add ("sign", SignTopRequest (dictionary, null, _ appSecret, "hmac"); byte [] postData = Encoding. UTF8.GetBytes (BuildQuery (dictionary); return postData;} public string SignTopRequest (IDictionary <string, string> parameters, string body, string secret, string s IgnMethod) {// Step 1: sort the dictionary by Key in alphabetical order. IDictionary <string, string> sortedParams = new SortedDictionary <string, string> (parameters, StringComparer. ordinal); // Step 2: string all parameter names and parameter numbers together StringBuilder query = new StringBuilder (); if ("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) ;}} // Step 3: splice the request subject after the parameter if (! String. isNullOrEmpty (body) {query. append (body);} // Step 4: use MD5/HMAC to encrypt byte [] bytes; if ("hmac ". equals (signMethod) {HMACMD5 hmac = new HMACMD5 (Encoding. UTF8.GetBytes (secret); bytes = hmac. computeHash (Encoding. UTF8.GetBytes (query. toString ();} else {query. append (secret); MD5 md5 = MD5.Create (); bytes = md5.ComputeHash (Encoding. UTF8.GetBytes (query. toString ();} // Step 5: Convert the binary into a capitalized hexadecimal StringBu Ilder result = new StringBuilder (); foreach (byte t in bytes) {result. append (t. toString ("X2");} return result. toString ();} public 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; // ignore the parameter name or 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 ();}}
View Code
If used
MessageSender messageSender = new MessageSender ("http://gw.api.taobao.com/router/rest", "Your APPkey", "Your APPSecret"); messageSender. smsType = "normal"; messageSender. smsFreeSignName = "birthday reminder"; messageSender. smsParam = string. format ("{\" birthday \ ": \" {0} \ ", \" SpiritualBirthday \ ": \" {1} \ "}", birthdayTemplate, spiritualBirthdayTemplate); messageSender. recNum = phone; messageSender. smsTemplateCode = "Your SMS template"; string result = messageSender. sendMessage ();