Ejemplo n.º 1
0
        // 将群发回包解析成结果对象
        public SmsMultiSenderResult ResponseStrToMultiSenderResult(string str)
        {
            SmsMultiSenderResult result = JsonConvert.DeserializeObject <SmsMultiSenderResult>(str);

            return(result);
        }
Ejemplo n.º 2
0
        /**
         * 指定模板群发
         * 【注意】海外短信无群发功能
         * @param nationCode 国家码,如 86 为中国
         * @param phoneNumbers 不带国家码的手机号列表
         * @param templId 模板 id
         * @param params 模板参数列表
         * @param sign 签名,如果填空,系统会使用默认签名
         * @param extend 扩展码,可以填空
         * @param ext 服务端原样返回的参数,可以填空
         * @return SmsMultiSenderResult
         */
        /// <summary>
        /// Sends the with parameter.指定模板群发
        /// </summary>
        /// <param name="nationCode">The nation code.国家码,如 86 为中国</param>
        /// <param name="phoneNumbers">The phone numbers.不带国家码的手机号列表</param>
        /// <param name="templId">The templ identifier.模板 id</param>
        /// <param name="templParams">The templ parameters.模板参数列表</param>
        /// <param name="sign">The sign.签名,如果填空,系统会使用默认签名</param>
        /// <param name="extend">The extend.扩展码,可以填空</param>
        /// <param name="ext">The ext.服务端原样返回的参数,可以填空</param>
        /// <returns>指定模板群发</returns>
        public SmsMultiSenderResult SendWithParam(
            String nationCode,
            List <string> phoneNumbers,
            int templId,
            List <string> templParams,
            string sign,
            string extend,
            string ext)
        {
            /*
             * 请求包体
             * {
             *  "tel": [
             *      {
             *          "nationcode": "86",
             *          "mobile": "13788888888"
             *      },
             *      {
             *          "nationcode": "86",
             *          "mobile": "13788888889"
             *      }
             *  ],
             *  "type": 0,
             *  "msg": "你的验证码是1234",
             *  "sig": "fdba654e05bc0d15796713a1a1a2318c",
             *  "time": 1479888540,
             *  "extend": "",
             *  "ext": ""
             * }
             * 应答包体
             * {
             *  "result": 0,
             *  "errmsg": "OK",
             *  "ext": "",
             *  "detail": [
             *      {
             *          "result": 0,
             *          "errmsg": "OK",
             *          "mobile": "13788888888",
             *          "nationcode": "86",
             *          "sid": "xxxxxxx",
             *          "fee": 1
             *      },
             *      {
             *          "result": 0,
             *          "errmsg": "OK",
             *          "mobile": "13788888889",
             *          "nationcode": "86",
             *          "sid": "xxxxxxx",
             *          "fee": 1
             *      }
             *  ]
             * }
             */
            if (null == sign)
            {
                sign = "";
            }
            if (null == extend)
            {
                extend = "";
            }
            if (null == ext)
            {
                ext = "";
            }

            long random  = util.GetRandom();
            long curTime = util.GetCurTime();

            // 按照协议组织 post 请求包体
            JObject data = new JObject();

            data.Add("tel", util.PhoneNumbersToJSONArray(nationCode, phoneNumbers));
            data.Add("sig", util.CalculateSigForTempl(appkey, random, curTime, phoneNumbers));
            data.Add("tpl_id", templId);
            data.Add("params", util.SmsParamsToJSONArray(templParams));
            data.Add("sign", sign);
            data.Add("time", curTime);
            data.Add("extend", extend);
            data.Add("ext", ext);

            string         wholeUrl = url + "?sdkappid=" + sdkappid + "&random=" + random;
            HttpWebRequest request  = util.GetPostHttpConn(wholeUrl);

            byte[] requestData = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data));
            request.ContentLength = requestData.Length;
            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(requestData, 0, requestData.Length);
                requestStream.Close();
            }

            // 接收返回包
            HttpWebResponse response    = (HttpWebResponse)request.GetResponse();
            string          responseStr = null;

            using (Stream responseStream = response.GetResponseStream())
            {
                using (StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")))
                {
                    responseStr = streamReader.ReadToEnd();
                    streamReader.Close();
                }
                responseStream.Close();
            }
            SmsMultiSenderResult result;

            if (HttpStatusCode.OK == response.StatusCode)
            {
                result = util.ResponseStrToMultiSenderResult(responseStr);
            }
            else
            {
                result        = new SmsMultiSenderResult();
                result.result = -1;
                result.errmsg = "http error " + response.StatusCode + " " + responseStr;
            }
            return(result);
        }