博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Asp.Net发送手机验证码
阅读量:4964 次
发布时间:2019-06-12

本文共 53971 字,大约阅读时间需要 179 分钟。

C#发送手机验证码,平台有很多,我就说说其中的1个平台

测试环境:.net2.0

测试效果:速度还可以,10秒内接收短信

1、去http://www.yuntongxun.com注册,会送8元测试金额;正式使用必须充值500元以上才行

2、部署代码

另一个平台感觉也不错,注册送100条短信,认证再送500条短信http://www.etuocloud.com

核心测试代码,红色为你需要改下的参数

using System;using System.Collections.Generic;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.IO;using System.Drawing;using System.Drawing.Drawing2D;   using System.Drawing.Imaging;    using System.Reflection;   public partial class ceshi : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {        string ret = null;        CCPRestSDK.CCPRestSDK api = new CCPRestSDK.CCPRestSDK();        //ip格式如下,不带https://        bool isInit = api.init("app.cloopen.com", "8883");//sandboxapp.cloopen.com        api.setAccount("816da7878745458787545878", "7bbd1891jkdjfjaksldfjk054bd54f6");//主账号,主账号令牌        api.setAppId("jkdsjfkdjfkdsfjlsjueorun");//AppID        try        {            if (isInit)            {                string[] data = { "1234", "30" };//验证码,时间                Dictionary
retData = api.SendTemplateSMS("18000000456", "1", data);//短信接收号码, 短信模板id, 内容数据 ret = getDictionaryData(retData); string statusCode = retData["statusCode"].ToString(); string statusMsg = retData["statusMsg"].ToString(); Response.Write(statusCode + "==" + statusMsg + "
"); } else { ret = "初始化失败"; } } catch (Exception exc) { ret = exc.Message; } finally { Response.Write(ret); } } private string getDictionaryData(Dictionary
data) { string ret = null; foreach (KeyValuePair
item in data) { if (item.Value != null && item.Value.GetType() == typeof(Dictionary
)) { ret += item.Key.ToString() + "={
"; ret += getDictionaryData((Dictionary
)item.Value); ret += "};"; } else { ret += item.Key.ToString() + "=" + (item.Value == null ? "null" : item.Value.ToString()) + ";"; } } return ret; }}

 

CCPRestSDK类,经改良支持.net2.0

