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
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
/*
功能: web.cr6868.com HTTP接口 發送短信
說明: http://web.cr6868.com/asmx/smsservice.aspx?name=登錄名&pwd=接口密碼
&mobile=手機號碼&content=內容&sign=簽名&stime=發送時間&type=pt&extno=自定義擴展碼
*/
public class xioo {
public static void main(String[] args) throws Exception {
// 用戶名
String name="wbxxx";
// 密碼
String pwd="0C759A360WWBD5F5E0F5FF9F0597";
// 電話號碼字符串,中間用英文逗號間隔
StringBuffer mobileString=new StringBuffer("");
// 內容字符串
StringBuffer contextString=new StringBuffer("短信內容");
// 簽名
String sign="簽名";
// 追加發送時間,可為空,為空為及時發送
String stime="";
// 擴展碼,必須為數字 可為空
StringBuffer extno=new StringBuffer();
System.out.println(doPost(name, pwd, mobileString, contextString, sign, stime, extno));
}
/**
* 發送短信
*
* @param name 用戶名
* @param pwd 密碼
* @param mobileString 電話號碼字符串,中間用英文逗號間隔
* @param contextString 內容字符串
* @param sign 簽名
* @param stime 追加發送時間,可為空,為空為及時發送
* @param extno 擴展碼,必須為數字 可為空
* @return
* @throws Exception
*/
public static String doPost(String name, String pwd,
StringBuffer mobileString, StringBuffer contextString,
String sign, String stime, StringBuffer extno) throws Exception {
StringBuffer param = new StringBuffer();
param.append("name="+name);
param.append("&pwd="+pwd);
param.append("&mobile=").append(mobileString);
param.append("&content=").append(URLEncoder.encode(contextString.toString(),"UTF-8"));
param.append("&stime="+stime);
param.append("&sign=").append(URLEncoder.encode(sign,"UTF-8"));
param.append("&type=pt");
param.append("&extno=").append(extno);
URL localURL = new URL("http://web.cr6868.com/asmx/smsservice.aspx?");
URLConnection connection = localURL.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection)connection;
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpURLConnection.setRequestProperty("Content-Length", String.valueOf(param.length()));
OutputStream outputStream = null;
OutputStreamWriter outputStreamWriter = null;
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader reader = null;
String resultBuffer = "";
try {
outputStream = httpURLConnection.getOutputStream();
outputStreamWriter = new OutputStreamWriter(outputStream);
outputStreamWriter.write(param.toString());
outputStreamWriter.flush();
if (httpURLConnection.getResponseCode() >= 300) {
throw new Exception
("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
inputStream = httpURLConnection.getInputStream();
resultBuffer = convertStreamToString(inputStream);
}
finally {
if (outputStreamWriter != null) {
outputStreamWriter.close();
}
if (outputStream != null) {
outputStream.close();
}
if (reader != null) {
reader.close();
}
if (inputStreamReader != null) {
inputStreamReader.close();
}
if (inputStream != null) {
inputStream.close();
}
}
return resultBuffer;
}
/**
* 轉換返回值類型為UTF-8格式.
* @param is
* @return
*/
public static String convertStreamToString(InputStream is) {
StringBuilder sb1 = new StringBuilder();
byte[] bytes = new byte[4096];
int size = 0;
try {
while ((size = is.read(bytes)) > 0) {
String str = new String(bytes, 0, size, "UTF-8");
sb1.append(str);
}
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
is.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
return sb1.toString();
}
}
|