基于SGIP协议的短信网关接口

转载自:http://www.oschina.net/bbs/thread/13041

使用:华为短信网关协议开发包(unix+windows) SMProxy合并协议(Java版本) - smproxy.jar



示例代码如下:

    public class Send {

        private static String SPNumber = "xxxxxxxxx";//接入号码

        private static String ChargeNumber = "86xxxxxxxxxxx"; //计费号码

        private static String ServiceType = "xxxxxx";//服务类型

        private static String host = "xx.xxx.xxx.xxx"; //主机名,网关IP

        private static int port = 8801; // 网关端口号

        private static String CorpId = "xxxxx"; // 企业代码

        private static String login_Name = "xxxxxxx"; // 登陆名

        private static String login_PassWord = "xxxxxxx"; // 登陆密码

    

        public static void main(String[] args) throws UnsupportedEncodingException {

            int srcnode =new BigInteger("3xxxxx").intValue(); //源节点编号,华为包中该字段类型为int,而接入协议中要求在企业代码前加上30000,这样就超过了int的取值范围,所以需要用BigInteger转一下

            Args argstr = new Args();

            argstr.set("host", host);

            argstr.set("port", port);

            argstr.set("local-host", "127.0.0.1");//本机IP地址

            argstr.set("local-port", 4560);//固定本机端口号

            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");

            SGIPSMProxy sgipsmp = new SGIPSMProxy(argstr);

            try {

                boolean reslut = sgipsmp.connect(login_Name, login_PassWord); //登陆

                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 = {"86xxxxxxxxxxx"};//接收短信的手机号码,前边要加上86

            String content = "短信测试内容";

            byte[] MessageContent = content.getBytes("GB2312");

            try {

                // 下发短息

                SGIPSubmitMessage sgipsubmit = new SGIPSubmitMessage(

                        SPNumber, // SP的接入号码

                        ChargeNumber, // 付费号码 string

                        UserNumber, // 接收该短消息的手机号,最多100个号码 string[]

                        CorpId, // 企业代码,取值范围为0~99999 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, // 短消息的内容 byte[]

                        "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) {

                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();

        }

    }

你可能感兴趣的:(java)