基于CMPP2以及东软API的短信平台的开发
10MS发表于www.10ms.net
本平台针对网关开发。
东软API不知道是什么版本的,源代码出现某个错误,某数组允许长度64,API文档里也说明是64,可是它
是65。破解之,改之,重新编译之,无问题。
API文档里示例程序也有若干错误,很奇怪,既然是对外发布的版本,居然没有一条注释,改之,现在调
试运行没有问题,可以发送短信。
下面提供的只是个简单的示例,要强调一些细节。假设你把API放在d:\message\com\commerceware\cmpp\
目录下,我的示例也在改目录下,那么编译和运行请转到message目录下
编译javac -classpath D:\message\ f:\message\com\commerceware\cmpp\Test.java
运行java com.commerceware.cmpp.Test 13711111111 我爱ABC
运行时第一个参数是对方手机号,第二个参数是发送内容。以下是程序内容,登陆IP和用户名密码等我已
隐藏。但程序是正常的,你首先需要申请一个SP的资格
package com.commerceware.cmpp;
public class Test{
public void readPaME(conn_desc con)
{
cmppe_result cr = null;
CMPP cp = new CMPP();
try{
cr = cp.readResPack(con);
System.out.println("cr.pack_id=" + cr.pack_id);
switch(cr.pack_id)
{
case CMPP.CMPPE_NACK_RESP:
System.out.println("CMPP.CMPPE_NACK_RESP:get nack pack");
break;
case CMPP.CMPPE_LOGIN_RESP:
cmppe_login_result cl;
cl = (cmppe_login_result)cr;
System.out.println("CMPP.CMPPE_LOGIN_RESP:login resp: STAT = "+cl.stat);
break;
case CMPP.CMPPE_LOGOUT_RESP:
System.out.println("CMPP.CMPPE_LOGOUT_RESP:logout resp: STAT = "+cr.stat);
break;
case CMPP.CMPPE_SUBMIT_RESP:
System.out.println("CMPP.CMPPE_SUBMIT_RESP");
cmppe_submit_result sr;
sr = (cmppe_submit_result)cr;
cmppe_cancel cc1 = new
cmppe_cancel();
//cc1.set_msgid(new
String(sr.msg_id)));
cc1.set_msgid(new
String(sr.msg_id));
break;
case CMPP.CMPPE_DELIVER:
System.out.println("CMPP.CMPPE_DELIVER:deliver: STAT = 0");
cmppe_deliver_result cd=
(cmppe_deliver_result)cr;
cp.cmpp_send_deliver_resp(con,cd.seq,cd.stat);
break;
case CMPP.CMPPE_CANCEL_RESP:
System.out.println("CMPP.CMPPE_CANCEL_RESP:cancel: STAT = " + cr.stat);
break;
case CMPP.CMPPE_ACTIVE_RESP:
System.out.println("CMPP.CMPPE_ACTIVE_RESP:active resp: STAT " + cr.stat);
break;
default:
break;
}
}catch(Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
System.out.println("have a exception");
try{
System.in.read();
}catch(Exception e1){}
}
}
public static void main(String[] args)
{
if(args[0] == null || args[0].equals("") || args[0].length() != 11){
System.out.println("手机号码输入不正确");
return;
}
if(args[1] == null || args[0].equals("") || args[1].length() == 0){
System.out.println("没有输入短信内容");
return;
}
CMPP p = new CMPP();
Test t = new Test();
char test[] = new char[10];
byte short_msg[] = new byte[150];
byte buf[] = new byte[40];
byte b1[] = new byte[10];
cmppe_submit sub = new cmppe_submit();
cmppe_submit_result res =new cmppe_submit_result();
cmppe_cancel can = new cmppe_cancel();
byte icp_id[] = new byte[10]; //ICP隐藏
icp_id[0] =0x**;
icp_id[1] =0x**;
icp_id[2] =0x**;
icp_id[3] =0x**;
icp_id[4] =0x**;
icp_id[5] =0x**;
icp_id[6] = 0;
byte svc_type[] = new byte[6];
svc_type[0] = 0x39;
svc_type[1] = 0x34;
svc_type[2] = 0x31;
svc_type[3] = 0x30;
svc_type[4] = 0;
byte fee_type = 0;
byte info_fee = 1;
byte proto_id = 1;
byte msg_mode = 1;
byte priority =9;
byte fee_utype = 2;
byte fee_user[] = new byte[CMPP.CMPPE_MAX_MSISDN_LEN];
for(int i=0; i<12; i++)
fee_user[0] = 0x31;
fee_user[1] = 0x33;
fee_user[2] = 0x38;
fee_user[3] = 0x30;
fee_user[4] = 0x35;
fee_user[5] = 0x30;
fee_user[6] = 0x30;
fee_user[7] = 0x31;
fee_user[8] = 0x30;
fee_user[9] = 0x36;
fee_user[10]= 0x34;
fee_user[11]= 0x0;
byte validate[] = new byte[10];
validate[0] =0;
byte schedule[] = new byte[2];
schedule[0] =0;
byte src_addr[] = new byte[12];
src_addr[0] = 0x31;
src_addr[1] = 0x33;
src_addr[2] = 0x38;
src_addr[3] = 0x30;
src_addr[4] = 0x35;
src_addr[5] = 0x30;
src_addr[6] = 0x30;
src_addr[7] = 0x31;
src_addr[8] = 0x30;
src_addr[9] = 0x36;
src_addr[10]= 0x34;
src_addr[11]= 0x0;
byte du_count = 1;
byte dst_addr[][] = new byte[10][15];
dst_addr[0][0] = toHex(args[0].charAt(0)); //CMPPE_MAX_MSISDN_LEN+1
dst_addr[0][1] = toHex(args[0].charAt(1));
dst_addr[0][2] = toHex(args[0].charAt(2));
dst_addr[0][3] = toHex(args[0].charAt(3));
dst_addr[0][4] = toHex(args[0].charAt(4));
dst_addr[0][5] = toHex(args[0].charAt(5));
dst_addr[0][6] = toHex(args[0].charAt(6));
dst_addr[0][7] = toHex(args[0].charAt(7));
dst_addr[0][8] = toHex(args[0].charAt(8));
dst_addr[0][9] = toHex(args[0].charAt(9));
dst_addr[0][10]= toHex(args[0].charAt(10));
dst_addr[0][11]= 0x0;
//byte data_coding = 1;
byte data_coding = 15; //15为GB2312
byte sm_len = 120;
String ss = args[1];
try{
short_msg = ss.getBytes("GB2312");
ss = new String(short_msg);
}catch(Exception e){
}
System.out.println(short_msg.length);
cmppe_result cr = new cmppe_result();
cmppe_deliver_result rs = new cmppe_deliver_result();
try{
conn_desc con = new conn_desc();
System.out.println("connecting...");
p.cmpp_connect_to_ismg("111.111.111.111",7890,con); //登
陆信息隐藏,SORRY哈
System.out.println("ICP_ID:" + "******" + " ICP_AUTH:" + "**");
p.cmpp_login(con,"*****","****",(byte)2,0x12,*******);
t.readPaME(con);
sub.set_icpid(icp_id );
sub.set_svctype(svc_type);
sub.set_feetype(fee_type );
sub.set_infofee(info_fee);
sub.set_protoid(proto_id);
sub.set_msgmode(msg_mode );
sub.set_priority(priority );
sub.set_validate(validate);
sub.set_schedule(schedule );
sub.set_feeutype(fee_utype);
sub.set_feeuser(fee_user);
sub.set_srcaddr(src_addr );
sub.set_dstaddr(dst_addr );
sub.set_ducount(du_count);
sub.set_msg(data_coding,short_msg.length,short_msg );
p.cmpp_submit(con,sub);
t.readPaME(con);
p.cmpp_active_test(con);
t.readPaME(con);
p.cmpp_logout(con);
t.readPaME(con);
}catch(Exception e)
{
System.out.println(e.getMessage());
e.printStackTrace();
System.out.println("have a exception");
try{
System.in.read();
}catch(Exception e1)
{
}
return;
}
}
public static byte toHex(char c){
switch(c){
case ?':
return 0x31;
case ?':
return 0x32;
case ?':
return 0x33;
case ?':
return 0x34;
case ?':
return 0x35;
case ?':
return 0x36;
case ?':
return 0x37;
case ?':
return 0x38;
case ?':
return 0x39;
case ?':
return 0x30;
default: break;
}
return 0x0;
}
}
如果您运行该程序得到该结果,就说明正常。
7
connecting...
ICP_ID:111111 ICP_AUTH:1111
login_type:2
CMPP_VERSION:18
cr.pack_id=-2147483647
CMPP.CMPPE_LOGIN_RESP:login resp: STAT = 0
cr.pack_id=5
CMPP.CMPPE_DELIVER:deliver: STAT = 0
cr.pack_id=-2147483644
CMPP.CMPPE_SUBMIT_RESP
cr.pack_id=-2147483640
CMPP.CMPPE_ACTIVE_RESP:active resp: STAT 0
主要看stat是0就行了,其它的是我的调试信息,首行7表示短信长度