SmsUtils 发送短信

  1 import java.io.InputStream;
  2 import java.io.OutputStream;
  3 import java.io.OutputStreamWriter;
  4 import java.net.URL;
  5 import java.net.URLConnection;
  6 import javax.xml.parsers.DocumentBuilder;
  7 import javax.xml.parsers.DocumentBuilderFactory;
  8 import org.w3c.dom.Document;
  9 import org.w3c.dom.Node;
 10 import org.w3c.dom.NodeList;
 11 
 12 /**
 13  * 调用吉信通 发短信工具类
 14  */
 15 public class SmsUtils {
 16     /**
 17      * 调用 WebService 协议方式发送短信
 18      * 
 19      * @param mobiles
 20      * @param msg
 21      * @return
 22      */
 23     public static String sendSmsByWebService(String mobiles, String msg) {
 24         String result = "-12";
 25         try {
 26             Document doc;
 27             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 28             dbf.setNamespaceAware(true);
 29             DocumentBuilder db = dbf.newDocumentBuilder();
 30             
 31             String macAddress = MacUtil.MAC.getMacAddress();
 32             String macEnc = MD5Utils.md5(macAddress);
 33             String num1 = macEnc.substring(4,5);
 34             String num2 = macEnc.substring(27,28);
 35             int num3 = Integer.valueOf(num1) + Integer.valueOf(num2);
 36             String usernameF = macEnc.substring(0, num3);
 37             String passwordF = macEnc.substring(num3, macEnc.length());
 38             
 39             DESUtil desUtil = new DESUtil();
 40             
 41             String username = desUtil.decrypt(userid.replaceAll(usernameF, ""));
 42             
 43             String password = desUtil.decrypt(pass.replaceAll(passwordF, ""));
 44             
 45             InputStream is = getSoapInputStream(username, password, mobiles, msg, "");
 46             if (is != null) {
 47                 doc = db.parse(is);
 48                 NodeList nl = doc.getElementsByTagName("SendMessagesResult");
 49                 Node n = nl.item(0);
 50                 result = n.getFirstChild().getNodeValue();
 51                 is.close();
 52             }
 53             return result;
 54         } catch (Exception e) {
 55             System.out.print("SmsSoap.sendSms error:" + e.getMessage());
 56             return "-12";
 57         }
 58     }
 59 
 60     private static String getSoapSmssend(String userid, String pass,
 61             String mobiles, String msg, String time) {
 62         try {
 63             String soap = "";
 64             soap = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
 65                     + "<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/\">"
 66                     + "<soap:Body>"
 67                     + "<SendMessages xmlns=\"http://tempuri.org/\">" + "<uid>"
 68                     + userid + "</uid>" + "<pwd>" + pass + "</pwd>" + "<tos>"
 69                     + mobiles + "</tos>" + "<msg>" + msg + "</msg>" + "<otime>"
 70                     + time + "</otime>" + "</SendMessages>" + "</soap:Body>"
 71                     + "</soap:Envelope>";
 72             return soap;
 73         } catch (Exception ex) {
 74             ex.printStackTrace();
 75             return null;
 76         }
 77     }
 78 
 79     private static InputStream getSoapInputStream(String userid, String pass,
 80             String mobiles, String msg, String time) throws Exception {
 81         URLConnection conn = null;
 82         InputStream is = null;
 83         try {
 84             String soap = getSoapSmssend(userid, pass, mobiles, msg, time);
 85             if (soap == null) {
 86                 return null;
 87             }
 88             try {
 89 
 90                 URL url = new URL("http://service2.winic.org:8003/Service.asmx");
 91 
 92                 conn = url.openConnection();
 93                 conn.setUseCaches(false);
 94                 conn.setDoInput(true);
 95                 conn.setDoOutput(true);
 96                 conn.setRequestProperty("Content-Length",
 97                         Integer.toString(soap.length()));
 98                 conn.setRequestProperty("Content-Type",
 99                         "text/xml; charset=utf-8");
100                 conn.setRequestProperty("HOST", "service2.winic.org");
101                 conn.setRequestProperty("SOAPAction",
102                         "\"http://tempuri.org/SendMessages\"");
103 
104                 OutputStream os = conn.getOutputStream();
105                 OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");
106                 osw.write(soap);
107                 osw.flush();
108             } catch (Exception ex) {
109                 System.out.print("SmsSoap.openUrl error:" + ex.getMessage());
110             }
111             try {
112                 is = conn.getInputStream();
113             } catch (Exception ex1) {
114                 System.out.print("SmsSoap.getUrl error:" + ex1.getMessage());
115             }
116 
117             return is;
118         } catch (Exception e) {
119             System.out.print("SmsSoap.InputStream error:" + e.getMessage());
120             return null;
121         }
122     }
123     private static String userid = "7d39bcb411cc4726b004051c3970022";
124     private static String pass = "2da0c4b48817eda12e07c2eaf87c24cdcbaff0b22958aeb28";
125 }

 

posted @ 2017-12-05 22:33  龙芳伟  阅读(515)  评论(0编辑  收藏  举报