fs-danaus
2023-09-14 31400fffd7b31397d6d8882d3927e7b870924233
提交 | 用户 | age
a6a76f 1 package com.yc.duanxin;
F 2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStreamReader;
6 import java.io.UnsupportedEncodingException;
7 import java.net.HttpURLConnection;
8 import java.net.MalformedURLException;
9 import java.net.URL;
10 import java.net.UnknownHostException;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.regex.Matcher;
15 import java.util.regex.Pattern;
16 import org.apache.commons.httpclient.HttpClient;
17 import org.apache.commons.httpclient.HttpException;
18 import org.apache.commons.httpclient.NameValuePair;
19 import org.apache.commons.httpclient.methods.PostMethod;
20 import com.yc.param.DuanXinPrama;
21 import com.yc.utils.KeywordFilter;
22
23 /**
24  * 接收及发送短信
25  * 
26  * @author pengbei
27  * 
28  */
29
30 public class Sms {
31     /**
32      * 发送短信
33      * 
34      * @param mob
35      *            手机号 可以多个如(13888888886,13888888887,1388888888 一次最多对50个手机发送)
36      * @param content
37      *            短信内容
38      * @return
39      * @throws IOException
40      * @throws HttpException
41      * @throws UnsupportedEncodingException
42      */
43     /** 登录帐号 */
44     private String uid = "";
45     /** 登录密匙 */
46     private String key = "";
47
48     public String getUid() {
49         return uid;
50     }
51
52     public void setUid(String uid) {
53         this.uid = uid;
54     }
55
56     public String getKey() {
57         return key;
58     }
59
60     public void setKey(String key) {
61         this.key = key;
62     }
ff7d02 63     
J 64     public static void main(String[] args) {
65         Sms sms = new Sms();
66         sms.setKey("b6f237346163cccc2eea");
67         sms.setUid("yingchen");
68         
48adfb 69         String rs = sms.sendSMS("13639117724", "【巴士软件】验证码 [6731],请勿告诉他人!有效时间10分钟");
ff7d02 70 //        int rsNum = Integer.parseInt(rs);
J 71 //        String ret;
72 //        if (rsNum < 0) {
73 //        
74 //            switch (rsNum) {
75 //                case -1:
76 //                ret = "没有该用户账户" + "[" + corpEntity.getSmsKey() + "]";
77 //                break;
78 //                case -2:
79 //                ret = "密钥不正确 ";
80 //                break;
81 //                case -3:
82 //                ret = "短信数量不足";
83 //                break;
84 //                case -11:
85 //                ret = "该用户被禁用"+"["+ corpEntity.getSmsKey() + "]";
86 //                break;
87 //                case -14:
88 //                ret = "短信内容出现非法字符"+"[" + content + "]";
89 //                break;
90 //                case -4:
91 //                ret = "手机号格式不正确"+"[" + tel + "]";
92 //                break;
93 //                case -41:
94 //                ret = "手机号码为空";
95 //                break;
96 //                case -42:
97 //                ret = "短信内容为空";
98 //                break;
99 //                case -51:
100 //                ret = "短信签名格式不正确,接口签名格式为:【签名内容】";
101 //                break;
102 //            }
103 //        }
104         System.out.println(rs);
105     }
a6a76f 106
F 107     /**
108      * 发送短信
109      * @param mob发送短信接收的手机号
110      * @param content 发送短信的内容
111      * @return
112      */
113     public String sendSMS(String mob, String content) {
114         try {
115             // 过滤关键词
116             String contents = filterSMSKey(content);
117             HttpClient client = new HttpClient();
ff7d02 118             //PostMethod post = new PostMethod("http://gbk.sms.webchinese.cn/");
J 119             //post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=gbk");// 在头文件中设置转码
120             PostMethod post = new PostMethod("https://utf8api.smschinese.cn");  //新版接口, Modified by Johns Wang,2021-04-01 , 新接口后台管理地址:  http://user.smschinese.cn/Login.shtml 
121             post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");// 在头文件中设置转码
a6a76f 122             NameValuePair[] data = { new NameValuePair("Uid", getUid()), new NameValuePair("Key", getKey()),
F 123                     new NameValuePair("smsMob", mob), new NameValuePair("smsText", contents) };
124             post.setRequestBody(data);
125             client.executeMethod(post);
ff7d02 126             String result = new String(post.getResponseBodyAsString().getBytes("utf-8"));
a6a76f 127             post.releaseConnection();
F 128             return result;
129         } catch (Exception e) {
130             return "-1";
131         }
132     }
133
134     /**
135      * 接收短信
136      * 
137      * @param Action
138      *            UP 输出上行回复信息
139      * @param Prompt
140      *            0 下次继续提示,1 输出回复下次不提示
141      * @return 回复获得用户的集合
142      * @throws IOException
143      * @throws HttpException
144      * @throws UnsupportedEncodingException
145      */
146     public List<HashMap<String, String>> upSMS(String Action, String Prompt)
147             throws IOException, HttpException, UnsupportedEncodingException {
148         SmsParm smsParm = new SmsParm();// 接收回复短信统一用公司账号进行处理,因为回复过来的不知道是属于那个客户的,也不需要收费。
149         HttpURLConnection httpconn = null;
150         // 组装配置参数
151         StringBuilder sb = new StringBuilder();
152         sb.append("http://sms.webchinese.cn/web_api/SMS/?");// utf-8:http://sms.webchinese.cn/web_api/SMS/
153                                                             // gbk:http://sms.webchinese.cn/web_api/SMS/GBK
154         sb.append("Uid=").append(smsParm.getUid());
155         sb.append("&Key=").append(smsParm.getKey());
156         sb.append("&Action=").append(Action);
157         sb.append("&Prompt=").append(Prompt);
158         List<HashMap<String, String>> infoList = new ArrayList<HashMap<String, String>>();
159         try {
160             URL url = new URL(sb.toString());
161             httpconn = (HttpURLConnection) url.openConnection();
162             BufferedReader rd = new BufferedReader(new InputStreamReader(httpconn.getInputStream()));
163             Pattern pattern = null;
164             Matcher matcher = null;
165             HashMap<String, String> infoMap = new HashMap<String, String>();
166             String result = "";
167             while (null != (result = rd.readLine())) {
168                 if (result.indexOf("<mob>") != -1) {// 有电话
169                     pattern = Pattern.compile("<mob>(.*)</mob>");
170                     matcher = pattern.matcher(result);
171                     if (matcher.find()) {// 只要读到的第一个
172                         result = matcher.group(1);
173                         infoMap.put(DuanXinPrama.MOB, result);
174                         continue;
175                     }
176                 }
177                 if (result.indexOf("<content>") != -1) {// 有回复内容
178                     pattern = Pattern.compile("<content>(.*)</content>");
179                     matcher = pattern.matcher(result);
180                     if (matcher.find()) {// 只要读到的第一个
181                         result = matcher.group(1);
182                         infoMap.put(DuanXinPrama.CONTENT, result);
183                         continue;
184                     }
185                 }
186                 if (result.indexOf("</time>") != -1) {// 有回复时间
187                     pattern = Pattern.compile("<time>(.*)</time>");
188                     matcher = pattern.matcher(result);
189                     if (matcher.find()) {// 只要读到的第一个
190                         result = matcher.group(1);
191                         infoMap.put(DuanXinPrama.TIME, result);
192                         continue;
193                     }
194                 }
195                 if (result.indexOf("</deliver>") != -1) {// 一条信息读取结束
196                     infoList.add(infoMap);
197                     infoMap = new HashMap<String, String>(); // 清空infoMap
198                 }
199             }
200             rd.close();
201         } catch (MalformedURLException e) {
202             e.printStackTrace();
203         } catch (UnknownHostException e) {
204             // 不处理
205             System.out.println("发送手机短信时,无法联系短信服务器:" + e.getMessage());
206         } catch (IOException e) {
207             e.printStackTrace();
208         } finally {
209             if (httpconn != null) {
210                 httpconn.disconnect();
211                 httpconn = null;
212             }
213         }
214         return infoList;
215     }
216
217     private String filterSMSKey(String content) {// 处理手机短信的一些关键词,以便能发送出去
218         KeywordFilter.init();
219         return KeywordFilter.getFilterString(content, "*");
220     }
221 }