Java语言实现的短信接入实例,各公司大同小异

Nafisa ·
更新时间:2024-11-13
· 708 次阅读

  和几家短信平台接触过,都进行了接入测试。总体来说短信发送又快,覆盖率又全的,价格相对贵些。简易选两家分开使用,短信验证码的用一家贵的快的,普通的推广群发短信用一个便宜的。   下面显示下测试代码 1 public class HttpSMSSender { 2 3     protected final Logger logger = LoggerFactory.getLogger(getClass()); 4 5     // 短信接口地址 6     private String postUrl = "http://ip:port/webservice.asmx"; 7     @Autowired 8     private AppVars vars; 9 10 11     private String sname = "username"; // 提交账户 12     private String spwd = "password"; // 提交账户的密码 13 14 15 16     private String spnumber = ""; // 企业代码(扩展号,可空着) 17     private String key = "【某某公司】"; 18 19     @Deprecated 20     String sdst; // 接收号码,每次只能提交1个号码 21     @Deprecated 22     String smsg; // 信息内容,通常为70汉字以内,具体由平台内部决定 23 24     /** 25      * @param phoneNum 26      *            手机号码 27      * @param msg 28      *            短信内容 29      * @return 成功返回0 30      */ 31     public static void main(String[] args) { 32         HttpSMSSender sms = new HttpSMSSender(); 33         // String msg = "您正通过" + vars2.websiteNameCn 34         // + "提现至银行卡(尾号0908),金额90元,请确认无误后方可输入短信验证码:289002。如非本人操作,请拨打客服电话" 35         // + vars2.servicePhone; 36         String msg = "您好!用户哈哈,您好!您" + "平台的手机绑定验证码为:989887,请确认。"; 37         sms.send("186****5286", msg); 38     } 39 40     public int send(String phoneNum, String msg) { 41         StringBuilder postData = new StringBuilder(); 42 43         try { 44 45             // sb 46             String pwdd = MD5.EncoderByMd5(sname + spwd); 47             postData.append("sn").append("=").append(sname).append("&"); 48             postData.append("pwd").append("=").append(pwdd).append("&"); 49             postData.append("Ext").append("=").append("").append("&"); 50             postData.append("stime").append("=").append("").append("&"); 51             postData.append("mobile").append("=").append(phoneNum).append("&"); 52             postData.append("Rrid").append("=").append("").append("&"); 53             postData.append("Content").append("=") 54                     .append(java.net.URLEncoder.encode(msg + key, "GBK")); 55             return send(postData.toString()); 56         } catch (Exception x) { 57             logger.error("发送短信时发生异常", x); 58             return -1; 59         } 60     } 61 62     private int send(String postData) throws UnsupportedEncodingException, 63             IOException, ParserConfigurationException, SAXException { 64         logger.info("请求短信接口内容[{}]", postData); 65         HttpURLConnection conn = null; 66         try { 67             final URL url = new URL(postUrl + "?" + postData); 68             conn = (HttpURLConnection) url.openConnection(); 69             conn.setRequestMethod("GET"); 70             conn.connect(); 71 72 73             // 获取响应状态 74             if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) { 75                 logger.error("连接短信服务器失败"); 76                 return -1; 77             } 78 79             InputStream is = conn.getInputStream(); 80             ByteArrayOutputStream baos = new ByteArrayOutputStream(); 81             int i = -1; 82             while ((i = is.read()) != -1) { 83                 baos.write(i); 84             } 85             logger.info("请求短信接口返回内容[{}]", baos.toString()); 86             int resultCode = -1; 87 88             try { 89                 resultCode = 0; 90 91             } catch (Exception e) { 92                 resultCode = -1; 93                 // TODO: handle exception 94             } 95 96             return resultCode; 97         } finally { 98             if (conn != null) { 99                 conn.disconnect(); 100             } 101         } 102     } 103 104     private int parseResultCode(InputStream is) 105             throws ParserConfigurationException, SAXException, IOException { 106         final Map<String, String> map = new HashMap<String, String>(); 107         SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); 108         saxParser.parse(is, new DefaultHandler() { 109 110             private String tag = ""; 111 112             public void endElement(String uri, String localName, String qName) 113                     throws SAXException { 114                 tag = ""; 115             } 116 117             public void startElement(String uri, String localName, 118                     String qName, Attributes attributes) throws SAXException { 119                 tag = qName; 120             } 121 122             public void characters(char[] ch, int start, int length) 123                     throws SAXException { 124                 if (!tag.isEmpty()) { 125                     map.put(tag, new String(ch, start, length)); 126                 } 127             } 128 129         }); 130         logger.info("短信接口返回码[{}]描述[{}]", map.get("State"), map.get("MsgState")); 131         return Integer.valueOf(map.get("State")); 132     } 133 134     public String getPostUrl() { 135         return postUrl; 136     } 137 138     public String getSname() { 139         return sname; 140     } 141 142     public String getSpwd() { 143         return spwd; 144     } 145 146     public void setPostUrl(String postUrl) { 147         this.postUrl = postUrl; 148     } 149 150     public void setSname(String sname) { 151         this.sname = sname; 152     } 153 154     public void setSpwd(String spwd) { 155         this.spwd = spwd; 156     } 157 }



java语言 JAVA

需要 登录 后方可回复, 如果你还没有账号请 注册新账号