1. [代码]短信提醒 跳至 [1] [全屏预览]
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
|
package
com.sh.sendsms;
import
java.io.IOException;
import
java.io.InputStream;
import
java.io.UnsupportedEncodingException;
import
java.util.ArrayList;
import
java.util.List;
import
org.apache.commons.io.IOUtils;
import
org.apache.http.HttpEntity;
import
org.apache.http.HttpResponse;
import
org.apache.http.NameValuePair;
import
org.apache.http.client.ClientProtocolException;
import
org.apache.http.client.entity.UrlEncodedFormEntity;
import
org.apache.http.client.methods.HttpPost;
import
org.apache.http.impl.client.DefaultHttpClient;
import
org.apache.http.message.BasicNameValuePair;
/**
* 尚景 短信 http://goinsms.com/
* 依赖jar包
* httpcore-4.2.2.jar
* httpclient-4.2.1.jar
* commons-io-2.0.1.jar
*
* 名称: SmsUtil.java
* 描述:
* 类型: JAVA
* 最近修改时间:2014-3-5 下午4:47:14
* @since 2014-3-5
* @author liaokn
*/
public
class
SmsUtil {
String url =
"http://api.goinsms.com/sms"
;
// api账号(请联系销售获取,不是网站登陆账号)
String userId =
"a0e72f36-e80d-4931-b70a-b2bd41d8ce2b"
;
// api密码(不是网站登陆密码)
String passwd =
"ebbfd3b1da"
;
public
String sendSms(String content, String mobiles) {
String result =
null
;
String posturl = url +
"/http/submit"
;
String message =
"{'content':'"
+ content +
"',"
+
"'mobiles':'"
+ mobiles +
"',"
+
"'passwd':'"
+ passwd +
"',"
+
"'userId':'"
+ userId +
"'}"
;
DefaultHttpClient httpclient =
new
DefaultHttpClient();
HttpPost httppost =
new
HttpPost(posturl);
List<NameValuePair> params =
new
ArrayList<NameValuePair>();
params.add(
new
BasicNameValuePair(
"message"
, message));
try
{
httppost.setEntity(
new
UrlEncodedFormEntity(params,
"UTF-8"
));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if
(entity !=
null
) {
InputStream instream =
null
;
try
{
instream = entity.getContent();
result = IOUtils.toString(instream,
"utf-8"
);
}
finally
{
if
(instream !=
null
)
instream.close();
}
}
}
catch
(UnsupportedEncodingException e) {
e.printStackTrace();
}
catch
(ClientProtocolException e) {
e.printStackTrace();
}
catch
(IOException e) {
e.printStackTrace();
}
return
result;
}
public
String getReport() {
String result =
null
;
String posturl = url +
"/http/report"
;
String message =
"{\"passwd\":\""
+ passwd +
"\",\"userId\":\""
+ userId +
"\"}"
;
DefaultHttpClient httpclient =
new
DefaultHttpClient();
HttpPost httppost =
new
HttpPost(posturl);
List<NameValuePair> params =
new
ArrayList<NameValuePair>();
params.add(
new
BasicNameValuePair(
"message"
, message));
try
{
httppost.setEntity(
new
UrlEncodedFormEntity(params,
"UTF-8"
));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if
(entity !=
null
) {
InputStream instream =
null
;
try
{
instream = entity.getContent();
result = IOUtils.toString(instream,
"utf-8"
);
}
finally
{
if
(instream !=
null
)
instream.close();
}
}
}
catch
(UnsupportedEncodingException e) {
e.printStackTrace();
}
catch
(ClientProtocolException e) {
e.printStackTrace();
}
catch
(IOException e) {
e.printStackTrace();
}
return
result;
}
public
String getDeliver() {
String result =
null
;
String posturl = url +
"/http/deliver"
;
String message =
"{\"passwd\":\""
+ passwd +
"\",\"userId\":\""
+ userId +
"\"}"
;
DefaultHttpClient httpclient =
new
DefaultHttpClient();
HttpPost httppost =
new
HttpPost(posturl);
List<NameValuePair> params =
new
ArrayList<NameValuePair>();
params.add(
new
BasicNameValuePair(
"message"
, message));
try
{
httppost.setEntity(
new
UrlEncodedFormEntity(params,
"UTF-8"
));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if
(entity !=
null
) {
InputStream instream =
null
;
try
{
instream = entity.getContent();
result = IOUtils.toString(instream,
"utf-8"
);
}
finally
{
if
(instream !=
null
)
instream.close();
}
}
}
catch
(UnsupportedEncodingException e) {
e.printStackTrace();
}
catch
(ClientProtocolException e) {
e.printStackTrace();
}
catch
(IOException e) {
e.printStackTrace();
}
return
result;
}
public
String getBalance() {
String result =
null
;
String posturl = url +
"/http/balance"
;
String message =
"{\"passwd\":\""
+ passwd +
"\",\"userId\":\""
+ userId +
"\"}"
;
DefaultHttpClient httpclient =
new
DefaultHttpClient();
HttpPost httppost =
new
HttpPost(posturl);
List<NameValuePair> params =
new
ArrayList<NameValuePair>();
params.add(
new
BasicNameValuePair(
"message"
, message));
try
{
httppost.setEntity(
new
UrlEncodedFormEntity(params,
"UTF-8"
));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if
(entity !=
null
) {
InputStream instream =
null
;
try
{
instream = entity.getContent();
result = IOUtils.toString(instream,
"utf-8"
);
}
finally
{
if
(instream !=
null
)
instream.close();
}
}
}
catch
(UnsupportedEncodingException e) {
e.printStackTrace();
}
catch
(ClientProtocolException e) {
e.printStackTrace();
}
catch
(IOException e) {
e.printStackTrace();
}
return
result;
}
/**
* @param args
*/
public
static
void
main(String[] args) {
// TODO Auto-generated method stub
SmsUtil api =
new
SmsUtil();
System.out.println(
"*************发送短信*************"
);
String content =
"祝你生日快乐,天天开心!"
;
String mobiles =
"18616131909"
;
System.out.println(
"号码:"
+ mobiles +
" 内容:"
+ content);
System.out.println(
"结果:"
+ api.sendSms(content, mobiles));
System.out.println(
"*************状态报告*************"
);
System.out.println(
"结果:"
+ api.getReport());
System.out.println(
"*************获取上行*************"
);
System.out.println(
"结果:"
+ api.getDeliver());
System.out.println(
"*************获取余额*************"
);
System.out.println(
"结果:"
+ api.getBalance());
}
}
|