一、彩信语音SDK初始化CWebInterface
CWebInterface::CWebInterface(CString &strSN,CString &strPwd)
{
m_pEndpoint =NULL;
m_pSN = UnicodeToUTF8(strSN);
CString strMD5Source = strSN + strPwd;
char *pMD5Source =UnicodeToANSI(strMD5Source);
char szMD5Code[64]={0};
MD5Memory(pMD5Source,strlen(pMD5Source),szMD5Code,64);
strupr(szMD5Code);
free(pMD5Source);
m_pMd5 =(char*)malloc(33);
memcpy(m_pMd5,szMD5Code,33);
m_WebService.soap->recv_timeout =150; //秒
m_WebService.soap->send_timeout =150;
m_WebService.endpoint = "http://sdk3.entinfo.cn:8060/webservice.asmx";//赋个初始值,防止从配置文件读取服务器地址失败时访问不了服务器
if(strlen(g_pEndpoint) >0)
{
m_WebService.endpoint=g_pEndpoint;
}
soap_set_mode(m_WebService.soap, SOAP_C_UTFSTRING); //设置编码为UTF8.SDK说明文档上说使用gb2312,但实测必须用utf8.
}
二、切换主备服务器SetWebsAddress(char *pszAddr)
void CWebInterface::SetWebsAddress(char *pszAddr)//切换主备服务器
{
if (!pszAddr) return;
if (m_pEndpoint) free(m_pEndpoint);
int nLen =strlen(pszAddr)+1;
m_pEndpoint = (char*)malloc(nLen);
ZeroMemory(m_pEndpoint,nLen);
strcpy(m_pEndpoint,pszAddr);
m_WebService.endpoint =m_pEndpoint;
}
三、查询账户余额GetBalance()
参数名称 | 说明 | 备注 |
Sn | 软件序列号 | 格式XXX-XXX-XXX-XXXXX |
Pwd | 密码 | md5(sn+password) 32位大写 密文 +表示连接 |
函数返回值:String(余额)
接口地址:http://sdk3.entinfo.cn:8060/webservice.asmx?op=balance
示例:序列号SDK-SSD-010-00001 密码 xxxxxx
参数输入:
SN= SDK-SSD-010-00001
PWD= 3B5D3C427365F40C1D27682D78BB31E0
示例返回结果: 余额79109条短信
XML格式:
具体函数C++实现:
int CWebInterface::GetBalance()
{
_ns1__balance param1;
_ns1__balanceResponse WebsResult;
param1.sn = m_pSN;
param1.pwd =m_pMd5;
SetTimeout(30);
int nRet =m_WebService.__ns2__balance(¶m1,&WebsResult);
if (nRet)
{
if (g_bHaveChange)//已经切换成备用地址,此时主备都不通,返回查询失败
{
return WEBS_RET_FAIL;
}
else
{
for(int i=0;i<=g_nFailedCount;i++)
{
nRet =m_WebService.__ns2__balance(¶m1,&WebsResult);
if (!nRet)
{
break;
}
}
if (nRet)
{
SetWebsAddress(g_pBackupEndpoint);//切换到备用地址
char szTmp[512];
strcpy(g_pEndpoint,g_pBackupEndpoint);
g_bHaveChange = true;
AfxMessageBox("主地址不通,已经自动切换到备用地址,请检查您的网络,如仍有问题请联系我们!");
nRet =m_WebService.__ns2__balance(¶m1,&WebsResult);
if (nRet)
{
return WEBS_RET_FAIL;
}
}
}
}
int nSvrRet =atoi(WebsResult.balanceResult);
if ( nSvrRet < WEBS_RET_OK)
{
return WEBS_RET_FAIL;
}
return nSvrRet;
}
四、群发彩信函数SendMMS(CString csTitle,char *pMobiles,MMSCONTENT *MmsContent,CString csTime)
参数名称 | 说明 | 是否必须 | 备注 |
Sn | 软件序列号 | 是 | 格式XXX-XXX-XXX-XXXXX |
Pwd | 密码 | 是 | md5(sn+password) 32位大写密文 |
title
|
彩信主题
|
是
| 限制在30字符以内 |
Mobile | 手机号 | 是 | 手机号码多个以英文逗号隔开 |
Content | 内容 | 是 | base64编码(文件名1,文件base64编码串;文件名2,文件base64编码串);实际发送文件小于等于50*1024 字节 |
stime | 定时时间 | 否 | 例如:2010-12-29 16:27:03(非定时置空) |
函数返回值:String。
content是您发送的彩信的内容:包含图片和声音及文本;
1.文件类型: 文本是.txt 图片是.jpg或者.gif 声音是.mid或者.amr
2.内容格式:
帧数_文件类型.扩展名 + 英文逗号 + 文件的base64编码字符串 + 分号 ;
例如:“1_1.txt,” + 文件base64编码字符串 + “;” + “1_2.jpg,” + 文件base64编码字符串+”;”+”1_3.mid”+文件base64编码字符串
文本是 n_1.txt 图片是 n_2.jpg 或者是 n_2.gif 声音是 n_3.mid ;
N表示当前第几帧,后缀不变,编码均为GB2312。
接口地址:http://sdk3.entinfo.cn:8060/webservice.asmx?op=mdMmsSend
示例1
SN= SDK-SSD-010-00001
PWD=3B5D3C427365F40C1D27682D78BB31E0
title:彩信标题
Mobile:139***404,138***213…………….
Content:测试
Stime: ""
输出结果:
XML格式:
具体函数C++实现:
int CWebInterface::SendMMS(CString csTitle,char *pMobiles,MMSCONTENT *MmsContent,CString csTime)
{
_ns1__mdMmsSend param1;
_ns1__mdMmsSendResponse WebsResult;
#define DESBUF_LEN MAX_FILE_LEN_MMS *2
int iWillSendLen=0;
CString csSendContent;
CString csPicFullPath=MmsContent->csPicPath;
CString csSoundFullPath=MmsContent->csSoundPath;
CString csTxt=MmsContent->csTxt;
if (csPicFullPath!=_T(""))
{
CString csPicType;
char *pBase64Pic=new char[DESBUF_LEN];
ZeroMemory(pBase64Pic,DESBUF_LEN);
char *pFileBufPic= new char[MAX_FILE_LEN_MMS ];
ZeroMemory(pFileBufPic,MAX_FILE_LEN_MMS);
int iDesBufLen= DESBUF_LEN;
int iIndex=csPicFullPath.ReverseFind(_T('.'));
CString csExtName=csPicFullPath.Right(csPicFullPath.GetLength()-iIndex);
csPicType.Format(_T("%d_2%s"),1,csExtName);
csPicType+=_T(",");
char *pTemp=UnicodeToUTF8(csPicType);
strcpy(pBase64Pic,pTemp);
free(pTemp);
int iPosEncode = strlen(pBase64Pic);
char *pszFileName = UnicodeToANSI(csPicFullPath);
FILE *pFile = fopen(pszFileName,"rb");
if(!pFile)
{
delete []pBase64Pic;
delete []pFileBufPic;
return -100;
}
free(pszFileName);
fseek(pFile,0,SEEK_END);
long lFileLen = ftell(pFile);
if (lFileLen > MAX_FILE_LEN_MMS)
{
delete []pBase64Pic;
delete []pFileBufPic;
CString csPicErr;
csPicErr.Format(_T("图片文件大小超过%dK"),MAX_FILE_LEN_MMS/1024);
::MessageBox(0,csPicErr,MESSAGE_INFO,MB_OK|MB_ICONERROR);
return -101;
}
fseek(pFile,0,SEEK_SET);
int iReadFile = fread(pFileBufPic,1,lFileLen,pFile);
fclose(pFile);
Base64Encode((byte*)pFileBufPic,iReadFile,pBase64Pic + iPosEncode ,&iDesBufLen);
WCHAR *wpPic=UTF8ToUnicode(pBase64Pic);
CString csEncodePic=wpPic;
csSendContent+=csEncodePic+_T(";");
iWillSendLen+=lFileLen;
free(wpPic);
delete []pBase64Pic;
delete []pFileBufPic;
}
if (csSoundFullPath!=_T(""))
{
CString csSoundType;
char *pBase64Sound=new char[DESBUF_LEN];
ZeroMemory(pBase64Sound,DESBUF_LEN);
char *pFileBufSound= new char[MAX_FILE_LEN_MMS ];
ZeroMemory(pFileBufSound,MAX_FILE_LEN_MMS);
int iDesBufLen= DESBUF_LEN;
int iIndex=csSoundFullPath.ReverseFind(_T('.'));
CString csExtName=csSoundFullPath.Right(csSoundFullPath.GetLength()-iIndex);
csSoundType.Format(_T("%d_3%s"),1,csExtName);
csSoundType+=_T(",");
char *pTemp=UnicodeToUTF8(csSoundType);
strcpy(pBase64Sound,pTemp);
free(pTemp);
int iPosEncode = strlen(pBase64Sound);
char *pszFileName = UnicodeToANSI(csSoundFullPath);
FILE *pFile = fopen(pszFileName,"rb");
if(!pFile)
{
delete []pBase64Sound;
delete []pFileBufSound;
return -100;
}
free(pszFileName);
fseek(pFile,0,SEEK_END);
long lFileLen = ftell(pFile);
if (lFileLen > MAX_FILE_LEN_MMS)
{
delete []pBase64Sound;
delete []pFileBufSound;
CString csSoundErr;
csSoundErr.Format(_T("声音文件大小超过%dK"),MAX_FILE_LEN_MMS/1024);
::MessageBox(0,csSoundErr,MESSAGE_INFO,MB_OK|MB_ICONERROR);
return -101;
}
fseek(pFile,0,SEEK_SET);
int iReadFile = fread(pFileBufSound,1,lFileLen,pFile);
fclose(pFile);
Base64Encode((byte*)pFileBufSound,iReadFile,pBase64Sound + iPosEncode ,&iDesBufLen);
WCHAR *wpPic=UTF8ToUnicode(pBase64Sound);
CString csEncodePic=wpPic;
csSendContent+=csEncodePic+_T(";");
iWillSendLen+=lFileLen;
free(wpPic);
delete []pBase64Sound;
delete []pFileBufSound;
}
if (csTxt!=_T(""))
{
CString csTxtType;
char *pBase64Txt=new char[DESBUF_LEN];
ZeroMemory(pBase64Txt,DESBUF_LEN);
int iDesBufLen= DESBUF_LEN;
csTxtType.Format(_T("%d_1.txt"),1);
csTxtType+=_T(",");
char *pTemp=UnicodeToUTF8(csTxtType);
strcpy(pBase64Txt,pTemp);
free(pTemp);
int iPosEncodeTxt = strlen(pBase64Txt);
char * pFileBufTxt=UnicodeToANSI(csTxt);
int iLen=strlen(pFileBufTxt);
Base64Encode((byte*)pFileBufTxt,iLen,pBase64Txt + iPosEncodeTxt ,&iDesBufLen);
WCHAR *wpTxt=UTF8ToUnicode(pBase64Txt);
CString csEncodeTxt=wpTxt;
csSendContent+=csEncodeTxt+_T(";");
iWillSendLen+=iLen;
free(pFileBufTxt);
free(wpTxt);
delete []pBase64Txt;
}
csSendContent=csSendContent.Left(csSendContent.GetLength()-1);//去掉最后一个分号.
if (iWillSendLen>MAX_FILE_LEN_MMS)
{
CString csTotle;
csTotle.Format(_T("本条彩信超过%dK"),MAX_FILE_LEN_MMS/1024);
::MessageBox(0,csTotle,MESSAGE_INFO,MB_OK|MB_ICONERROR);
return -101;
}
param1.sn =UnicodeToUTF8(Pregister->szSerNO);
param1.pwd =m_pMd5;
param1.mobile=pMobiles;
param1.title=UnicodeToUTF8(csTitle);
param1.content=UnicodeToUTF8(csSendContent);
param1.stime= UnicodeToUTF8(csTime);
SetTimeout(150);
int nRet =m_WebService.__ns2__mdMmsSend(¶m1,&WebsResult);
free(param1.sn);
free(param1.title);
// free(param1.mobile);
free(param1.content);
free(param1.stime);
if (nRet)
{
return WEBS_RET_FAIL;
}
char szResult[6]={0};
memcpy(szResult,WebsResult.mdMmsSendResult,5);
int nSvrRet =atoi(szResult);
if ( nSvrRet >= WEBS_RET_OK)
{
return WEBS_RET_OK;
}
return WEBS_RET_FAIL;
}
五、webservice返回集合对照表:
返回值 | 返回值说明 |
1 | 没有数据需要接收 |
-2 | 帐号/密码不正确 |
-3 | 重复登陆 |
-4 | 余额不足 |
-5 | 数据格式错误 |
-6 | 参数有误 |
-8 | 流量控制错误 |
-11 | 数据库错误 |
-12 | 序列号状态错误 |
-13 | 没有提交增值内容 |
-14 | 服务器写文件失败 |
-17 | 该接口禁止使用该方法 |
-18 | 等上一批提交结果返回再继续下一批提交 |
-15 | 内容长度长 |
-22 | Ip绑定 |
六、附加说明:
1.彩信语音SDK地址:
常用接口地址:http://sdk3.entinfo.cn:8060/webservice.asmx
2.其它说明:
(1)开发使用的帐号必须为SDK开头,如SDK-SSD-010-00001,帐号第一次需要调用Register方法注册一次.仅需注册一次即可,信息必须真实
(2)UnRegister与Register配合使用, 连续使用不得超过10次/天;
3. 郑重声明:
(1)禁止相同的内容多个手机号连续一条一条提交. 否则禁用帐号,由此带来损失由客户自行负责.
(2)请客户提供外网服务器IP以便于绑定IP发送,提高账号的安全性!
(3)在程序里最好有配置文件,程序自动判断当某个接口连接超时提交速度变慢时.程序可以自动切换其它的接口以下是推荐的几个服务器,仅接口地址不同而已.方法全部相同;
地址1:http://sdk3.entinfo.cn:8060/webservice.asmx
这些地址都是标准的webservice地址,C#,Java客户可以按照自己熟悉的方式去解析String
或者
地址1:http://sdk3.entinfo.cn:8060/webservice.asmx?wsdl
八、示例Demo源代码下载: