标签:电话 ble pil instr stack last 黑户 font put
发短信接口
1 import java.io.IOException;
2 import java.io.InputStream;
3 import java.io.OutputStream;
4 import java.net.HttpURLConnection;
5 import java.net.MalformedURLException;
6 import java.net.URL;
7 import java.security.MessageDigest;
8 import java.text.SimpleDateFormat;
9 import java.util.Date;
10 import java.util.HashMap;
11 import java.util.regex.Matcher;
12 import java.util.regex.Pattern;
13
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17
18 public class MessageSend {
19 //获取当前时间
20 private static final Logger log = LoggerFactory.getLogger(MessageSend.class);
21 private static String getnowtime(){
22 String todayTime=new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss-SSS").format( new Date() );
23 return todayTime;
24
25 }
26 public static void main(String[] args){
27 HashMap<String, Object> map = new HashMap();
28 map.put("password", password);
29 map.put("password", password);
30 map.put("epid", epid);
31 String phone_num="电话号码";
32 String msgContent="测试消息";
33
34 String returnValue=sendMsg(map, phone_num, msgContent);
35 System.out.println(returnValue);
36 }
37
38 public static String sendurl = "www.fbaidu.com";//发短信URL
39 public static String recurl = "www.sbaidu.com";
40 //发送短信
41 public static String sendMsg(HashMap map,String phone_num,String msgContent){
42 String returnValue="-1";
43 try {
44 String userName = (String)map.get("username");
45 String passWord = (String)map.get("password");
46 String epid = (String)map.get("epid");
47 String[] num = phone_num.split(",");
48 StringBuffer wrongNum = new StringBuffer();
49 wrongNum.append("");
50 boolean checkPhoneNum = true;
51 for(int i=0;i<num.length;i++){
52 checkPhoneNum = checkPhoneNumFormat(num[i]);
53 if(!checkPhoneNum){
54 wrongNum.append(num[i]);
55 wrongNum.append(".");
56 }
57 checkPhoneNum = true;
58 }
59 if(!wrongNum.toString().equals("")){
60 throw new Exception("电话号码输入有误["+wrongNum.toString()+"]");
61 }
62 StringBuffer sb = new StringBuffer();
63 sb.append("username=");
64 sb.append(userName);
65 sb.append("&");
66 sb.append("password=");
67 sb.append(passWord);
68 sb.append("&");
69 sb.append("phone=");
70 sb.append(phone_num);
71 sb.append("&");
72 sb.append("message=");
73 sb.append(msgContent);
74 sb.append("&");
75 sb.append("epid=");
76 String key = "";
77 // if(epid != null && !epid.equals(""))
78 // key = encodeToMD5(phone_num,8,10,epid);
79 sb.append(epid);
80 String linkid = "";
81 sb.append("&");
82 sb.append("linkid=");
83 sb.append(linkid);
84 String subcode = "";
85 sb.append("&");
86 sb.append("subcode=");
87 sb.append(subcode);
88 URL http_url = new URL(sendurl);
89 HttpURLConnection http_conn = (HttpURLConnection)http_url.openConnection();
90 http_conn.setRequestMethod("GET");
91 http_conn.setDoOutput(true);
92 OutputStream outStream = http_conn.getOutputStream();
93
94 // System.out.println(sb.toString());
95 outStream.write(sb.toString().getBytes());
96 // System.out.println(sb.toString().getBytes()+"=sb.toString().getBytes()");
97 outStream.flush();
98 outStream.close();
99
100 InputStream inStream = http_conn.getInputStream();
101 byte[] inByte = new byte[inStream.available()];
102 int offset = 0;
103
104 while(offset < inByte.length){
105 offset += inStream.read(inByte);
106 }
107 inStream.close();
108 http_conn.disconnect();
109 returnValue = new String(inByte);
110 //System.out.println("inByte="+inByte.length);
111 // System.out.println("inStream.read(inByte)="+inStream.read(inByte));
112 if(returnValue.equals("00")){//
113 log.debug("短信发送成功");
114 }else if(returnValue.equals("1")){//
115 log.debug("短信发送失败,原因:参数不完整!");
116 }else if(returnValue.equals("2")){//
117 log.debug("短信发送失败,原因:鉴权失败(包括:用户状态不正常、密码错误、用户不存在、地址验证失败,黑户)");
118 }else if(returnValue.equals("3")){//
119 log.debug("短信发送失败,原因:号码数量超出50条");
120 }else if(returnValue.equals("4")){//
121 log.debug("短信发送失败,原因:发送失败");
122 }else if(returnValue.equals("5")){//
123 log.debug("短信发送失败,原因:余额不足");
124 }else if(returnValue.equals("6")){//
125 log.debug("短信发送失败,原因:发送内容含屏蔽词");
126 }else {
127 System.out.println(returnValue);
128 log.debug("短信发送失败:原因"+returnValue);
129 }
130 } catch (MalformedURLException e){
131 e.printStackTrace();
132 } catch (IOException e){
133 e.printStackTrace();
134 } catch(Exception e){
135 e.printStackTrace();
136 }
137 return returnValue;
138 }
139 private static boolean isLogin = false;
140
141 public static String encodeToMD5(String phone_num, int firstFixd, int lastFixd, Number epid){
142 StringBuffer sb = new StringBuffer();
143 try {
144 if(null == phone_num)
145 throw new NullPointerException();
146 String[] num = phone_num.split(",");
147 String key = num[0].substring(0, firstFixd)
148 + num[num.length -1].substring(num[num.length -1].length() - lastFixd) + epid;
149 MessageDigest msgd = MessageDigest.getInstance("MD5");
150 msgd.update(key.getBytes());
151 byte[] md5 = msgd.digest();
152 for(int i=0;i < md5.length; i++){
153 if((md5[i] & 0xFF) < 10)
154 sb.append("0");
155 sb.append(Integer.toString(md5[i] & 0xFF, 16));
156 }
157 } catch (Exception e) {
158 e.printStackTrace();
159 }
160 System.out.println(sb.toString().toUpperCase());
161 return sb.toString().toUpperCase();
162 }
163 //验证手机号
164 public static boolean checkPhoneNumFormat(String phone_num){
165 boolean flag = false;
166 String regex = "^(13[0-9]|15[0-9]|18[0-9])\\d{8}$";
167 Pattern pattern = Pattern.compile(regex);
168 Matcher matcher = pattern.matcher(phone_num);
169 if(matcher.find()){
170 flag = true;
171 }
172 return flag;
173 }
174
175
176 }
标签:电话 ble pil instr stack last 黑户 font put
原文地址:http://www.cnblogs.com/guoziyi/p/5996158.html