/* *  Copyright (c) 2014 The CCP project authors. All Rights Reserved. * *  Use of this source code is governed by a Beijing Speedtong Information Technology Co.,Ltd license *  that can be found in the LICENSE file in the root of the web site. * *   http://www.114390.com * *  An additional intellectual property rights grant can be found *  in the file PATENTS.  All contributing project authors may *  be found in the AUTHORS file in the root of the source tree. */using System;using System.Collections.Generic;using System.IO;using System.Net;using System.Reflection;using System.Text;using System.Xml;namespace CCPRestSDK{    enum EBodyType:uint    {        EType_XML = 0,        EType_JSON    };    public class CCPRestSDK    {        private string m_restAddress = null;        private string m_restPort = null;        private string m_mainAccount = null;        private string m_mainToken = null;        private string m_subAccount = null;        private string m_subToken = null;        private string m_voipAccount = null;        private string m_voipPwd = null;        private string m_appId = null;        private bool m_isWriteLog = false;        private EBodyType m_bodyType = EBodyType.EType_XML;        ///         /// 服务器api版本        ///         const string softVer = "2013-12-26";        ///         /// 初始化函数        ///         /// 服务器地址        /// 服务器端口        /// 
public bool init(string restAddress, string restPort) { this.m_restAddress = restAddress; this.m_restPort = restPort; if (m_restAddress == null || m_restAddress.Length < 0 || m_restPort == null || m_restPort.Length < 0 || Convert.ToInt32(m_restPort) < 0) return false; return true; } /// /// 设置主帐号信息 /// /// 主帐号 /// 主帐号令牌 public void setAccount(string accountSid, string accountToken) { this.m_mainAccount = accountSid; this.m_mainToken = accountToken; } /// /// 设置子帐号信息 /// /// 子帐号 /// 子帐号令牌 /// VoIP帐号 /// VoIP密码 public void setSubAccount(string subAccountSid, string subAccountToken, string voipAccount, string voipPassword) { this.m_subAccount = subAccountSid; this.m_subToken = subAccountToken; this.m_voipAccount = voipAccount; this.m_voipPwd = voipPassword; } /// /// 设置应用ID /// /// 应用ID public void setAppId(string appId) { this.m_appId = appId; } /// /// 日志开关 /// /// 日志开关 public void enabeLog(bool enable) { this.m_isWriteLog = enable; } /// /// 获取日志路径 /// ///
日志路径
public string GetLogPath() { string dllpath = System.Reflection.Assembly.GetExecutingAssembly().CodeBase; dllpath = dllpath.Substring(8, dllpath.Length - 8); // 8是 file:// 的长度 return System.IO.Path.GetDirectoryName(dllpath)+"\\log.txt"; } /// /// 主帐号信息查询 /// ///
///
包体内容
public Dictionary
QueryAccountInfo() { Dictionary
initError = paramCheckRest(); if (initError != null) { return initError; } initError = paramCheckMainAccount(); if (initError != null) { return initError; } try { string date = DateTime.Now.ToString("yyyyMMddhhmmss"); // 构建URL内容 string sigstr = MD5Encrypt(m_mainAccount + m_mainToken + date); string uriStr = string.Format("https://{0}:{1}/{2}/Accounts/{3}/AccountInfo?sig={4}", m_restAddress, m_restPort, softVer, m_mainAccount, sigstr); Uri address = new Uri(uriStr); WriteLog("QueryAccountInfo url = "+uriStr); // 创建网络请求 HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest; setCertificateValidationCallBack(); // 构建Head request.Method = "GET"; Encoding myEncoding = Encoding.GetEncoding("utf-8"); byte[] myByte = myEncoding.GetBytes(m_mainAccount + ":" + date); string authStr = Convert.ToBase64String(myByte); request.Headers.Add("Authorization", authStr); if (m_bodyType == EBodyType.EType_XML) { request.Accept = "application/xml"; request.ContentType = "application/xml;charset=utf-8"; using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { // Get the response stream StreamReader reader = new StreamReader(response.GetResponseStream()); string responseStr = reader.ReadToEnd(); WriteLog("QueryAccountInfo responseBody = " + responseStr); if (responseStr != null && responseStr.Length > 0) { //Dictionary
responseResult = new Dictionary
{ { "statusCode", "0" }, { "statusMsg", "成功" }, { "data", null } }; Dictionary
responseResult = new Dictionary
(); responseResult.Add("statusCode", "0"); responseResult.Add("statusMsg", "成功"); responseResult.Add("data", null); XmlDocument resultXml = new XmlDocument(); resultXml.LoadXml(responseStr); XmlNodeList nodeList = resultXml.SelectSingleNode("Response").ChildNodes; foreach (XmlNode item in nodeList) { if (item.Name == "statusCode") { responseResult["statusCode"] = item.InnerText; } else if (item.Name == "statusMsg") { responseResult["statusMsg"] = item.InnerText; } else if (item.Name == "Account") { Dictionary
data = new Dictionary
(); foreach (XmlNode subItem in item.ChildNodes) { data.Add(subItem.Name, subItem.InnerText); } //responseResult["data"] = new Dictionary
{ {item.Name,data}}; Dictionary
data1 = new Dictionary
(); data1.Add(item.Name, data); responseResult["data"] = data1; } } return responseResult; } //return new Dictionary
{ { "statusCode", 172002 }, { "statusMsg", "无返回" }, { "data", null } }; Dictionary
data2 = new Dictionary
(); data2.Add("statusCode", 172002); data2.Add("statusMsg", "无返回"); data2.Add("data", null); return data2; } } else { request.Accept = "application/json"; request.ContentType = "application/json;charset=utf-8"; using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { StreamReader reader = new StreamReader(response.GetResponseStream()); string responseStr = reader.ReadToEnd(); WriteLog("QueryAccountInfo responseBody = " + responseStr); if (responseStr != null && responseStr.Length > 0) { Dictionary
responseResult = new Dictionary
(); responseResult["resposeBody"] = responseStr; return responseResult; } //return new Dictionary
{ { "statusCode", 172002 }, { "statusMsg", "无返回" }, { "data", null } }; Dictionary
data2 = new Dictionary
(); data2.Add("statusCode", 172002); data2.Add("statusMsg", "无返回"); data2.Add("data", null); return data2; } } // 获取请求 } catch (Exception e) { throw e; } } ///
/// 创建子帐号 /// ///
子帐号名称。可由英文字母和阿拉伯数字组成子帐号唯一名称,推荐使用电子邮箱地址 ///
参数不能为空
///
///
包体内容
public Dictionary
CreateSubAccount(string friendlyName) { Dictionary
initError = paramCheckRest(); if (initError != null) { return initError; } initError = paramCheckMainAccount(); if (initError != null) { return initError; } initError = paramCheckAppId(); if (initError != null) { return initError; } if(friendlyName == null) throw new ArgumentNullException("friendlyName"); try { string date = DateTime.Now.ToString("yyyyMMddhhmmss"); // 构建URL内容 string sigstr = MD5Encrypt(m_mainAccount + m_mainToken + date); string uriStr = string.Format("https://{0}:{1}/{2}/Accounts/{3}/SubAccounts?sig={4}", m_restAddress, m_restPort, softVer, m_mainAccount, sigstr); Uri address = new Uri(uriStr); WriteLog("CreateSubAccount url = " +uriStr); // 创建网络请求 HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest; setCertificateValidationCallBack(); // 构建Head request.Method = "POST"; Encoding myEncoding = Encoding.GetEncoding("utf-8"); byte[] myByte = myEncoding.GetBytes(m_mainAccount + ":" + date); string authStr = Convert.ToBase64String(myByte); request.Headers.Add("Authorization", authStr); // 构建Body StringBuilder data = new StringBuilder(); if (m_bodyType == EBodyType.EType_XML) { request.Accept = "application/xml"; request.ContentType = "application/xml;charset=utf-8"; data.Append("
"); data.Append("
").Append(m_appId).Append("
"); data.Append("
").Append(friendlyName).Append("
"); data.Append("
"); } else { request.Accept = "application/json"; request.ContentType = "application/json;charset=utf-8"; data.Append("{ "); data.Append("\"appId\":\"").Append(m_appId).Append("\""); data.Append(",\"friendlyName\":\"").Append(friendlyName).Append("\""); data.Append("}"); } byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString()); WriteLog("CreateSubAccount requestBody = " +data.ToString()); // 开始请求 using (Stream postStream = request.GetRequestStream()) { postStream.Write(byteData, 0, byteData.Length); } // 获取请求 using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { // Get the response stream StreamReader reader = new StreamReader(response.GetResponseStream()); string responseStr = reader.ReadToEnd(); WriteLog("CreateSubAccount responseBody = " + responseStr); if (responseStr != null && responseStr.Length > 0) { //Dictionary
responseResult = new Dictionary
{ { "statusCode", "0" }, { "statusMsg", "成功" }, { "data", null } }; Dictionary
responseResult = new Dictionary
(); responseResult.Add("statusCode", "0"); responseResult.Add("statusMsg", "成功"); responseResult.Add("data", null); if (m_bodyType == EBodyType.EType_XML) { XmlDocument resultXml = new XmlDocument(); resultXml.LoadXml(responseStr); XmlNodeList nodeList = resultXml.SelectSingleNode("Response").ChildNodes; foreach (XmlNode item in nodeList) { if (item.Name == "statusCode") { responseResult["statusCode"] = item.InnerText; } else if (item.Name == "statusMsg") { responseResult["statusMsg"] = item.InnerText; } else if (item.Name == "SubAccount") { Dictionary
retData = new Dictionary
(); foreach (XmlNode subItem in item.ChildNodes) { retData.Add(subItem.Name, subItem.InnerText); } //responseResult["data"] = new Dictionary
{ { item.Name, retData } }; Dictionary
data1 = new Dictionary
(); data1.Add(item.Name, retData); responseResult["data"] = data1; } } } else { responseResult.Clear(); responseResult["resposeBody"] = responseStr; } return responseResult; } //return new Dictionary
{ { "statusCode", 172002 }, { "statusMsg", "无返回" }, { "data", null } }; Dictionary
data2 = new Dictionary
(); data2.Add("statusCode", 172002); data2.Add("statusMsg", "无返回"); data2.Add("data", null); return data2; } } catch (Exception e) { throw e; } } ///
/// 获取应用下子帐号 /// ///
开始的序号,默认从0开始 ///
一次查询的最大条数,最小是1条,最大是100条 ///
参数超出范围
///
///
public Dictionary
GetSubAccounts(uint startNo, uint offset) { Dictionary
initError = paramCheckRest(); if (initError != null) { return initError; } initError = paramCheckMainAccount(); if (initError != null) { return initError; } initError = paramCheckAppId(); if (initError != null) { return initError; } if (offset < 1 || offset > 100) { throw new ArgumentOutOfRangeException("offset超出范围"); } try { string date = DateTime.Now.ToString("yyyyMMddhhmmss"); // 构建URL内容 string sigstr = MD5Encrypt(m_mainAccount + m_mainToken + date); string uriStr = string.Format("https://{0}:{1}/{2}/Accounts/{3}/GetSubAccounts?sig={4}", m_restAddress, m_restPort, softVer, m_mainAccount, sigstr); Uri address = new Uri(uriStr); WriteLog("GetSubAccounts url = "+uriStr); // 创建网络请求 HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest; setCertificateValidationCallBack(); // 构建Head request.Method = "POST"; Encoding myEncoding = Encoding.GetEncoding("utf-8"); byte[] myByte = myEncoding.GetBytes(m_mainAccount + ":" + date); string authStr = Convert.ToBase64String(myByte); request.Headers.Add("Authorization", authStr); // 构建Body StringBuilder data = new StringBuilder(); if (m_bodyType == EBodyType.EType_XML) { request.Accept = "application/xml"; request.ContentType = "application/xml;charset=utf-8"; data.Append("
"); data.Append("
").Append(m_appId).Append("
"); data.Append("
").Append(startNo).Append("
"); data.Append("
").Append(offset).Append("
"); data.Append("
"); } else { request.Accept = "application/json"; request.ContentType = "application/json;charset=utf-8"; data.Append("{ "); data.Append("\"appId\":\"").Append(m_appId).Append("\""); data.Append(",\"startNo\":\"").Append(startNo).Append("\""); data.Append(",\"offset\":\"").Append(offset).Append("\""); data.Append("}"); } byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString()); WriteLog("GetSubAccounts requestBody = " + data.ToString()); // 开始请求 using (Stream postStream = request.GetRequestStream()) { postStream.Write(byteData, 0, byteData.Length); } // 获取请求 using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { // Get the response stream StreamReader reader = new StreamReader(response.GetResponseStream()); string responseStr = reader.ReadToEnd(); WriteLog("GetSubAccounts responseBody = "+responseStr); if (responseStr != null && responseStr.Length > 0) { //Dictionary
responseResult = new Dictionary
{ { "statusCode", "0" }, { "statusMsg", "成功" }, { "data", null } }; Dictionary
responseResult = new Dictionary
(); responseResult.Add("statusCode", "0"); responseResult.Add("statusMsg", "成功"); responseResult.Add("data", null); Dictionary
retData = new Dictionary
(); List
subAccountList = new List(); if (m_bodyType == EBodyType.EType_XML) { XmlDocument resultXml = new XmlDocument(); resultXml.LoadXml(responseStr); XmlNodeList nodeList = resultXml.SelectSingleNode("Response").ChildNodes; foreach (XmlNode item in nodeList) { if (item.Name == "statusCode") { responseResult["statusCode"] = item.InnerText; } else if (item.Name == "statusMsg") { responseResult["statusMsg"] = item.InnerText; } else if (item.Name == "totalCount") { retData.Add(item.Name, item.InnerText); } else if (item.Name == "SubAccount") { Dictionary
SubAccount = new Dictionary
(); foreach (XmlNode subItem in item.ChildNodes) { SubAccount.Add(subItem.Name, subItem.InnerText); } subAccountList.Add(SubAccount); } } } else { responseResult.Clear(); responseResult["resposeBody"] = responseStr; } if (retData.Count > 0) { if (subAccountList.Count > 0) { retData.Add("SubAccount", subAccountList); } responseResult["data"] = retData; } return responseResult; } //return new Dictionary
{ { "statusCode", 172002 }, { "statusMsg", "无返回" }, { "data", null } }; Dictionary
data2 = new Dictionary
(); data2.Add("statusCode", 172002); data2.Add("statusMsg", "无返回"); data2.Add("data", null); return data2; } } catch (Exception e) { throw e; } } ///
/// 查询子帐号信息 /// ///
子帐号名称 ///
参数不能为空
///
///
包体内容
public Dictionary
QuerySubAccount(string friendlyName) { Dictionary
initError = paramCheckRest(); if (initError != null) { return initError; } initError = paramCheckMainAccount(); if (initError != null) { return initError; } initError = paramCheckAppId(); if (initError != null) { return initError; } if (friendlyName == null) throw new ArgumentNullException("friendlyName"); try { string date = DateTime.Now.ToString("yyyyMMddhhmmss"); // 构建URL内容 string sigstr = MD5Encrypt(m_mainAccount + m_mainToken + date); string uriStr = string.Format("https://{0}:{1}/{2}/Accounts/{3}/QuerySubAccountByName?sig={4}", m_restAddress, m_restPort, softVer, m_mainAccount, sigstr); Uri address = new Uri(uriStr); WriteLog("QuerySubAccount url = " + uriStr); // 创建网络请求 HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest; setCertificateValidationCallBack(); // 构建Head request.Method = "POST"; Encoding myEncoding = Encoding.GetEncoding("utf-8"); byte[] myByte = myEncoding.GetBytes(m_mainAccount + ":" + date); string authStr = Convert.ToBase64String(myByte); request.Headers.Add("Authorization", authStr); // 构建Body StringBuilder data = new StringBuilder(); if (m_bodyType == EBodyType.EType_XML) { request.Accept = "application/xml"; request.ContentType = "application/xml;charset=utf-8"; data.Append("
"); data.Append("
").Append(m_appId).Append("
"); data.Append("
").Append(friendlyName).Append("
"); data.Append("
"); } else { request.Accept = "application/json"; request.ContentType = "application/json;charset=utf-8"; data.Append("{ "); data.Append("\"appId\":\"").Append(m_appId).Append("\""); data.Append(",\"friendlyName\":\"").Append(friendlyName).Append("\""); data.Append("}"); } byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString()); WriteLog("QuerySubAccount requestBody = "+data.ToString()); // 开始请求 using (Stream postStream = request.GetRequestStream()) { postStream.Write(byteData, 0, byteData.Length); } // 获取请求 using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { // Get the response stream StreamReader reader = new StreamReader(response.GetResponseStream()); string responseStr = reader.ReadToEnd(); WriteLog("QuerySubAccount responseBody = " + responseStr); if (responseStr != null && responseStr.Length > 0) { //Dictionary
responseResult = new Dictionary
{ { "statusCode", "0" }, { "statusMsg", "成功" }, { "data", null } }; Dictionary
responseResult = new Dictionary
(); responseResult.Add("statusCode", "0"); responseResult.Add("statusMsg", "成功"); responseResult.Add("data", null); if (m_bodyType == EBodyType.EType_XML) { XmlDocument resultXml = new XmlDocument(); resultXml.LoadXml(responseStr); XmlNodeList nodeList = resultXml.SelectSingleNode("Response").ChildNodes; foreach (XmlNode item in nodeList) { if (item.Name == "statusCode") { responseResult["statusCode"] = item.InnerText; } else if (item.Name == "statusMsg") { responseResult["statusMsg"] = item.InnerText; } else if (item.Name == "SubAccount") { Dictionary
retData = new Dictionary
(); foreach (XmlNode subItem in item.ChildNodes) { retData.Add(subItem.Name, subItem.InnerText); } //responseResult["data"] = new Dictionary
{ { item.Name, retData } }; Dictionary
data1 = new Dictionary
(); data1.Add(item.Name, retData); responseResult["data"] = data1; } } } else { responseResult.Clear(); responseResult["resposeBody"] = responseStr; } return responseResult; } //return new Dictionary
{ { "statusCode", 172002 }, { "statusMsg", "无返回" }, { "data", null } }; Dictionary
data2 = new Dictionary
(); data2.Add("statusCode", 172002); data2.Add("statusMsg", "无返回"); data2.Add("data", null); return data2; } } catch (Exception e) { throw e; } } ///
/// 发送短信 /// ///
短信接收端手机号码集合,用英文逗号分开,每批发送的手机号数量不得超过100个 ///
短信内容 ///
参数不能为空
///
参数超出范围
///
///
包体内容
public Dictionary
SendSMS(string to, string body) { Dictionary
initError = paramCheckRest(); if (initError != null) { return initError; } initError = paramCheckMainAccount(); if (initError != null) { return initError; } initError = paramCheckAppId(); if (initError != null) { return initError; } if (to == null) { throw new ArgumentNullException("to"); } if (body == null) { throw new ArgumentNullException("body"); } try { string date = DateTime.Now.ToString("yyyyMMddhhmmss"); // 构建URL内容 string sigstr = MD5Encrypt(m_mainAccount + m_mainToken + date); string uriStr = string.Format("https://{0}:{1}/{2}/Accounts/{3}/SMS/Messages?sig={4}", m_restAddress, m_restPort, softVer, m_mainAccount, sigstr); Uri address = new Uri(uriStr); WriteLog("SendSMS url = " + uriStr); // 创建网络请求 HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest; setCertificateValidationCallBack(); // 构建Head request.Method = "POST"; Encoding myEncoding = Encoding.GetEncoding("utf-8"); byte[] myByte = myEncoding.GetBytes(m_mainAccount + ":" + date); string authStr = Convert.ToBase64String(myByte); request.Headers.Add("Authorization", authStr); // 构建Body StringBuilder data = new StringBuilder(); if (m_bodyType == EBodyType.EType_XML) { request.Accept = "application/xml"; request.ContentType = "application/xml;charset=utf-8"; data.Append("
"); data.Append("
").Append(to).Append("
"); data.Append("").Append(body).Append(""); data.Append("
").Append(m_appId).Append("
"); data.Append("
"); } else { request.Accept = "application/json"; request.ContentType = "application/json;charset=utf-8"; data.Append("{ "); data.Append("\"to\":\"").Append(to).Append("\""); data.Append(",\"body\":\"").Append(body).Append("\""); data.Append(",\"appId\":\"").Append(m_appId).Append("\""); data.Append("}"); } byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString()); WriteLog("SendSMS requestBody = "+data.ToString()); // 开始请求 using (Stream postStream = request.GetRequestStream()) { postStream.Write(byteData, 0, byteData.Length); } // 获取请求 using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { // Get the response stream StreamReader reader = new StreamReader(response.GetResponseStream()); string responseStr = reader.ReadToEnd(); WriteLog("SendSMS responseBody = "+responseStr); if (responseStr != null && responseStr.Length > 0) { //Dictionary
responseResult = new Dictionary
{ { "statusCode", "0" }, { "statusMsg", "成功" }, { "data", null } }; Dictionary
responseResult = new Dictionary
(); responseResult.Add("statusCode", "0"); responseResult.Add("statusMsg", "成功"); responseResult.Add("data", null); if (m_bodyType == EBodyType.EType_XML) { XmlDocument resultXml = new XmlDocument(); resultXml.LoadXml(responseStr); XmlNodeList nodeList = resultXml.SelectSingleNode("Response").ChildNodes; foreach (XmlNode item in nodeList) { if (item.Name == "statusCode") { responseResult["statusCode"] = item.InnerText; } else if (item.Name == "statusMsg") { responseResult["statusMsg"] = item.InnerText; } else if (item.Name == "SMSMessage") { Dictionary
retData = new Dictionary
(); foreach (XmlNode subItem in item.ChildNodes) { retData.Add(subItem.Name, subItem.InnerText); } //responseResult["data"] = new Dictionary
{ { item.Name, retData } }; Dictionary
data1 = new Dictionary
(); data1.Add(item.Name, retData); responseResult["data"] = data1; } } } else { responseResult.Clear(); responseResult["resposeBody"] = responseStr; } return responseResult; } //return new Dictionary
{ { "statusCode", 172002 }, { "statusMsg", "无返回" }, { "data", null } }; Dictionary
data2 = new Dictionary
(); data2.Add("statusCode", 172002); data2.Add("statusMsg", "无返回"); data2.Add("data", null); return data2; } } catch (Exception e) { throw e; } } ///
/// 模板短信 /// ///
短信接收端手机号码集合,用英文逗号分开,每批发送的手机号数量不得超过100个 ///
模板Id ///
可选字段 内容数据,用于替换模板中{序号} ///
参数不能为空
///
///
public Dictionary
SendTemplateSMS(string to, string templateId, string[] data) { Dictionary
initError = paramCheckRest(); if (initError != null) { return initError; } initError = paramCheckMainAccount(); if (initError != null) { return initError; } initError = paramCheckAppId(); if (initError != null) { return initError; } if (to == null) { throw new ArgumentNullException("to"); } if (templateId == null) { throw new ArgumentNullException("templateId"); } try { string date = DateTime.Now.ToString("yyyyMMddhhmmss"); // 构建URL内容 string sigstr = MD5Encrypt(m_mainAccount + m_mainToken + date); string uriStr = string.Format("https://{0}:{1}/{2}/Accounts/{3}/SMS/TemplateSMS?sig={4}", m_restAddress, m_restPort, softVer, m_mainAccount, sigstr); Uri address = new Uri(uriStr); WriteLog("SendTemplateSMS url = " + uriStr); // 创建网络请求 HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest; setCertificateValidationCallBack(); // 构建Head request.Method = "POST"; Encoding myEncoding = Encoding.GetEncoding("utf-8"); byte[] myByte = myEncoding.GetBytes(m_mainAccount + ":" + date); string authStr = Convert.ToBase64String(myByte); request.Headers.Add("Authorization", authStr); // 构建Body StringBuilder bodyData = new StringBuilder(); if (m_bodyType == EBodyType.EType_XML) { request.Accept = "application/xml"; request.ContentType = "application/xml;charset=utf-8"; bodyData.Append("
"); bodyData.Append("
").Append(to).Append("
"); bodyData.Append("
").Append(m_appId).Append("
"); bodyData.Append("
").Append(templateId).Append("
"); if (data != null && data.Length>0) { bodyData.Append("
"); foreach (string item in data) { bodyData.Append("
").Append(item).Append(""); } bodyData.Append("
"); } bodyData.Append("
"); } else { request.Accept = "application/json"; request.ContentType = "application/json;charset=utf-8"; bodyData.Append("{ "); bodyData.Append("\"to\":\"").Append(to).Append("\""); bodyData.Append(",\"appId\":\"").Append(m_appId).Append("\""); bodyData.Append(",\"templateId\":\"").Append(templateId).Append("\""); if (data != null && data.Length > 0) { bodyData.Append(",\"datas\":["); int index = 0; foreach (string item in data) { if (index == 0) { bodyData.Append("\""+item+"\""); } else { bodyData.Append(",\"" + item + "\""); } index++; } bodyData.Append("]"); } bodyData.Append("}"); } byte[] byteData = UTF8Encoding.UTF8.GetBytes(bodyData.ToString()); WriteLog("SendTemplateSMS requestBody = " + bodyData.ToString()); // 开始请求 using (Stream postStream = request.GetRequestStream()) { postStream.Write(byteData, 0, byteData.Length); } // 获取请求 using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { // Get the response stream StreamReader reader = new StreamReader(response.GetResponseStream()); string responseStr = reader.ReadToEnd(); WriteLog("SendTemplateSMS responseBody = " + responseStr); if (responseStr != null && responseStr.Length > 0) { //Dictionary
responseResult = new Dictionary
{ { "statusCode", "0" }, { "statusMsg", "成功" }, { "data", null } }; Dictionary
responseResult = new Dictionary
(); responseResult.Add("statusCode", "0"); responseResult.Add("statusMsg", "成功"); responseResult.Add("data", null); if (m_bodyType == EBodyType.EType_XML) { XmlDocument resultXml = new XmlDocument(); resultXml.LoadXml(responseStr); XmlNodeList nodeList = resultXml.SelectSingleNode("Response").ChildNodes; foreach (XmlNode item in nodeList) { if (item.Name == "statusCode") { responseResult["statusCode"] = item.InnerText; } else if (item.Name == "statusMsg") { responseResult["statusMsg"] = item.InnerText; } else if (item.Name == "TemplateSMS") { Dictionary
retData = new Dictionary
(); foreach (XmlNode subItem in item.ChildNodes) { retData.Add(subItem.Name, subItem.InnerText); } //responseResult["data"] = new Dictionary
{ { item.Name, retData } }; Dictionary
data1 = new Dictionary
(); data1.Add(item.Name, retData); responseResult["data"] = data1; } } } else { responseResult.Clear(); responseResult["resposeBody"] = responseStr; } return responseResult; } //return new Dictionary
{ { "statusCode", 172002 }, { "statusMsg", "无返回" }, { "data", null } }; Dictionary
data2 = new Dictionary
(); data2.Add("statusCode", 172002); data2.Add("statusMsg", "无返回"); data2.Add("data", null); return data2; } } catch (Exception e) { throw e; } } ///
/// 双向回呼 /// ///
主叫电话 ///
被叫电话 ///
可选字段 被叫侧显示的客服号码,根据平台侧显号规则控制 ///
可选字段 主叫侧显示的号码,根据平台侧显号规则控制 ///
可选字段 第三方自定义回拨提示音 ///
参数不能为空
///
///
包体内容
public Dictionary
CallBack(string from, string to, string customerSerNum, string fromSerNum, string promptTone) { Dictionary
initError = paramCheckRest(); if (initError != null) { return initError; } initError = paramCheckSunAccount(); if (initError != null) { return initError; } if (from == null) { throw new ArgumentNullException("from"); } if (to == null) { throw new ArgumentNullException("to"); } try { string date = DateTime.Now.ToString("yyyyMMddhhmmss"); // 构建URL内容 string sigstr = MD5Encrypt(m_subAccount + m_subToken + date); string uriStr = string.Format("https://{0}:{1}/{2}/SubAccounts/{3}/Calls/Callback?sig={4}", m_restAddress, m_restPort, softVer, m_subAccount, sigstr); Uri address = new Uri(uriStr); WriteLog("CallBack url = " + uriStr); // 创建网络请求 HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest; setCertificateValidationCallBack(); // 构建Head request.Method = "POST"; Encoding myEncoding = Encoding.GetEncoding("utf-8"); byte[] myByte = myEncoding.GetBytes(m_subAccount + ":" + date); string authStr = Convert.ToBase64String(myByte); request.Headers.Add("Authorization", authStr); // 构建Body StringBuilder data = new StringBuilder(); if (m_bodyType == EBodyType.EType_XML) { request.Accept = "application/xml"; request.ContentType = "application/xml;charset=utf-8"; data.Append("
"); data.Append("
").Append(from).Append("
"); data.Append("
").Append(to).Append("
"); if (customerSerNum != null) { data.Append("
").Append(customerSerNum).Append("
"); } if (fromSerNum != null) { data.Append("
").Append(fromSerNum).Append("
"); } if (promptTone != null) { data.Append("
").Append(promptTone).Append("
"); } data.Append("
"); } else { request.Accept = "application/json"; request.ContentType = "application/json;charset=utf-8"; data.Append("{ "); data.Append("\"from\":\"").Append(from).Append("\""); data.Append(",\"to\":\"").Append(to).Append("\""); if (customerSerNum != null) { data.Append(",\"customerSerNum\":\"").Append(customerSerNum).Append("\""); } if (fromSerNum != null) { data.Append(",\"fromSerNum\":\"").Append(fromSerNum).Append("\""); } if (promptTone != null) { data.Append(",\"promptTone\":\"").Append(promptTone).Append("\""); } data.Append("}"); } byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString()); WriteLog("CallBack requestBody = " + data.ToString()); // 开始请求 using (Stream postStream = request.GetRequestStream()) { postStream.Write(byteData, 0, byteData.Length); } // 获取请求 using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { // Get the response stream StreamReader reader = new StreamReader(response.GetResponseStream()); string responseStr = reader.ReadToEnd(); WriteLog("CallBack responseBody = " + responseStr); if (responseStr != null && responseStr.Length > 0) { //Dictionary
responseResult = new Dictionary
{ { "statusCode", "0" }, { "statusMsg", "成功" }, { "data", null } }; Dictionary
responseResult = new Dictionary
(); responseResult.Add("statusCode", "0"); responseResult.Add("statusMsg", "成功"); responseResult.Add("data", null); if (m_bodyType == EBodyType.EType_XML) { XmlDocument resultXml = new XmlDocument(); resultXml.LoadXml(responseStr); XmlNodeList nodeList = resultXml.SelectSingleNode("Response").ChildNodes; foreach (XmlNode item in nodeList) { if (item.Name == "statusCode") { responseResult["statusCode"] = item.InnerText; } else if (item.Name == "statusMsg") { responseResult["statusMsg"] = item.InnerText; } else if (item.Name == "CallBack") { Dictionary
retData = new Dictionary
(); foreach (XmlNode subItem in item.ChildNodes) { retData.Add(subItem.Name, subItem.InnerText); } //responseResult["data"] = new Dictionary
{ { item.Name, retData } }; Dictionary
data1 = new Dictionary
(); data1.Add(item.Name, retData); responseResult["data"] = data1; } } } else { responseResult.Clear(); responseResult["resposeBody"] = responseStr; } return responseResult; } //return new Dictionary
{ { "statusCode", 172002 }, { "statusMsg", "无返回" }, { "data", null } }; Dictionary
data2 = new Dictionary
(); data2.Add("statusCode", 172002); data2.Add("statusMsg", "无返回"); data2.Add("data", null); return data2; } } catch (Exception e) { throw e; } } ///
/// 外呼通知 /// ///
被叫号码 ///
可选字段 语音文件名称,格式 wav。与mediaTxt不能同时为空,不为空时mediaTxt属性失效 ///
可选字段 文本内容,默认值为空 ///
可选字段 显示的主叫号码,显示权限由服务侧控制 ///
可选字段 循环播放次数,1-3次,默认播放1次 ///
可选字段 语音文件名的类型,默认值为0,表示用户语音文件;值为1表示平台通用文件 ///
可选字段 外呼通知状态通知回调地址,云通讯平台将向该Url地址发送呼叫结果通知 ///
参数不能为空
///
///
public Dictionary
LandingCall(string to, string mediaName, string mediaTxt, string displayNum, string playTimes, int type, string respUrl) { Dictionary
initError = paramCheckRest(); if (initError != null) { return initError; } initError = paramCheckMainAccount(); if (initError != null) { return initError; } initError = paramCheckAppId(); if (initError != null) { return initError; } if (to == null) { throw new ArgumentNullException("to"); } if (mediaName==null && mediaTxt==null) { throw new ArgumentNullException("mediaName和mediaTxt同时为null"); } try { string date = DateTime.Now.ToString("yyyyMMddhhmmss"); // 构建URL内容 string sigstr = MD5Encrypt(m_mainAccount + m_mainToken + date); string uriStr = string.Format("https://{0}:{1}/{2}/Accounts/{3}/Calls/LandingCalls?sig={4}", m_restAddress, m_restPort, softVer, m_mainAccount, sigstr); Uri address = new Uri(uriStr); WriteLog("LandingCall url = " + uriStr); // 创建网络请求 HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest; setCertificateValidationCallBack(); // 构建Head request.Method = "POST"; Encoding myEncoding = Encoding.GetEncoding("utf-8"); byte[] myByte = myEncoding.GetBytes(m_mainAccount + ":" + date); string authStr = Convert.ToBase64String(myByte); request.Headers.Add("Authorization", authStr); // 构建Body StringBuilder data = new StringBuilder(); if (m_bodyType == EBodyType.EType_XML) { request.Accept = "application/xml"; request.ContentType = "application/xml;charset=utf-8"; data.Append("
"); data.Append("
").Append(m_appId).Append("
"); data.Append("
").Append(to).Append("
"); if (mediaName != null) { data.Append("
").Append(mediaName).Append("
"); } if (mediaTxt != null) { data.Append("
").Append(mediaTxt).Append("
"); } if (displayNum!=null) { data.Append("
").Append(displayNum).Append("
"); } if (playTimes!=null) { data.Append("
").Append(playTimes).Append("
"); } if (respUrl!=null) { data.Append("
").Append(respUrl).Append("
"); } data.Append("
"); } else { request.Accept = "application/json"; request.ContentType = "application/json;charset=utf-8"; data.Append("{ "); data.Append("\"to\":\"").Append(to).Append("\""); data.Append(",\"appId\":\"").Append(m_appId).Append("\""); if (mediaName != null) { data.Append(",\"mediaName\":\"").Append(mediaName).Append("\""); data.Append(",\"mediaNameType\":\"").Append(type).Append("\""); } if (mediaTxt != null) { data.Append(",\"mediaTxt\":\"").Append(mediaTxt).Append("\""); } if (displayNum != null) { data.Append(",\"displayNum\":\"").Append(displayNum).Append("\""); } if (playTimes != null) { data.Append(",\"playTimes\":\"").Append(playTimes).Append("\""); } if (respUrl != null) { data.Append(",\"respUrl\":\"").Append(respUrl).Append("\""); } data.Append("}"); } byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString()); WriteLog("LandingCall requestBody = " + data.ToString()); // 开始请求 using (Stream postStream = request.GetRequestStream()) { postStream.Write(byteData, 0, byteData.Length); } // 获取请求 using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { // Get the response stream StreamReader reader = new StreamReader(response.GetResponseStream()); string responseStr = reader.ReadToEnd(); WriteLog("LandingCall responseBody = " + responseStr); if (responseStr != null && responseStr.Length > 0) { //Dictionary
responseResult = new Dictionary
{ { "statusCode", "0" }, { "statusMsg", "成功" }, { "data", null } }; Dictionary
responseResult = new Dictionary
(); responseResult.Add("statusCode", "0"); responseResult.Add("statusMsg", "成功"); responseResult.Add("data", null); if (m_bodyType == EBodyType.EType_XML) { XmlDocument resultXml = new XmlDocument(); resultXml.LoadXml(responseStr); XmlNodeList nodeList = resultXml.SelectSingleNode("Response").ChildNodes; foreach (XmlNode item in nodeList) { if (item.Name == "statusCode") { responseResult["statusCode"] = item.InnerText; } else if (item.Name == "statusMsg") { responseResult["statusMsg"] = item.InnerText; } else if (item.Name == "LandingCall") { Dictionary
retData = new Dictionary
(); foreach (XmlNode subItem in item.ChildNodes) { retData.Add(subItem.Name, subItem.InnerText); } //responseResult["data"] = new Dictionary
{ { item.Name, retData } }; Dictionary
data1 = new Dictionary
(); data1.Add(item.Name, retData); responseResult["data"] = data1; } } } else { responseResult.Clear(); responseResult["resposeBody"] = responseStr; } return responseResult; } //return new Dictionary
{ { "statusCode", 172002 }, { "statusMsg", "无返回" }, { "data", null } }; Dictionary
data2 = new Dictionary
(); data2.Add("statusCode", 172002); data2.Add("statusMsg", "无返回"); data2.Add("data", null); return data2; } } catch (Exception e) { throw e; } } ///
/// 语音验证码 /// ///
接收号码 ///
验证码内容,为数字和英文字母,不区分大小写,长度4-8位 ///
可选字段 显示主叫号码,显示权限由服务侧控制 ///
可选字段 循环播放次数,1-3次,默认播放1次 ///
可选字段 语音验证码状态通知回调地址,云通讯平台将向该Url地址发送呼叫结果通知 ///
参数不能为空
///
///
public Dictionary
VoiceVerify(string to, string verifyCode, string displayNum, string playTimes, string respUrl) { Dictionary
initError = paramCheckRest(); if (initError != null) { return initError; } initError = paramCheckMainAccount(); if (initError != null) { return initError; } initError = paramCheckAppId(); if (initError != null) { return initError; } if (to == null) { throw new ArgumentNullException("to"); } if (verifyCode == null) { throw new ArgumentNullException("verifyCode"); } try { string date = DateTime.Now.ToString("yyyyMMddhhmmss"); // 构建URL内容 string sigstr = MD5Encrypt(m_mainAccount + m_mainToken + date); string uriStr = string.Format("https://{0}:{1}/{2}/Accounts/{3}/Calls/VoiceVerify?sig={4}", m_restAddress, m_restPort, softVer, m_mainAccount, sigstr); Uri address = new Uri(uriStr); WriteLog("VoiceVerify url = " + uriStr); // 创建网络请求 HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest; setCertificateValidationCallBack(); // 构建Head request.Method = "POST"; Encoding myEncoding = Encoding.GetEncoding("utf-8"); byte[] myByte = myEncoding.GetBytes(m_mainAccount + ":" + date); string authStr = Convert.ToBase64String(myByte); request.Headers.Add("Authorization", authStr); // 构建Body StringBuilder data = new StringBuilder(); if (m_bodyType == EBodyType.EType_XML) { request.Accept = "application/xml"; request.ContentType = "application/xml;charset=utf-8"; data.Append("
"); data.Append("
").Append(m_appId).Append("
"); data.Append("
").Append(verifyCode).Append("
"); data.Append("
").Append(to).Append("
"); if (displayNum != null) { data.Append("
").Append(displayNum).Append("
"); } if (playTimes != null) { data.Append("
").Append(playTimes).Append("
"); } if (respUrl != null) { data.Append("
").Append(respUrl).Append("
"); } data.Append("
"); } else { request.Accept = "application/json"; request.ContentType = "application/json;charset=utf-8"; data.Append("{ "); data.Append("\"to\":\"").Append(to).Append("\""); data.Append(",\"appId\":\"").Append(m_appId).Append("\""); data.Append(",\"verifyCode\":\"").Append(verifyCode).Append("\""); if (displayNum != null) { data.Append(",\"displayNum\":\"").Append(displayNum).Append("\""); } if (playTimes != null) { data.Append(",\"playTimes\":\"").Append(playTimes).Append("\""); } if (respUrl != null) { data.Append(",\"respUrl\":\"").Append(respUrl).Append("\""); } data.Append("}"); } byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString()); WriteLog("VoiceVerify requestBody = " + data.ToString()); // 开始请求 using (Stream postStream = request.GetRequestStream()) { postStream.Write(byteData, 0, byteData.Length); } // 获取请求 using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { // Get the response stream StreamReader reader = new StreamReader(response.GetResponseStream()); string responseStr = reader.ReadToEnd(); WriteLog("VoiceVerify responseBody = " + responseStr); if (responseStr != null && responseStr.Length > 0) { //Dictionary
responseResult = new Dictionary
{ { "statusCode", "0" }, { "statusMsg", "成功" }, { "data", null } }; Dictionary
responseResult = new Dictionary
(); responseResult.Add("statusCode", "0"); responseResult.Add("statusMsg", "成功"); responseResult.Add("data", null); if (m_bodyType == EBodyType.EType_XML) { XmlDocument resultXml = new XmlDocument(); resultXml.LoadXml(responseStr); XmlNodeList nodeList = resultXml.SelectSingleNode("Response").ChildNodes; foreach (XmlNode item in nodeList) { if (item.Name == "statusCode") { responseResult["statusCode"] = item.InnerText; } else if (item.Name == "statusMsg") { responseResult["statusMsg"] = item.InnerText; } else if (item.Name == "VoiceVerify") { Dictionary
retData = new Dictionary
(); foreach (XmlNode subItem in item.ChildNodes) { retData.Add(subItem.Name, subItem.InnerText); } //responseResult["data"] = new Dictionary
{ { item.Name, retData } }; Dictionary
data1 = new Dictionary
(); data1.Add(item.Name, retData); responseResult["data"] = data1; } } } else { responseResult.Clear(); responseResult["resposeBody"] = responseStr; } return responseResult; } //return new Dictionary
{ { "statusCode", 172002 }, { "statusMsg", "无返回" }, { "data", null } }; Dictionary
data2 = new Dictionary
(); data2.Add("statusCode", 172002); data2.Add("statusMsg", "无返回"); data2.Add("data", null); return data2; } } catch (Exception e) { throw e; } } ///
/// IVR外呼 暂不支持json格式 /// ///
待呼叫号码 ///
可选字段 用户数据,在
通知中返回,只允许填写数字字符 ///
可选字段 是否录音,可填项为true和false,默认值为false不录音 ///
参数不能为空
///
///
public Dictionary
IvrDial(string number, string userdata, string record) { Dictionary
initError = paramCheckRest(); if (initError != null) { return initError; } initError = paramCheckMainAccount(); if (initError != null) { return initError; } initError = paramCheckAppId(); if (initError != null) { return initError; } if (number == null) { throw new ArgumentNullException("number"); } try { string date = DateTime.Now.ToString("yyyyMMddhhmmss"); // 构建URL内容 string sigstr = MD5Encrypt(m_mainAccount + m_mainToken + date); string uriStr = string.Format("https://{0}:{1}/{2}/Accounts/{3}/ivr/dial?sig={4}", m_restAddress, m_restPort, softVer, m_mainAccount, sigstr); Uri address = new Uri(uriStr); WriteLog("IvrDial url = " + uriStr); // 创建网络请求 HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest; setCertificateValidationCallBack(); // 构建Head request.Method = "POST"; request.Accept = "application/xml"; request.ContentType = "application/xml;charset=utf-8"; Encoding myEncoding = Encoding.GetEncoding("utf-8"); byte[] myByte = myEncoding.GetBytes(m_mainAccount + ":" + date); string authStr = Convert.ToBase64String(myByte); request.Headers.Add("Authorization", authStr); // 构建Body StringBuilder data = new StringBuilder(); data.Append("
"); data.Append("
").Append(m_appId).Append("
"); data.Append("
"); data.Append("
"); byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString()); WriteLog("IvrDial requestBody = " + data.ToString()); // 开始请求 using (Stream postStream = request.GetRequestStream()) { postStream.Write(byteData, 0, byteData.Length); } // 获取请求 using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { // Get the response stream StreamReader reader = new StreamReader(response.GetResponseStream()); string responseStr = reader.ReadToEnd(); WriteLog("IvrDial responseBody = " + responseStr); if (responseStr != null && responseStr.Length > 0) { //Dictionary
responseResult = new Dictionary
{ { "statusCode", "0" }, { "statusMsg", "成功" }, { "data", null } }; Dictionary
responseResult = new Dictionary
(); responseResult.Add("statusCode", "0"); responseResult.Add("statusMsg", "成功"); responseResult.Add("data", null); if (m_bodyType == EBodyType.EType_XML) { XmlDocument resultXml = new XmlDocument(); resultXml.LoadXml(responseStr); XmlNodeList nodeList = resultXml.SelectSingleNode("Response").ChildNodes; foreach (XmlNode item in nodeList) { if (item.Name == "statusCode") { responseResult["statusCode"] = item.InnerText; } else if (item.Name == "statusMsg") { responseResult["statusMsg"] = item.InnerText; } } } else { responseResult.Clear(); responseResult["resposeBody"] = responseStr; } return responseResult; } //return new Dictionary
{ { "statusCode", 172002 }, { "statusMsg", "无返回" }, { "data", null } }; Dictionary
data2 = new Dictionary
(); data2.Add("statusCode", 172002); data2.Add("statusMsg", "无返回"); data2.Add("data", null); return data2; } } catch (Exception e) { throw e; } } ///
/// 话单下载 /// ///
day 代表前一天的数据(从00:00 – 23:59);week代表前一周的数据(周一 到周日);month表示上一个月的数据(上个月表示当前月减1,如果今天是4月10号,则查询结果是3月份的数据) ///
客户的查询条件,由客户自行定义并提供给云通讯平台。默认不填忽略此参数 ///
参数不能为空
///
///
public Dictionary
BillRecords(string date, string keywords) { Dictionary
initError = paramCheckRest(); if (initError != null) { return initError; } initError = paramCheckMainAccount(); if (initError != null) { return initError; } initError = paramCheckAppId(); if (initError != null) { return initError; } if (date == null) { throw new ArgumentNullException("date"); } try { string mydate = DateTime.Now.ToString("yyyyMMddhhmmss"); // 构建URL内容 string sigstr = MD5Encrypt(m_mainAccount + m_mainToken + mydate); string uriStr = string.Format("https://{0}:{1}/{2}/Accounts/{3}/BillRecords?sig={4}", m_restAddress, m_restPort, softVer, m_mainAccount, sigstr); Uri address = new Uri(uriStr); WriteLog("BillRecords url = " + uriStr); // 创建网络请求 HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest; setCertificateValidationCallBack(); // 构建Head request.Method = "POST"; Encoding myEncoding = Encoding.GetEncoding("utf-8"); byte[] myByte = myEncoding.GetBytes(m_mainAccount + ":" + mydate); string authStr = Convert.ToBase64String(myByte); request.Headers.Add("Authorization", authStr); // 构建Body StringBuilder data = new StringBuilder(); if (m_bodyType == EBodyType.EType_XML) { request.Accept = "application/xml"; request.ContentType = "application/xml;charset=utf-8"; data.Append("
"); data.Append("
").Append(m_appId).Append("
"); data.Append("
").Append(date).Append("
"); if (keywords != null) { data.Append("
").Append(keywords).Append("
"); } data.Append("
"); } else { request.Accept = "application/json"; request.ContentType = "application/json;charset=utf-8"; data.Append("{ "); data.Append("\"appId\":\"").Append(m_appId).Append("\""); data.Append(",\"date\":\"").Append(date).Append("\""); if (keywords != null) { data.Append(",\"keywords\":\"").Append(keywords).Append("\""); } data.Append("}"); } byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString()); WriteLog("BillRecords requestBody = " + data.ToString()); // 开始请求 using (Stream postStream = request.GetRequestStream()) { postStream.Write(byteData, 0, byteData.Length); } // 获取请求 using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { // Get the response stream StreamReader reader = new StreamReader(response.GetResponseStream()); string responseStr = reader.ReadToEnd(); WriteLog("BillRecords responseBody = " + responseStr); if (responseStr != null && responseStr.Length > 0) { //Dictionary
responseResult = new Dictionary
{ { "statusCode", "0" }, { "statusMsg", "成功" }, { "data", null } }; Dictionary
responseResult = new Dictionary
(); responseResult.Add("statusCode", "0"); responseResult.Add("statusMsg", "成功"); responseResult.Add("data", null); Dictionary
retData = new Dictionary
(); if (m_bodyType == EBodyType.EType_XML) { XmlDocument resultXml = new XmlDocument(); resultXml.LoadXml(responseStr); XmlNodeList nodeList = resultXml.SelectSingleNode("Response").ChildNodes; foreach (XmlNode item in nodeList) { if (item.Name == "statusCode") { responseResult["statusCode"] = item.InnerText; } else if (item.Name == "statusMsg") { responseResult["statusMsg"] = item.InnerText; } else { retData.Add(item.Name, item.InnerText); } } } else { responseResult.Clear(); responseResult["resposeBody"] = responseStr; } if (retData.Count > 0) { responseResult["data"] = retData; } return responseResult; } //return new Dictionary
{ { "statusCode", 172002 }, { "statusMsg", "无返回" }, { "data", null } }; Dictionary
data2 = new Dictionary
(); data2.Add("statusCode", 172002); data2.Add("statusMsg", "无返回"); data2.Add("data", null); return data2; } } catch (Exception e) { throw e; } } #region MD5 和 https交互函数定义 private void WriteLog(string log) { if (m_isWriteLog) { string strFilePath = GetLogPath(); System.IO.FileStream fs = new System.IO.FileStream(strFilePath, System.IO.FileMode.Append); System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, System.Text.Encoding.Default); sw.WriteLine(DateTime.Now.ToString() + "\t" + log); sw.Close(); fs.Close(); } } ///
/// 检查服务器地址信息 /// ///
private Dictionary
paramCheckRest() { int statusCode = 0; string statusMsg = null; if (m_restAddress == null) { statusCode = 172004; statusMsg = "IP空"; } else if (m_restPort == null) { statusCode = 172005; statusMsg = "端口错误"; } if (statusCode != 0) { //return new Dictionary
{ { "statusCode", statusCode + "" }, { "statusMsg", statusMsg }, { "data", null } }; Dictionary
data2 = new Dictionary
(); data2.Add("statusCode", statusCode + ""); data2.Add("statusMsg", statusMsg); data2.Add("data", null); return data2; } return null; } ///
/// 检查主帐号信息 /// ///
private Dictionary
paramCheckMainAccount() { int statusCode = 0; string statusMsg = null; if (m_mainAccount == null) { statusCode = 172006; statusMsg = "主帐号空"; } else if (m_mainToken == null) { statusCode = 172007; statusMsg = "主帐号令牌空"; } if (statusCode != 0) { //return new Dictionary
{ { "statusCode", statusCode + "" }, { "statusMsg", statusMsg }, { "data", null } }; Dictionary
data2 = new Dictionary
(); data2.Add("statusCode", statusCode + ""); data2.Add("statusMsg", statusMsg); data2.Add("data", null); return data2; } return null; } ///
/// 检查子帐号信息 /// ///
private Dictionary
paramCheckSunAccount() { int statusCode = 0; string statusMsg = null; if (m_subAccount == null) { statusCode = 172008; statusMsg = "子帐号空"; } else if (m_subToken == null) { statusCode = 172009; statusMsg = "子帐号令牌空"; } else if (m_voipAccount == null) { statusCode = 1720010; statusMsg = "VoIP帐号空"; } else if (m_voipPwd == null) { statusCode = 172011; statusMsg = "VoIP密码空"; } if (statusCode != 0) { //return new Dictionary
{ { "statusCode", statusCode+"" }, { "statusMsg", statusMsg }, { "data", null } }; Dictionary
data2 = new Dictionary
(); data2.Add("statusCode", statusCode + ""); data2.Add("statusMsg", statusMsg); data2.Add("data", null); return data2; } return null; } ///
/// 检查应用ID /// ///
private Dictionary
paramCheckAppId() { if (m_appId == null) { //return new Dictionary
{ { "statusCode", 172012 + "" }, { "statusMsg", "应用ID为空" }, { "data", null } }; Dictionary
data2 = new Dictionary
(); data2.Add("statusCode", 172012 + ""); data2.Add("statusMsg", "应用ID为空"); data2.Add("data", null); return data2; } return null; } ///
/// MD5加密 /// ///
原内容 ///
加密后内容
public static string MD5Encrypt(string source) { // Create a new instance of the MD5CryptoServiceProvider object. System.Security.Cryptography.MD5 md5Hasher = System.Security.Cryptography.MD5.Create(); // Convert the input string to a byte array and compute the hash. byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(source)); // Create a new Stringbuilder to collect the bytes and create a string. StringBuilder sBuilder = new StringBuilder(); // Loop through each byte of the hashed data and format each one as a hexadecimal string. for (int i = 0; i < data.Length; i++) { sBuilder.Append(data[i].ToString("X2")); } // Return the hexadecimal string. return sBuilder.ToString(); } ///
/// 设置服务器证书验证回调 /// public void setCertificateValidationCallBack() { System.Net.ServicePointManager.ServerCertificateValidationCallback = CertificateValidationResult; } ///
/// 证书验证回调函数 /// ///
///
///
///
///
public bool CertificateValidationResult(object obj, System.Security.Cryptography.X509Certificates.X509Certificate cer, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors error) { return true; } #endregion }}

 

转载于:https://www.cnblogs.com/webapi/p/5711764.html

你可能感兴趣的文章
『扩欧简单运用』
查看>>
QTP教程入门到高级(转)
查看>>
机器学习基石HOW BETTER部分(3)
查看>>
BZOJ 1010: [HNOI2008]玩具装箱toy | 单调队列优化DP
查看>>
3D打印机如何添加自动调平功能
查看>>
iOS开发之 第三方字体的应用
查看>>
Executor框架的简要分析
查看>>
C++实现获取本机机器名及外网IP代码
查看>>
Uncaught TypeError: Illegal invocation
查看>>
只有高中学历的我是怎样加入谷歌的?
查看>>
MySQL的安装和启动
查看>>
hdu 1020 Encoding
查看>>
hdu 4006 The kth great number(优先队列)
查看>>
JDBC连接MySQL数据库
查看>>
一般算数表达式转换为“后缀式”
查看>>
600-460 Exam Cram - Authentic 600-460 Exam Dumps
查看>>
树-堆结构练习——合并果子之哈夫曼树(其实是优先队列)
查看>>
poj3686The Windy's(费用流)
查看>>
持久化对象
查看>>
rest-framework之视图和源码解析
查看>>