zoukankan      html  css  js  c++  java
  • 定时发送短信总结

    package com;

    import java.io.*;
    import java.net.*;
    import java.security.*;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    public class Client {

    /*
    * webservice服务器定义
    */

    private String serviceURL = "http://sdk.entinfo.cn:8061/webservice.asmx";
    private String sn = "";// 序列号
    private String password = "";
    private String pwd = "";// 密码

    /*
    * 构造函数
    */
    public Client(String sn, String password)
    throws UnsupportedEncodingException {
    this.sn = sn;
    this.password = password;
    //密码为md5(sn+password)
    this.pwd = this.getMD5(sn + password);
    }

    /*
    * 方法名称:getMD5
    * 功 能:字符串MD5加密
    * 参 数:待转换字符串
    * 返 回 值:加密之后字符串
    */
    public String getMD5(String sourceStr) throws UnsupportedEncodingException {
    String resultStr = "";
    try {
    byte[] temp = sourceStr.getBytes();
    MessageDigest md5 = MessageDigest.getInstance("MD5");
    md5.update(temp);
    // resultStr = new String(md5.digest());
    byte[] b = md5.digest();
    for (int i = 0; i < b.length; i++) {
    char[] digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
    '9', 'A', 'B', 'C', 'D', 'E', 'F' };
    char[] ob = new char[2];
    ob[0] = digit[(b[i] >>> 4) & 0X0F];
    ob[1] = digit[b[i] & 0X0F];
    resultStr += new String(ob);
    }
    return resultStr;
    } catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
    return null;
    }
    }


    /*
    * 方法名称:mdgetSninfo
    * 功 能:获取信息
    * 参 数:sn,pwd(软件序列号,加密密码md5(sn+password))
    *
    */
    public String mdgetSninfo() {
    String result = "";
    String soapAction = "http://entinfo.cn/mdgetSninfo";
    String xml = "<?xml version="1.0" encoding="utf-8"?>";
    xml += "<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">";
    xml += "<soap:Body>";
    xml += "<mdgetSninfo xmlns="http://entinfo.cn/">";
    xml += "<sn>" + sn + "</sn>";
    xml += "<pwd>" + pwd + "</pwd>";
    xml += "</mdgetSninfo>";
    xml += "</soap:Body>";
    xml += "</soap:Envelope>";

    URL url;
    try {
    url = new URL(serviceURL);

    URLConnection connection = url.openConnection();
    HttpURLConnection httpconn = (HttpURLConnection) connection;
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    bout.write(xml.getBytes());
    byte[] b = bout.toByteArray();
    httpconn.setRequestProperty("Content-Length", String
    .valueOf(b.length));
    httpconn.setRequestProperty("Content-Type",
    "text/xml; charset=gb2312");
    httpconn.setRequestProperty("SOAPAction", soapAction);
    httpconn.setRequestMethod("POST");
    httpconn.setDoInput(true);
    httpconn.setDoOutput(true);

    OutputStream out = httpconn.getOutputStream();
    out.write(b);
    out.close();

    InputStreamReader isr = new InputStreamReader(httpconn
    .getInputStream());
    BufferedReader in = new BufferedReader(isr);
    String inputLine;
    while (null != (inputLine = in.readLine())) {
    Pattern pattern = Pattern.compile("<mdgetSninfoResult>(.*)</mdgetSninfoResult>");
    Matcher matcher = pattern.matcher(inputLine);
    while (matcher.find()) {
    result = matcher.group(1);
    }
    }
    return result;
    } catch (Exception e) {
    e.printStackTrace();
    return "";
    }
    }


    /*
    * 方法名称:mdgxsend
    * 功 能:发送个性短信
    * 参 数:mobile,content,ext,stime,rrid,msgfmt(手机号,内容,扩展码,定时时间,唯一标识,内容编码)
    * 返 回 值:唯一标识,如果不填写rrid将返回系统生成的
    */
    public String mdgxsend(String mobile, String content, String ext, String stime,
    String rrid, String msgfmt) {
    String result = "";
    String soapAction = "http://entinfo.cn/mdgxsend";
    String xml = "<?xml version="1.0" encoding="utf-8"?>";
    xml += "<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">";
    xml += "<soap:Body>";
    xml += "<mdgxsend xmlns="http://entinfo.cn/">";
    xml += "<sn>" + sn + "</sn>";
    xml += "<pwd>" + pwd + "</pwd>";
    xml += "<mobile>" + mobile + "</mobile>";
    xml += "<content>" + content + "</content>";
    xml += "<ext>" + ext + "</ext>";
    xml += "<stime>" + stime + "</stime>";
    xml += "<rrid>" + rrid + "</rrid>";
    xml += "<msgfmt>" + msgfmt + "</msgfmt>";
    xml += "</mdgxsend>";
    xml += "</soap:Body>";
    xml += "</soap:Envelope>";

    URL url;
    try {
    url = new URL(serviceURL);

    URLConnection connection = url.openConnection();
    HttpURLConnection httpconn = (HttpURLConnection) connection;
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    bout.write(xml.getBytes());
    byte[] b = bout.toByteArray();
    httpconn.setRequestProperty("Content-Length", String
    .valueOf(b.length));
    httpconn.setRequestProperty("Content-Type",
    "text/xml; charset=gb2312");
    httpconn.setRequestProperty("SOAPAction", soapAction);
    httpconn.setRequestMethod("POST");
    httpconn.setDoInput(true);
    httpconn.setDoOutput(true);

    OutputStream out = httpconn.getOutputStream();
    out.write(b);
    out.close();

    InputStreamReader isr = new InputStreamReader(httpconn
    .getInputStream());
    BufferedReader in = new BufferedReader(isr);
    String inputLine;
    while (null != (inputLine = in.readLine())) {
    Pattern pattern = Pattern.compile("<mdgxsendResult>(.*)</mdgxsendResult>");
    Matcher matcher = pattern.matcher(inputLine);
    while (matcher.find()) {
    result = matcher.group(1);
    }
    }
    return result;
    } catch (Exception e) {
    e.printStackTrace();
    return "";
    }
    }


    /*
    * 方法名称:mdsmssend
    * 功 能:发送短信
    * 参 数:mobile,content,ext,stime,rrid,msgfmt(手机号,内容,扩展码,定时时间,唯一标识,内容编码)
    * 返 回 值:唯一标识,如果不填写rrid将返回系统生成的
    */
    public String mdsmssend(String mobile, String content, String ext, String stime,
    String rrid,String msgfmt) {
    String result = "";
    String soapAction = "http://entinfo.cn/mdsmssend";
    String xml = "<?xml version="1.0" encoding="utf-8"?>";
    xml += "<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">";
    xml += "<soap:Body>";
    xml += "<mdsmssend xmlns="http://entinfo.cn/">";
    xml += "<sn>" + sn + "</sn>";
    xml += "<pwd>" + pwd + "</pwd>";
    xml += "<mobile>" + mobile + "</mobile>";
    xml += "<content>" + content + "</content>";
    xml += "<ext>" + ext + "</ext>";
    xml += "<stime>" + stime + "</stime>";
    xml += "<rrid>" + rrid + "</rrid>";
    xml += "<msgfmt>" + msgfmt + "</msgfmt>";
    xml += "</mdsmssend>";
    xml += "</soap:Body>";
    xml += "</soap:Envelope>";

    URL url;
    try {
    url = new URL(serviceURL);

    URLConnection connection = url.openConnection();
    HttpURLConnection httpconn = (HttpURLConnection) connection;
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    bout.write(xml.getBytes());
    byte[] b = bout.toByteArray();
    httpconn.setRequestProperty("Content-Length", String
    .valueOf(b.length));
    httpconn.setRequestProperty("Content-Type",
    "text/xml; charset=gb2312");
    httpconn.setRequestProperty("SOAPAction", soapAction);
    httpconn.setRequestMethod("POST");
    httpconn.setDoInput(true);
    httpconn.setDoOutput(true);

    OutputStream out = httpconn.getOutputStream();
    out.write(b);
    out.close();

    InputStreamReader isr = new InputStreamReader(httpconn
    .getInputStream());
    BufferedReader in = new BufferedReader(isr);
    String inputLine;
    while (null != (inputLine = in.readLine())) {
    Pattern pattern = Pattern.compile("<mdsmssendResult>(.*)</mdsmssendResult>");
    Matcher matcher = pattern.matcher(inputLine);
    while (matcher.find()) {
    result = matcher.group(1);
    }
    }
    return result;
    } catch (Exception e) {
    e.printStackTrace();
    return "";
    }
    }

    }

    2.测试

    package com;

    import java.io.UnsupportedEncodingException;
    import java.net.*;

    public class Demo_Client {


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

    String sn="SDK-";
    String pwd="000000";
    Client client=new Client(sn,pwd);

    //短信发送
    String content=URLEncoder.encode("测试788", "utf8");
    String result_mt = client.mdsmssend("13800138", content, "", "", "", "");
    System.out.print(result_mt);
    //个性短信发送
    // String result_gxmt = client.mdgxsend("13800138", "测试", "", "", "", "");
    // System.out.print(result_gxmt);

    }
    }

  • 相关阅读:
    MVC部署到IIS 出现未能加载文件或程序集“System.Web.Http.WebHost.....
    web在线聊天框滚动条自动在底部
    IE 浏览器下英文 微软雅黑 不起作用
    U3D MonoBehaviour.InvokeRepeating 和 MonoBehaviour.Invoke
    U3D 中关于相同的怪物不发生碰撞 或者是想让一些物体发生碰撞 又不想让一些物体发生碰撞
    U3D 2D中给精灵添加刚体后 发现精灵会倒 ..
    Axure教程 | 轻量级的后台原型框架
    Axure:侧导航收缩与展开
    SQLSERVER数据库调优
    MySQL用GROUP BY分组取字段最大值或最新一条
  • 原文地址:https://www.cnblogs.com/airycode/p/5294638.html
Copyright © 2011-2022 走看看