ad5081d3
孙向锦
初始化项目
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
package com.fh.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
/**
* 说明:短信接口类
* 创建人:FH Q313596790
* 修改时间:2013年2月22日
* @version
*/
public class SmsUtil {
public static void main(String [] args) {
sendSms2("13511111111","您的验证码是:1111。请不要把验证码泄露给其他人。");
//sendSmsAll(List<PageData> list)
//sendSms1();
}
//短信商 一 http://www.dxton.com/ =====================================================================================
/**
* 给一个人发送单条短信
* @param mobile 手机号
* @param code 短信内容
*/
public static void sendSms1(String mobile,String code){
String account = "", password = "";
String strSMS1 = Tools.readTxtFile(Const.SMS1); //读取短信1配置
if(null != strSMS1 && !"".equals(strSMS1)){
String strS1[] = strSMS1.split(",fh,");
if(strS1.length == 2){
account = strS1[0];
password = strS1[1];
}
}
String PostData = "";
try {
PostData = "account="+account+"&password="+password+"&mobile="+mobile+"&content="+URLEncoder.encode(code,"utf-8");
} catch (UnsupportedEncodingException e) {
System.out.println("短信提交失败");
}
//System.out.println(PostData);
String ret = SMS(PostData, "http://sms.106jiekou.com/utf8/sms.aspx");
System.out.println(ret);
/*
100 发送成功
101 验证失败
102 手机号码格式不正确
103 会员级别不够
104 内容未审核
105 内容过多
106 账户余额不足
107 Ip受限
108 手机号码发送太频繁,请换号或隔天再发
109 帐号被锁定
110 发送通道不正确
111 当前时间段禁止短信发送
120 系统升级
*/
}
public static String SMS(String postData, String postUrl) {
try {
//发送POST请求
URL url = new URL(postUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setUseCaches(false);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Length", "" + postData.length());
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
out.write(postData);
out.flush();
out.close();
//获取响应状态
if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
System.out.println("connect failed!");
return "";
}
//获取响应内容体
String line, result = "";
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
while ((line = in.readLine()) != null) {
result += line + "\n";
}
in.close();
return result;
} catch (IOException e) {
e.printStackTrace(System.out);
}
return "";
}
//===================================================================================================================
/**
*
* 短信商 二 http://www.ihuyi.com/ =====================================================================================
*
*/
private static String Url = "http://106.ihuyi.com/webservice/sms.php?method=Submit";
/**
* 给一个人发送单条短信
* @param mobile 手机号
* @param code 短信内容
*/
public static void sendSms2(String mobile,String code){
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(Url);
client.getParams().setContentCharset("UTF-8");
method.setRequestHeader("ContentType","application/x-www-form-urlencoded;charset=UTF-8");
String content = new String(code);
String account = "", password = "";
String strSMS2 = Tools.readTxtFile(Const.SMS2); //读取短信2配置
if(null != strSMS2 && !"".equals(strSMS2)){
String strS2[] = strSMS2.split(",fh,");
if(strS2.length == 2){
account = strS2[0];
password = strS2[1];
}
}
NameValuePair[] data = {//提交短信
new NameValuePair("account", account),
new NameValuePair("password", password), //密码可以使用明文密码或使用32位MD5加密
new NameValuePair("mobile", mobile),
new NameValuePair("content", content),
};
method.setRequestBody(data);
try {
client.executeMethod(method);
String SubmitResult =method.getResponseBodyAsString();
Document doc = DocumentHelper.parseText(SubmitResult);
Element root = doc.getRootElement();
code = root.elementText("code");
String msg = root.elementText("msg");
String smsid = root.elementText("smsid");
System.out.println(code);
System.out.println(msg);
System.out.println(smsid);
if(code == "2"){
System.out.println("短信提交成功");
}
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
}
/**
* 给多个人发送单条短信
* @param list 手机号验证码
*/
public static void sendSmsAll(List<PageData> list){
String code;
String mobile;
for(int i=0;i<list.size();i++){
code=list.get(i).get("code").toString();
mobile=list.get(i).get("mobile").toString();
sendSms2(mobile,code);
}
}
// =================================================================================================
}
|