Alibaba big fish simple text message function. net core version, Ali core
Alibaba big fish hasn't provided the. net core SDK yet, but provides related APIs. The following is the implementation of. net core, which is just a simple text message sending function:
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 = ut F-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 (writable t Ion 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; // ignore the parameter 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 {// read the HTTP Response stream = rsp as a response stream. getResponseStream (); reader = ne W StreamReader (stream, encoding); return reader. ReadToEnd () ;}finally {// release the resource 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) {// sort the dictionary by Key in alphabetical order. IDictionary <string, string> sortedParams = new SortedDictionary <string, string> (parameters, StringComparer. ordinal); // string all parameter names and parameter numbers together StringBuilder query = new StringBuilder (); if (Constants. SIGN_METHOD_MD5.Equals (signMethod) {query. append (sec Ret);} foreach (KeyValuePair <string, string> kv in sortedParams) {if (! String. IsNullOrEmpty (kv. Key )&&! String. isNullOrEmpty (kv. value) {query. append (kv. key ). append (kv. value) ;}/// use MD5/HMAC to encrypt 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 ;}}}
When sending a text message:
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_0000000"); var req = SmsHelper. sendSms ("http://gw.api.taobao.com/router/rest", "appKey", "appSecret", DateTime. now, parms );
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.