Ali big fish simply send SMS function. NET Core Edition _ Practical Tips

Source: Internet
Author: User
Tags foreach datetime hmac md5

Ali Big Fish has not yet provided the. NET Core Edition SDK, but has provided the relevant API, the following is the. NET Core Edition implementation, but simply sends the short message 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 encod
        ing) {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 = N ull | | 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; Parameters that omit the parameter name or null argument 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 {//Read the HTTP response stream = RSP in the form of character streams.
        GetResponseStream ();
        reader = new StreamReader (stream, encoding); Return reader.
      ReadToEnd (); finally {//release 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 SIGNM  Ethod) {//Idictionary<string dictionary by key alphabetical order, string> sortedparams = new sorteddictionary<string,

      string> (parameters, stringcomparer.ordinal);
      String all parameter names and values together 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); }///using MD5/HMAC encryption if (CONSTANTS.SIGN_METHOD_HMAC. Equals (Signmethod)) {return Hmac (query).
      ToString (), secret); } else {query.
        Append (secret); return Md5 (query).
      ToString ());
      The 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<s
      Tring, 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 text messages:

var parms = new dictionary<string, string> ();
  Parms. ADD (Constants.extend, "123456");
  Parms. ADD (Constants.rec_num, "138********");
  Parms. ADD (Constants.sms_free_sign_name, "Ali Big Fish");
  Parms. ADD (Constants.sms_param, "{\" code\ ": \" 1234\ "," product\ ": \" Ali Big Fish "}");
  Parms. ADD (Constants.sms_template_code, "sms_10000000");

var req = smshelper.sendsms ("Http://gw.api.taobao.com/router/rest", "Appkey", "Appsecret", DateTime.Now, parms);

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.