admin - 2009/12/7 16:11:00
本代码封装了对电信能力开放平台提供的发短信、语言呼叫接口的调用。把此代码加到自己的项目中就很容易实现短信验证码、短信提示、回拨卡业务等功能。
TelephoneAPI.cs:
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Security.Cryptography;
- using System.Web;
- using System.Net;
- namespace Telephone
- {
- public enum ResultCode
- {
- OK = 200,
- 非法请求_禁止发送 = 403,
- 请求消息错误 = 400,
- 请求失败 = 480
- }
- public class API
- {
- string transactionID;
- string token;
- #region 获取由SHA1加密的字符串
- public string EncryptToSHA1(string str)
- {
- SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
- byte[] str1 = Encoding.UTF8.GetBytes(str);
- byte[] str2 = sha1.ComputeHash(str1);
- sha1.Clear();
- (sha1 as IDisposable).Dispose();
- return Convert.ToBase64String(str2);
- }
- #endregion
- #region 获取由MD5加密的字符串
- public string EncryptToMD5(string str)
- {
- MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
- byte[] str1 = Encoding.UTF8.GetBytes(str);
- byte[] str2 = md5.ComputeHash(str1, 0, str1.Length);
- md5.Clear();
- (md5 as IDisposable).Dispose();
- return Convert.ToBase64String(str2);
- }
- #endregion
- public string GetBase64String(string str)
- {
- byte[] buf = Encoding.UTF8.GetBytes(str);
- return Convert.ToBase64String(buf);
- }
- /// <summary>
- /// 获取一个时间戳,它是从1970年1月1日0时开始至现在的毫秒数
- /// </summary>
- /// <returns></returns>
- public long GetTimeStamp()
- {
- TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1);
- long timeStamp = (long)ts.TotalMilliseconds;
- return timeStamp;
- }
- /// <summary>
- /// 登录
- /// </summary>
- /// <param name="apID">你的用户ID</param>
- /// <param name="apUserAccount">你的用户名</param>
- /// <param name="funID">功能ID(短信或者呼叫等能力的编号)</param>
- /// <param name="apKey">密钥</param>
- public void Login(int apID, string apUserAccount, int funID, string apKey)
- {
- long timeStamp = this.GetTimeStamp();
- //AuthRequestValue 格式如下:TimeStamp + “$” + APID+ “$” + APUserAccount + “$” + FunID + “$” + UrlEncode(Authenticator),其中Authenticator的生成算法如下:Authenticator = Base64(SHA1 (TimeStamp + “$” + APID+ “$” + APUserAccount + “$” + FunID + “$” + APKEY))
- string authenticator = EncryptToSHA1(string.Format("{0}${1}${2}${3}${4}", timeStamp, apID, apUserAccount, funID, apKey));
- string authRequestValue = string.Format("{0}${1}${2}${3}${4}", timeStamp, apID, apUserAccount, funID, HttpUtility.UrlEncode(authenticator));
- using (WebClient client = new WebClient())
- {
- client.Encoding = Encoding.UTF8;
- string authResponseValue = client.DownloadString(string.Format("http://www.ctopen.cn/InterfaceForAP/Auth.aspx?AuthRequest={0}", authRequestValue));
- //AuthResponseValue格式如下:Result + “$”+ TransactionID + “$”+ Token+ “$” + ErrorDescription+ “$” + TimeStamp
- string[] resultArr = authResponseValue.Split(new char[] { '$' });
- int resultCode = int.Parse(resultArr[0]);
- if (resultCode == 0)
- {
- this.transactionID = resultArr[1];
- this.token = resultArr[2];
- timeStamp = long.Parse(resultArr[4]);
- }
- else
- {
- string errorDesc = resultArr[3];
- throw new Exception(errorDesc);
- }
- }
- }
- /// <summary>
- /// 打电话
- /// </summary>
- /// <param name="num1">第一呼叫号码(目前会听到广告)</param>
- /// <param name="num2">第二呼叫号码</param>
- /// <param name="num3"></param>
- /// <param name="num4"></param>
- /// <param name="num5"></param>
- /// <returns>返回本次通话的会话ID,用于挂断电话</returns>
- public string MakeCall(string num1, string num2, string num3, string num4, string num5)
- {
- string callbackUrl = "http://mail.loopc.com:443/CallReport.aspx";//用于记录通话时间等信息。查看费用请访问[url]http://mail.loopc.com:443/[/url]你的电话号码.txt
- string safeCode = "";//好像没有作用。
- long timeStamp = this.GetTimeStamp();//时间戳,用于计费
- //用以标识唯一的呼叫,呼叫模块在中间计费或结束计费上报时将通过该字段上报。格式:第一呼叫号_时间戳(秒)_三位随机数
- string sessionId = string.Format("{0}_{1}_{2}", num1, timeStamp, 123);//挂机时会用到
- // 其中MakeCallRequestValue格式如下:Token+ “$” + Sessionid + “$” + Num1 + “$” + Num2+ “$”+Num3+ “$”+Num4+ “$”+Num5+ “$”+Code+ “$”+Url
- string makeCallRequestValue = string.Format("{0}${1}${2}${3}${4}${5}${6}${7}${8}", this.token, sessionId, num1, num2, num3, num4, num5, safeCode, callbackUrl);
- using (WebClient client = new WebClient())
- {
- client.Encoding = Encoding.UTF8;
- string makeCallResponse = client.DownloadString(string.Format("http://118.123.249.4/ims/ghcall.php?MakeCallRequest={0}", makeCallRequestValue));
- ResultCode resultCode = (ResultCode)int.Parse(makeCallResponse);//200: 发送成功,403:非法请求_禁止发送,400:请求消息错误,480:请求失败
- if (resultCode != ResultCode.OK)
- {
- throw new Exception(resultCode.ToString());
- }
- return sessionId;
- }
- }
- /// <summary>
- /// 挂断电话
- /// </summary>
- /// <param name="sessioinId">打电话时的会话ID</param>
- public void HangupCall(string sessioinId)
- {
- using (WebClient client = new WebClient())
- {
- client.Encoding = Encoding.UTF8;
- string response = client.DownloadString(string.Format("http://118.123.249.4/ims/ghcall.php?HangupCallRequest={0}", sessioinId));
- ResultCode resultCode = (ResultCode)int.Parse(response);//挂断呼叫结果{200: 挂机成功,400:请求消息错误,480:请求失败}
- if (resultCode != ResultCode.OK)
- {
- throw new Exception(resultCode.ToString());
- }
- }
- }
- /// <summary>
- /// 发送短信给指定号码
- /// </summary>
- /// <param name="msg">消息内容(不超过70汉字,英文不能超过140字符)</param>
- /// <param name="toNo">接收者号码</param>
- public void SendMsg(string msg, string toNo)
- {
- string fromNo = "";//指定以电信的哪个号码发出去。
- msg = GetBase64String(msg);
- //SendMsgRequestValue=UrlEncode(Token+ “$” + Phone + “$” + Msg + “$” + From)
- string sendMsgRequestValue = string.Format("{0}${1}${2}${3}", this.token, toNo, msg, fromNo);
- sendMsgRequestValue = HttpUtility.UrlEncode(sendMsgRequestValue);
- using (WebClient client = new WebClient())
- {
- client.Encoding = Encoding.UTF8;
- string response = client.DownloadString(string.Format("http://118.123.249.4/ims/ghsendim.php?SendMsgRequest={0}", sendMsgRequestValue));
- response = response.Replace("SendMsgResponse=", "");//接口返回格式不统一,只好去除多余的字符。
- ResultCode resultCode = (ResultCode)int.Parse(response); //短信发送结果:{200: 发送成功,403:非法请求_禁止发送,400:请求消息错误}
- if (resultCode != ResultCode.OK)
- {
- throw new Exception(resultCode.ToString());
- }
- }
- }
- }
- }
复制代码测试代码
Program.cs:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Telephone
- {
- class Program
- {
- static void Main(string[] args)
- {
- API api = new API();
- api.Login(10000111, "xu123456", 10000033, "5e30d0303a874415ae641f227b5c29b6"); //登录
- api.SendMsg("hello", "13500000001");//发短信
- string sessionId = api.MakeCall("13500000001", "1350000002", null, null, null);//打电话,目前好像就支持两个人通话
- Console.WriteLine(sessionId);
- Console.WriteLine("press any key to hangup the call.");
- Console.ReadLine();
- api.HangupCall(sessionId);//挂断电话
- Console.WriteLine("end.");
- Console.ReadLine();
- }
- }
- }
复制代码
net200 - 2009/12/7 22:52:00
支持 ,这个需要跟电信申请什么么
admin - 2009/12/8 14:16:00
原帖由 net200 于 2009-12-7 22:52:00 发表 支持 ,这个需要跟电信申请什么么 |
对,需要到电信的网站注册账号就可以使用了。
电信能力开放平台网址:
http://open.ctfactory.com/Portal/index/index.aspx