当前位置: 移动技术网 > IT编程>开发语言>Java > java实现基于SGIP协议开发联通短信的方法

java实现基于SGIP协议开发联通短信的方法

2019年07月22日  | 移动技术网IT编程  | 我要评论

本文实例讲述了java实现基于sgip协议开发联通短信的方法。分享给大家供大家参考。具体如下:

近段时间,由于公司的业务需要,开发出了联通短信.此文章的编写也是根据网上的一些示例来完成的.闲话少说,下面来看代码:(运行此程序的时候需要导入华为的开发包,此包可以到网上下载)

下行:

public class mt {
 private static string spnumber = "**********";
 //接入号码 
 private static string chargenumber = "000000000000000000000";
 // 计费号码,白名单 
 private static string servicetype = "";
 //服务类型 
 private static string host = "***.***.***.***";
 // 主机名,网关ip 
 private static int port = ****;
 // 端口号 
 private static string corpid = "*****";
 // 企业代码 
 private static string login_name = "**";
 // 登陆名 
 private static string login_password = "***";
 // 登陆密码 
  public static void main(string[] args) throws unsupportedencodingexception {
  int srcnode = new biginteger("*******").intvalue();
  //源节点编号(nodeid),这一步非常重要,华为包中,该字段类型为int,而接入协议中要求在企业代码前加上30000,这样就超过了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", "false");
   // 连接登陆
   sgipsmproxy sgipsmp = new sgipsmproxy(argstr);
   // 这里 连接到短信息网关
   try {
    //connect表示向smg登陆,登录名与密码分别是smg向sp分配的用户名与密码,调用这个接口方法,向smg发送bind命令消息。
    //如果发送消息超时或通信异常则抛出异常,需要调用者捕获处理。
    boolean reslut = sgipsmp.connect(login_name, login_password);
    // 登陆得到true和false
    if (reslut) {
     system.out.println("连接成功...........");
    } else {
     system.out.println("连接失败(用户名或密码错误)...........");
     return;
    }
   } catch (exception ex) {
    system.out.println("网络异常...........");
    ex.printstacktrace();
    return;
   }
   string[] usernumber = { "8618686619970","8618686619977"};
   //接收短信的手机号码,前边要加上86
   string content = "联通短信发送成功";
   byte[] messagecontent = content.getbytes("gb2312");
    system.out.println("短信内容: "+content);
   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, // 短消息的内容 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) {
    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();
  }
}

上行:

public class mo extends sgipsmproxy {
 //smg服务器信息
 private static string serhost = "***.***.***.***";
 private static int serviceport = ****;
 //本机信息
 private static string localhost = "***.***.***.***";
 private static int localport = ****; 
 public mo(args args) {
  super(args);
  system.out.println("进入启动监听........");
  startservice(localhost, localport);
  //我想知道这里传递的host和port是本地的还是那的
 }
 public static void main(string[] args)
 {
  args argstr = new args();
  argstr.set("serhost", serhost);
  argstr.set("serviceport", serviceport);
  argstr.set("localhost", localhost);
  argstr.set("localport", localport);
  argstr.set("transaction-timeout", 10);
  // 操作超时时间(单位:秒)
  argstr.set("read-timeout", 15);
  // 物理连接读操作超时时间(单位:秒)
  //这里的安全认证问题如何解决?
  mo mymo=new mo(argstr);  
 }
 public sgipmessage ondeliver(sgipdelivermessage msg) {
  processrecvdelivermsg(msg);
  system.out.println("正在等待接收.......");
  return super.ondeliver(msg);
 }
 public void processrecvdelivermsg(sgipmessage msg) {
  if (msg instanceof sgipsubmitrepmessage) {
  //是否是类的实例
   system.out.println("返回下发短信的相应消息");
  }
  if (msg instanceof sgipdelivermessage) {
   // 收到用户发送的短信(上行)
   sgipdelivermessage delivermsg = (sgipdelivermessage) msg;
   string usernumber = delivermsg.getusernumber();
   // 手机号码
   string msgcontent = delivermsg.tostring();
   // 短信内容
   // byte[] msgid = delivermsg.getmsgcontent();
   system.out.println("usernumber::::::" + delivermsg.getusernumber());
   system.out.println("msgcontent:::::::" + delivermsg.tostring());
   system.out.println("spnumber::::::::" + delivermsg.getspnumber());
   //log.info("收到消息:" + delivermsg);
   system.out.println("收到消息 :"+delivermsg);
   int commandid = delivermsg.getcommandid();
   // 响应类型
   system.out.println("commandid:::::::::" + commandid);
   if (commandid == 0) { //上传短信(接收)
    system.out.println("dstaddr::::::" + delivermsg.getspnumber());
    try {
    } catch (exception e) {
     // todo auto-generated catch block
     e.printstacktrace();
    }
   }
  }
 }
}

希望本文所述对大家的java程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网