java或者C#发送短信
之前做餐饮的项目,要求结完帐或者充值的时候给手机端推送一条会员的短信,
只要调用下面代码中的sendSms方法就好了,
需注意的是:1.您需要找到短信的供应商得到用户名和密码
sendStr.append("username=").append("xxxxxx");
sendStr.append("&password=").append("xxxxx");
2.需要短信供应商提供给你服务器的端口(IP)
String serviceURL = "http://IP:8030/service/httpService/httpInterface.do?method=sendMsg";
1 package com.hqzn.services.common.impl; 2 3 import org.apache.commons.logging.Log; 4 import org.apache.commons.logging.LogFactory; 5 import org.springframework.stereotype.Service; 6 import java.io.BufferedReader; 7 import java.io.InputStreamReader; 8 import java.io.OutputStream; 9 import java.net.HttpURLConnection; 10 import java.net.URL; 11 import java.net.URLDecoder; 12 import java.util.List; 13 import java.util.regex.Matcher; 14 import java.util.regex.Pattern; 15 import com.hqzn.services.common.interfaces.ISendMessageSV; 16 17 @Service("ISendMessageSV") 18 public class SendMessageSVImpl implements ISendMessageSV { 19 private Log log=LogFactory.getLog(SendMessageSVImpl.class); 20 21 public static String connectURL(String commString,String sendsmsaddress) { 22 String rec_string = ""; 23 URL url = null; 24 HttpURLConnection urlConn = null; 25 try { 26 url = new URL(sendsmsaddress); //根据数据的发送地址构建URL 27 urlConn = (HttpURLConnection) url.openConnection(); //打开链接 28 urlConn.setConnectTimeout(30000); //链接超时设置为30秒 29 urlConn.setReadTimeout(30000); //读取超时设置30秒 30 urlConn.setRequestMethod("POST"); //链接相应方式为post 31 urlConn.setDoOutput(true); 32 urlConn.setDoInput(true); 33 34 OutputStream out = urlConn.getOutputStream(); 35 out.write(commString.getBytes("UTF-8")); 36 out.flush(); 37 out.close(); 38 39 BufferedReader rd = new BufferedReader(new InputStreamReader(urlConn.getInputStream(), "UTF-8")); 40 StringBuffer sb = new StringBuffer(); 41 int ch; 42 while ((ch = rd.read()) > -1) { 43 sb.append((char) ch); 44 } 45 46 rec_string = sb.toString().trim(); 47 rec_string = URLDecoder.decode(rec_string,"UTF-8"); 48 49 rd.close(); 50 } catch (Exception e) { 51 rec_string = "-107"; 52 } finally { 53 if (urlConn != null) { 54 urlConn.disconnect(); 55 } 56 } 57 58 return rec_string; 59 } 60 61 @Override 62 public String sendSms(String mobile,List<String> lstParams,String template) { 63 String resultok = ""; 64 try { 65 String res = ""; 66 String content=""; 67 StringBuilder sendStr=new StringBuilder(); 68 sendStr.append("username=").append("xxxxxx"); 69 sendStr.append("&password=").append("xxxxx"); 70 sendStr.append("&veryCode=").append("p2wmnbqk9cd2"); 71 sendStr.append("&mobile=").append(mobile); 72 for (int i = 0; i < lstParams.size(); i++) { 73 content+=",@"+String.valueOf(i+1)+"@="+lstParams.get(i); 74 } 75 if(!content.equals("")){ 76 content=content.substring(1,content.length()); 77 } 78 sendStr.append("&content=").append(content); 79 sendStr.append("&msgtype=").append("2"); 80 sendStr.append("&code=").append("utf-8"); 81 sendStr.append("&tempid=").append(template); 82 String serviceURL = "http://IP:8030/service/httpService/httpInterface.do?method=sendMsg"; 83 try { 84 //String commString ="Sn="+username+"&Pwd="+password+"&mobile="+mobile+"&content="+URLEncoder.encode(content,"UTF-8"); 85 res = connectURL(sendStr.toString().trim(),serviceURL); 86 } catch (Exception e) { 87 return "-10000"; 88 } 89 //设置返回值 解析返回值 90 //正则表达式 91 Pattern pattern = Pattern.compile("<int xmlns=\"http://tempuri.org/\">(.*)</int>"); 92 Matcher matcher = pattern.matcher(res); 93 while (matcher.find()) { 94 resultok = matcher.group(1); 95 } 96 } catch (Exception e) { 97 log.error(e.getMessage()); 98 } 99 return resultok; 100 } 101 102 103 }