sendmttowapdm.java

来自「短信平台接口开发例子,java语言实现的。」· Java 代码 · 共 229 行

JAVA
229
字号
package business.mt;

import java.io.*;
import java.net.*;
import javax.xml.parsers.*;

import org.w3c.dom.*;
import org.xml.sax.*;
import business.common.*;
import sun.misc.*;

/**
 * <p>Title: 商户MT消息给短信平台</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004.8</p>
 * <p>Company: 掌上通</p>
 * @author hjj
 * @version 1.0
 */

public class SendMTtoWapdm {

  //成功发送
  private static int SEND_OK = 0;

  //目前短信业务平台定义的错误代码都是>0的整数在这里防止和平台定义冲突可以用负整数定义
  //连接错误
  private static int CONNECT_ERROR = -1000;
  //IO异常
  private static int IO_ERROR = -2000;
  //分析短信业务平台回应的错误
  private static int WAPDM_ERROR = -3000;

  public SendMTtoWapdm() {
  }


  public static void main(String[] args) {
    SendMTtoWapdm sendSMSToWapdm1 = new SendMTtoWapdm();
    //商户在这里测试MT消息
    sendSMSToWapdm1.SmbppSendUnicodeMessage("0","4084MFBZ4084M0000","","13552965392","13552965392","test send mess",0,0,0,0,0,0);
  }

  /**
   * 发送Unicode消息(此方法可以用jsp来调用或是其他方法来调用)
   * @param bstrMoMessageID      MO消息ID,如果发送的此条信息是商户接收到的MO消息的回应,
   *                             那么必须填写接收到的那条MO消息ID,否则填写空字符串。
   * @param bstrBusinessCode     业务代码
   * @param bstrLongCode         发送此信息使用的长号码
   * @param bstrFeeMsisdn        计费手机号码
   * @param bstrDesMsisdn        接收该信息的目的手机号码
   * @param bstrMessageContent   此信息的内容
   * @param lTpPid               GSM协议
   * @param lTpUdhi              GSM协议
   * @param lSendDate            定时发送的日期
   * @param lSendTime            定时发送的时间
   * @param lExpireDate          定时发送的过期日期
   * @param lExpireTime          定时发送的过期时间
   */
  public void SmbppSendUnicodeMessage(String bstrMoMessageID,String bstrBusinessCode,String bstrLongCode,
                                       String bstrFeeMsisdn,String bstrDesMsisdn,String bstrMessageContent,
                                       int lTpPid,int lTpUdhi,
                                       int lSendDate,int lSendTime,
                                       int lExpireDate,int lExpireTime){
           //传递给短信平台的参数
           String paramStr = "bstrMoMessageID=" + bstrMoMessageID +
               "&bstrBusinessCode=" + bstrBusinessCode +
               "&bstrLongCode=" + bstrLongCode + "&bstrFeeMsisdn=" + bstrFeeMsisdn +
               "&bstrDesMsisdn=" + bstrDesMsisdn + "&bstrMessageContent=" +
               bstrMessageContent +
               "&lTpPid=" + lTpPid + "&lTpUdhi=" + lTpUdhi +
               "&lSendDate=" + lSendDate + "&lSendTime=" + lSendTime +
               "&lExpireDate=" + lExpireDate + "&lExpireTime=" + lExpireTime;
           //发送UnicodeMess的地址
           String SendUnicodeMessURL = "http://211.157.104.166/SmbpHttpAgent/SmbpHttpAgent.asmx/SmbppSendUnicodeMessage";//"http://localhost:8080/wapdmToBusinessHTTP/RecvMTFromBusiness.jsp";//"http://211.157.104.166/SmbpHttpAgent/SmbpHttpAgent.asmx/SmbppSendUnicodeMessage";

           int Result = businessMTtoWapdm(SendUnicodeMessURL,paramStr);
           if(Result == this.SEND_OK){
             Tools.trace("send ok");
             //在这里商户可以对成功送往短信平台的消息做相应的处理
           }else if(Result == this.CONNECT_ERROR){
             Tools.trace("not connect");
             //在这里商户可以对未成功送往短信平台的消息做相应的处理
           }else if(Result == this.IO_ERROR){
             Tools.trace("IO Exception");
             //在这里商户可以对未成功送往短信平台的消息做相应的处理
           }else if(Result == this.WAPDM_ERROR){
             Tools.trace("wapdm error");
             //在这里商户可以对未成功送往短信平台的消息做相应的处理
           }else{
             Tools.trace("unknown error");
             //在这里商户可以对未成功送往短信平台的消息做相应的处理
           }
           //在这里也可以是做统一的处理
    }

