基于SGIP协议编写短信网关接口
通过不断的调试和沟通,短信功能终于好使了
为了让更多的人少走弯路,特将编写方法和注意事项记录下来,希望能够对大家有帮助;
短信网关的申请流程(此部分工作我们有专门的同事去搞定,我仅略知一二,仅供参考);
向宽带公司(我们用的是联通在信平台)提交申请,然后一顿审核,
审核通过后,宽带公司在网关做数据(分配接入号、公司代码、绑定ip等)
做完数据后会通知接口人,并要求测试
基于SGIP协议的短信接口开发:
我是在华为的短信开发包的基础上开发的,由于不知道该包是否涉及版权问题,所以本人暂不提供了,可以到网上自行解决;
下载后就是一个jar包
短信发送的代码如下:
public class Send { // private static Logger log = LogManager.getLogger("send"); private static String SPNumber = "xxxx";//接入号码 private static String ChargeNumber = "000000000000000000000"; // 计费号码,我们是白名单 private static String ServiceType = "xxxxx";//服务类型 private static String host = "xxx.xxx.xxx.xxx"; // 主机名,网关IP private static int port = 8881; // 端口号,这里特别注意下,接入协议中写的是8804,害得我调了很久,后来才知道改了,所以,这个在接入前,建议与网关人员确定 private static String CorpId = "xxxxx"; // 企业代码 private static String login_Name = "xxxxxxxx"; // 登陆名 private static String login_PassWord = "xxxxxxx"; // 登陆密码 public static void main(String[] args) throws UnsupportedEncodingException { int srcnode =new BigInteger("+企业代码").intValue(); //源节点编号,这一步非常重要,华为包中,该字段类型为int,而接入协议中要求在企业代码前加上,这样就超过了int的取值范围,所以需要用BigInteger转一下就可以了 Args argstr = new Args(); argstr.set("host", host); argstr.set("port", port); argstr.set("transaction-timeout", 10); // 操作超时时间(单位:秒) argstr.set("read-timeout", 15); // 物理连接读操作超时时间(单位:秒) argstr.set("source-addr", srcnode); // SP…ID(最大为六位字符) argstr.set("login-name", login_Name); argstr.set("login-pass", login_PassWord); argstr.set("debug", "true"); // 连接登陆zhuxiao5 SGIPSMProxy sgipsmp = new SGIPSMProxy(argstr); // 这里 try { boolean reslut = sgipsmp.connect(login_Name, login_PassWord); // 登陆得到true和false System.out.println("reslut:::::::" + reslut); if (reslut) { System.out.println("连接成功..........."); } else { System.out.println("连接失败(用户名或密码错误)..........."); return; } } catch (Exception ex) { System.out.println("网络异常..........."); ex.printStackTrace(); return; } String[] UserNumber = { "xxxxxxxx","xxxxxxxx"};//接收短信的手机号码,前边要加上86 String content = "短信测试内容"; byte[] MessageContent = content.getBytes("GB2312"); try { // 下发短息 SGIPSubmitMessage sgipsubmit = new SGIPSubmitMessage( SPNumber, // SP的接入号码 ChargeNumber, // 付费号码 string UserNumber, // 接收该短消息的手机号,最多100个号码 string[] CorpId, // 企业代码,取值范围为0~ string ServiceType, // 业务代码,由SP定义 stirng 03, // 计费类型 int "0", // 该条短消息的收费值 stirng "0", // 赠送用户的话费 string 0, // 代收费标志0:应收1:实收 int 0, // 引起MT消息的原因 int 06, // 优先级0~9从低 到高,默认为0 int null, // 短消息寿命的终止时间 date null, // 短消息定时发送的时间 date 1, // 状态报告标记 int 0, // GSM协议类型 int 0, // GSM协议类型 int 15, // 短消息的编码格式 int 0, // 信息类型 int MessageContent.length, // 短消息内容长度 int MessageContent, // 短消息的内容 btye[] "0" // 保留,扩展用 string ); // 收到的响应消息转换成rep int status = ProcessSubmitRep(sgipsmp.send(sgipsubmit)); System.out.println(status); if (status == 0) { System.out.println("消息发送成功.........."); } else { System.out.println("消息发送失败.........."); } } catch (Exception ex) { // log.error("发送消息异常:"+ex.getMessage(),ex); ex.printStackTrace(); } } private static int ProcessSubmitRep(SGIPMessage msg) { // 收到的响应消息转换成repMsg SGIPSubmitRepMessage repMsg = (SGIPSubmitRepMessage) msg; System.out.println(repMsg.getSrcNodeId()); System.out.println("status:::::::" + repMsg.getResult()); if (repMsg != null && repMsg.getResult() == 0) { System.out.println("发送成功:::"); } return repMsg.getResult(); } }
