标签:line 种类 return url sse hex 腾讯 md5 默认
| using System; |
| using System.Collections.Generic; |
| using System.Linq; |
| using System.Text; |
| using System.Threading.Tasks; |
| using System.Security.Cryptography; |
| using System.Net; |
| using System.IO; |
| // newtonsoft json 模块请自行到 http://www.newtonsoft.com/json 下载 |
| using Newtonsoft.Json.Linq; |
| using Newtonsoft.Json; |
| namespace SMSDemo |
| { |
| class SmsSender |
| { |
| int sdkappid; |
| string appkey; |
| string url = "https://yun.tim.qq.com/v3/tlssmssvr/sendsms"; |
| public SmsSender(int sdkappid, string appkey) |
| { |
| this.sdkappid = sdkappid; |
| this.appkey = appkey; |
| } |
| public void sendMsg(string nationCode, string phoneNumber, string content) |
| { |
| JObject data = new JObject(); |
| JObject tel = new JObject(); |
| tel.Add("nationcode", nationCode); |
| tel.Add("phone", phoneNumber); |
| data.Add("msg", content); |
| string sig = stringMD5(appkey + phoneNumber); |
| data.Add("type", "0"); // 默认为单发 |
| data.Add("sig", sig); |
| data.Add("tel", tel); |
| data.Add("extend", ""); // 根据需要添加,一般保持默认 |
| data.Add("ext", ""); // 根据需要添加,一般保持默认 |
| string msgString = JsonConvert.SerializeObject(data); |
| Console.WriteLine(msgString); |
| try |
| { |
| // 发送 POST 请求 |
| Random rnd = new Random(); |
| int random = rnd.Next(1000000) % (900000) + 1000000; |
| string wholeUrl = url + "?sdkappid=" + sdkappid + "&random=" + random; |
| HttpWebRequest request = (HttpWebRequest)WebRequest.Create(wholeUrl); |
| request.Method = "POST"; |
| request.ContentType = "application/x-www-form-urlencoded"; |
| byte[] requestData = Encoding.UTF8.GetBytes(msgString); |
| request.ContentLength = requestData.Length; |
| Stream requestStream = request.GetRequestStream(); |
| requestStream.Write(requestData, 0, requestData.Length); |
| requestStream.Close(); |
| // 接收返回包 |
| HttpWebResponse response = (HttpWebResponse)request.GetResponse(); |
| Stream responseStream = response.GetResponseStream(); |
| StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")); |
| string retString = streamReader.ReadToEnd(); |
| streamReader.Close(); |
| responseStream.Close(); |
| Console.WriteLine(retString); |
| Console.ReadLine(); |
| } |
| catch (Exception e) |
| { |
| Console.WriteLine(e.ToString()); |
| } |
| } |
| private static string stringMD5(string input) |
| { |
| MD5 md5 = new MD5CryptoServiceProvider(); |
| byte[] targetData = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(input)); |
| return byteToHexStr(targetData); |
| } |
| // 将二进制的数值转换为 16 进制字符串,如 "abc" => "616263" |
| private static string byteToHexStr(byte[] input) |
| { |
| string returnStr = ""; |
| if (input != null) |
| { |
| for (int i = 0; i < input.Length; i++) |
| { |
| returnStr += input[i].ToString("x2"); |
| } |
| } |
| return returnStr; |
| } |
| static void Main(string[] args) |
| { |
| // sdkappid 和 appkey 请根据开放者申请的填写,下面的是无效的 |
| SmsSender sender = new SmsSender(1234567890, "1234567890"); |
| // 发送手机号以及发送内容请根据实际情况调整,特别是内容需要匹配模板 |
| sender.sendMsg("86", "13012345678", "验证码 1234"); |
| } |
| } |
}
1 协议说明
协议 HTTP POST
编码格式 UTF8
URL 举例:https://yun.tim.qq.com/v3/tlssmssvr/sendsms?sdkappid=xxxxx&random=xxxx
接口说明 给用户发短信验证码、短信通知,营销短信(内容长度不超过450字)。
注:sdkappid请填写您在腾讯云上申请到的,random请填成随机数。
2 请求包体
包体为json字符串,参数如下:
{
"tel": {
"nationcode": "86", //国家码
"phone": "13788888888" //手机号码
},
"type": "0", //0:普通短信;1:营销短信(强调:要按需填值,不然会影响到业务的正常使用)
"msg": "你的验证码是1234", //验证码,utf8编码
"sig": "fdba654e05bc0d15796713a1a1a2318c", //app凭证,具体计算方式见下注
"extend": "", //可选字段,默认没有开通(需要填空)。通道扩展码,
//在短信回复场景中,腾讯server会原样返回,开发者可依此区分是哪种类型的回复
"ext": "" //可选字段,不需要就填空。用户的session内容,腾讯server回包中会原样返回
}
注:
1、"msg"字段需要匹配审核通过的模板内容
如果您的模板是"你的验证码是{1}",则"msg"字段可赋值为:"你的验证码是xxxx"。(其中"xxxx"为下发的验证码)
如果您有多个短信签名,请将需要的短信签名放在短信内容前面
例如您有"【腾讯科技】","【腾讯云】"两个签名,但是想以"【腾讯云】"签名发送短信,
则"msg"字段可赋值为:"【腾讯云】你的验证码是xxxx"。(其中"xxxx"为下发的验证码)
如果是海外手机,则不要携带短信签名。
2、"extend"字段的配置请联系腾讯云短信技术支持
3、"sig"字段 为appkey + phone字段的 md5值,c++ 示例代码如下:
string strPhone = "13712345678";
string strAppkey = "dffdfd6029698a5fdf4"; //sdkappid对应的appkey,需要业务方高度保密
string sig = md5(strAppkey + strPhone); //将strAppkey和 strPhone拼接后再求md5
3 应答包体
{
"result": "0", //0表示成功(计费依据),非0表示失败
"errmsg": "", //result非0时的具体错误信息
"ext": "some msg", //用户的session内容,腾讯server回包中会原样返回
"sid": "xxxxxxx", //标识本次发送id
"fee": 1 //短信计费的条数
}
标签:line 种类 return url sse hex 腾讯 md5 默认
原文地址:http://www.cnblogs.com/MJK-ONG/p/6203748.html