    /**
     * 发送ASCII消息(此方法可以用jsp来调用或是其他方法来调用)
     * @param bstrMoMessageID
     * @param bstrBusinessCode
     * @param bstrLongCode
     * @param bstrFeeMsisdn
     * @param bstrDesMsisdn
     * @param bstrMessageContent
     * @param lTpPid
     * @param lTpUdhi
     * @param lSendDate
     * @param lSendTime
     * @param lExpireDate
     * @param lExpireTime
     */
    public void SmbppSendASCIIMessage(String bstrMoMessageID,String bstrBusinessCode,String bstrLongCode,
                                      String bstrFeeMsisdn,String bstrDesMsisdn,String bstrMessageContent,
                                      int lTpPid,int lTpUdhi,
                                      int lSendDate,int lSendTime,
                                      int lExpireDate,int lExpireTime ){
      //可以参考SmbppSendUnicodeMessage方法
      throw new java.lang.UnsupportedOperationException("not implement  ");

    }




    /**
     * 将MT消息POST给短信业务平台
     * @param destURL  目的地址
     * @param paramStr 传递的参数
     */
    private int businessMTtoWapdm(String destURL,String paramStr) {
      int Result = 0;
      HttpURLConnection httpConn = null;
      PrintWriter out = null;
      BufferedReader reader = null;
      try {
        //与短信平台建立一个连接
        URL url = new URL(destURL);
        //打开连接
        httpConn = (HttpURLConnection) url.openConnection();
        httpConn.setRequestMethod("POST");
        //商户登陆短信平台的用户名(商户在此填写掌上通分配的用户名)
        String userName = "";
        //商户登陆短信平台的密码(商户在此填写掌上通分配的用户密码)
        String passWord = "";
        String userPassword = userName + ":" + passWord;
        userPassword = new BASE64Encoder().encode(userPassword.getBytes());
        //设置商户身份验证
        httpConn.setRequestProperty("Authorization", "Basic " + userPassword);

        httpConn.setRequestProperty("content-type",
                                    "application/x-www-form-urlencoded");
        httpConn.setDoOutput(true);
        httpConn.setDoInput(true);
        out = new PrintWriter(httpConn.getOutputStream());
        out.print(paramStr);
        out.flush();
        httpConn.connect();
        reader = new BufferedReader(new InputStreamReader(httpConn.
            getInputStream()));

        StringBuffer buf = new StringBuffer();
        String line;
        while ( (line = reader.readLine()) != null) {
          buf.append(line);
        }
        ByteArrayInputStream bis = new ByteArrayInputStream(buf.toString().
            getBytes());

        //在这个地方调用分析短信业务平台的回应的方法
        Result = this.parseWAPDMResponse(bis);

      }
      catch (MalformedURLException ex) {
        Result = this.CONNECT_ERROR;//连接失败(此处错误属于在商户方产生的)
        Tools.error(ex);
      }
      catch (IOException ex) {
        Result = this.IO_ERROR;//IO异常(此处错误属于在商户方产生的)
        Tools.error(ex);
      }
      finally {
        Tools.closeBufferedReader(reader);
        Tools.closePrintWriter(out);
        Tools.closeHttpURLConnection(httpConn);
      }
      return Result;
    }

  /**
   * 分析短信平台回应
   * @param result
   */
  private int parseWAPDMResponse(InputStream in) {
    int Result=0;
    try {
      //获得一个XML文件的解析器
      DocumentBuilderFactory documentbuilderfactory = DocumentBuilderFactory.
          newInstance();
      //解析XML文件生成DOM文档的接口类,以便访问DOM
      DocumentBuilder documentbuilder = documentbuilderfactory.
          newDocumentBuilder();
      Document document = documentbuilder.parse(in);
      document.normalize();

      NodeList nodelist = document.getElementsByTagName("IMessageID");

      Element node = (Element) nodelist.item(0);

      //获取Result节点
      Result = Integer.parseInt(node.getElementsByTagName("Result").item(
          0).getFirstChild().getNodeValue());

      Tools.trace("result=" + Result);

    }catch (ParserConfigurationException e) {
      Result = this.WAPDM_ERROR;
      Tools.error(e);//此处错误属于分析短信业务平台的结果时产生的
    }catch (IOException e) {
      Result = this.WAPDM_ERROR;
      Tools.error(e);//此处错误属于分析短信业务平台的结果时产生的
    }catch (SAXException e) {
      Result = this.WAPDM_ERROR;
      Tools.error(e);//此处错误属于分析短信业务平台的结果时产生的
    }
    return Result;

  }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?