C# connect to the SMS interface of China Mobile

Posted by Paws on Sat, 19 Feb 2022 12:23:35 +0100

C# connect to the SMS interface of China Mobile

China Mobile SMS interface (publicity)

Mobile SMS platform address: http://mas.10086.cn/login , enter the user name and password to log in.
We usually need to dynamically display the content of SMS or add our own project links when using it, so we must contact your customer service manager to open template SMS.
The blogger also opened it after contacting,

The next step is to create a new template and review. Generally, the "applicable rules" can meet the requirements. If not, you have to ask the customer service manager to apply for large fields (personally, it's still troublesome. After all, it takes time to go through the process). After the creation is completed, submit it for review, and obtain the template ID after the review. The first step is completed.
Other parameters are required to call the interface. Let's move on to home page - Management - Interface Management - new interface. The blogger has built it, so I'll show you directly. The user name and password are set by myself. The ip address I set is the LAN ip of my server (it seems that this item is useless). For others, please refer to the blogger's settings. After completion, remember to download the signature of this interface, and the second step is completed.

The above configuration is very simple. It is to call the interface service. Take out the small book and record it. You can download the interface document in the website. The blogger adopts the HTTPS interface. The first task is to get the connection address (the blogger adds the beauty customer service QQ to get it), and then look at the message content (is it all, ha ha)

The preparation work is completed and the C# code is added. According to the requirements of this blog, there are many dynamic fields to be spliced,

 //Send SMS. phoneList is the LIST of SMS to be sent
                    for (int i = 0; i < phoneList.Count; i++) { 
                        StringBuilder paramss = new StringBuilder();
                        paramss.Append('[');
                        paramss.Append('"' + GuestUserNameList[i] + '"' + ',');
                        paramss.Append('"' + bdateBegin.ToString("yyyy year MM month dd day HH:mm") + '"' + ',');//{[starttime: Chinese + English + number + symbol, total length 50]}
                        paramss.Append('"' + bdateEnd.ToString("yyyy year MM month dd day HH:mm") + '"' + ',');
                        paramss.Append('"' + urldetail + '"' + ']');
                        bool b = xfService.sendsms(phoneList[i], paramss, "Here is the template ID 4aae25ed5ad370f58164");
                        if (b) {//Record sending SMS
                            BaseEntity_Sms insertObj = new BaseEntity_Sms();
                            insertObj.PhoneNum = phoneList[i];
                            insertObj.CreateDate = dNow;
                            insertObj.SmsType = "visitor";
                            ccms_business.insertSms(insertObj);
                        }
                    }
 /// <summary>
        ///SMS interface
        /// </summary>
        public bool sendsms(String mobiles, StringBuilder paramss, String templateId) {

            bool bRet = false;
            JObject jsonObj = new JObject();
            jsonObj.Add("ecName", "The Company filling in the SMS platform: Nantong's most powerful Co., Ltd");
            jsonObj.Add("apId", "attend");
            jsonObj.Add("templateId", templateId);
            jsonObj.Add("mobiles", mobiles);
            jsonObj.Add("params", paramss.ToString());
            jsonObj.Add("sign", "Here is the SMS interface signature pwa4T");
            jsonObj.Add("addSerial", "");

            StringBuilder sbFormatString = new StringBuilder();
            sbFormatString.Append("{0}{1}{2}{3}{4}{5}{6}");

            jsonObj.Add("mac", MD5Str.MD5Encrypt(String.Format(sbFormatString.ToString(), "The Company filling in the SMS platform: Nantong's most powerful Co., Ltd", "attend", "Password of SMS interface!@#$",
                templateId, mobiles, paramss.ToString(), "Here is the SMS interface signature pwa4T", ""), new UTF8Encoding()));
//Pay attention to base64 encryption here, otherwise an error will be reported
            string _data = Base64Encode(Encoding.UTF8, jsonObj.ToString());
            JObject rejsonObj = null;
            try {
                String sResult = HttpWebRequestHelper.GetDatahttps(_data, "https://Ask the customer service MM for the address / sms/tmpsubmit ");
                rejsonObj = JObject.Parse(sResult);
                Console.WriteLine(rejsonObj.ToString());
                if ("success".Equals(rejsonObj.GetValue("rspcod").ToString())) {
                    bRet = true;
                }
            }
            catch (Exception ex) {
                throw new Exception("XFRemoteService Interface call:ERROR => " + ex.Message);
            }
            return bRet;
        }
//The blogger's name is a little funny. Don't mind
public static string GetDatahttps(string JSONData, string Url) {
            byte[] bytes = Encoding.UTF8.GetBytes(JSONData);

            HttpWebRequest request = null;
            //HTTPSQ request
            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
            request = WebRequest.Create(Url) as HttpWebRequest;
            request.ProtocolVersion = HttpVersion.Version11;
            request.Method = "POST";
            request.ContentType = "application/json";
            request.UserAgent = DefaultUserAgent;
            //request.
            Stream reqstream = request.GetRequestStream();
            reqstream.Write(bytes, 0, bytes.Length);

            //Declare an HttpWebRequest request
            request.Timeout = 90000;
            //Set connection timeout
            request.Headers.Set("Pragma", "no-cache");
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream streamReceive = response.GetResponseStream();
            Encoding encoding = Encoding.UTF8;

            StreamReader streamReader = new StreamReader(streamReceive, encoding);
            string strResult = streamReader.ReadToEnd();
            streamReceive.Dispose();
            streamReader.Dispose();

            return strResult;

        }

Then there is no problem. After the test is completed, you will encounter the problem of success but unable to receive SMS. Generally, there is a problem with your own parameters. You can contact the customer service MM to check the reason for the error, but the SMS fee is still charged..... Worse than me.