最近由于公司的業務拓展,需要給國外用戶發送國際短信,像西班牙、葡萄牙、意大利這些國家都要發,還有中國的香港、澳門、臺灣(港澳臺)這些地區也要發,不過現在已經有許多公司提供國際短信的業務了,之前使用過云片的驗證碼業務,順便看到他們也有國際短信的業務,并且更重要的是,不需要修改任何代碼,只要添加下國際短信模板,就可以直接使用之前的代碼繼續發送國際短信,簡直太方便了。
廢話不多說,直接上代碼。
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
/** * Created by bingone on 15/12/16. */ import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import java.io.IOException; import java.net.URISyntaxException; import java.net.URLEncoder; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 短信http接口的java代碼調用示例 * 基于Apache HttpClient 4.3 * * @author songchao * @since 2015-04-03 */ public class JavaSmsApi { //查賬戶信息的http地址 private static String URI_GET_USER_INFO = "https://sms.yunpian.com/v2/user/get.json" ; //智能匹配模板發送接口的http地址 private static String URI_SEND_SMS = "https://sms.yunpian.com/v2/sms/single_send.json" ; //模板發送接口的http地址 private static String URI_TPL_SEND_SMS = "https://sms.yunpian.com/v2/sms/tpl_single_send.json" ; //發送語音驗證碼接口的http地址 private static String URI_SEND_VOICE = "https://voice.yunpian.com/v2/voice/send.json" ; //綁定主叫、被叫關系的接口http地址 private static String URI_SEND_BIND = "https://call.yunpian.com/v2/call/bind.json" ; //解綁主叫、被叫關系的接口http地址 private static String URI_SEND_UNBIND = "https://call.yunpian.com/v2/call/unbind.json" ; //編碼格式。發送編碼格式統一用UTF-8 private static String ENCODING = "UTF-8" ; public static void main(String[] args) throws IOException, URISyntaxException { //修改為您的apikey.apikey可在官網(http://www.yunpian.com)登錄后獲取 String apikey = "xxxxxxxxxxxxxxxxxxxxx" ; //修改為您要發送的手機號 String mobile = "130xxxxxxxx" ; /**************** 查賬戶信息調用示例 *****************/ System.out.println(JavaSmsApi.getUserInfo(apikey)); /**************** 使用智能匹配模板接口發短信(推薦) *****************/ //設置您要發送的內容(內容必須和某個模板匹配。以下例子匹配的是系統提供的1號模板) String text = "【云片網】您的驗證碼是1234" ; //發短信調用示例 // System.out.println(JavaSmsApi.sendSms(apikey, text, mobile)); /**************** 使用指定模板接口發短信(不推薦,建議使用智能匹配模板接口) *****************/ //設置模板ID,如使用1號模板:【#company#】您的驗證碼是#code# long tpl_id = 1 ; //設置對應的模板變量值 String tpl_value = URLEncoder.encode( "#code#" ,ENCODING) + "=" + URLEncoder.encode( "1234" , ENCODING) + "&" + URLEncoder.encode( "#company#" ,ENCODING) + "=" + URLEncoder.encode( "云片網" ,ENCODING); //模板發送的調用示例 System.out.println(tpl_value); System.out.println(JavaSmsApi.tplSendSms(apikey, tpl_id, tpl_value, mobile)); /**************** 使用接口發語音驗證碼 *****************/ String code = "1234" ; //System.out.println(JavaSmsApi.sendVoice(apikey, mobile ,code)); /**************** 使用接口綁定主被叫號碼 *****************/ String from = "+86130xxxxxxxx" ; String to = "+86131xxxxxxxx" ; Integer duration = 30 * 60 ; // 綁定30分鐘 // System.out.println(JavaSmsApi.bindCall(apikey, from ,to , duration)); /**************** 使用接口解綁主被叫號碼 *****************/ // System.out.println(JavaSmsApi.unbindCall(apikey, from, to)); } /** * 取賬戶信息 * * @return json格式字符串 * @throws java.io.IOException */ public static String getUserInfo(String apikey) throws IOException, URISyntaxException { Map<String, String> params = new HashMap<String, String>(); params.put( "apikey" , apikey); return post(URI_GET_USER_INFO, params); } /** * 智能匹配模板接口發短信 * * @param apikey apikey * @param text 短信內容 * @param mobile 接受的手機號 * @return json格式字符串 * @throws IOException */ public static String sendSms(String apikey, String text, String mobile) throws IOException { Map<String, String> params = new HashMap<String, String>(); params.put( "apikey" , apikey); params.put( "text" , text); params.put( "mobile" , mobile); return post(URI_SEND_SMS, params); } /** * 通過模板發送短信(不推薦) * * @param apikey apikey * @param tpl_id 模板id * @param tpl_value 模板變量值 * @param mobile 接受的手機號 * @return json格式字符串 * @throws IOException */ public static String tplSendSms(String apikey, long tpl_id, String tpl_value, String mobile) throws IOException { Map<String, String> params = new HashMap<String, String>(); params.put( "apikey" , apikey); params.put( "tpl_id" , String.valueOf(tpl_id)); params.put( "tpl_value" , tpl_value); params.put( "mobile" , mobile); return post(URI_TPL_SEND_SMS, params); } /** * 通過接口發送語音驗證碼 * @param apikey apikey * @param mobile 接收的手機號 * @param code 驗證碼 * @return */ public static String sendVoice(String apikey, String mobile, String code) { Map<String, String> params = new HashMap<String, String>(); params.put( "apikey" , apikey); params.put( "mobile" , mobile); params.put( "code" , code); return post(URI_SEND_VOICE, params); } /** * 通過接口綁定主被叫號碼 * @param apikey apikey * @param from 主叫 * @param to 被叫 * @param duration 有效時長,單位:秒 * @return */ public static String bindCall(String apikey, String from, String to , Integer duration ) { Map<String, String> params = new HashMap<String, String>(); params.put( "apikey" , apikey); params.put( "from" , from); params.put( "to" , to); params.put( "duration" , String.valueOf(duration)); return post(URI_SEND_BIND, params); } /** * 通過接口解綁綁定主被叫號碼 * @param apikey apikey * @param from 主叫 * @param to 被叫 * @return */ public static String unbindCall(String apikey, String from, String to) { Map<String, String> params = new HashMap<String, String>(); params.put( "apikey" , apikey); params.put( "from" , from); params.put( "to" , to); return post(URI_SEND_UNBIND, params); } /** * 基于HttpClient 4.3的通用POST方法 * * @param url 提交的URL * @param paramsMap 提交<參數,值>Map * @return 提交響應 */ public static String post(String url, Map<String, String> paramsMap) { CloseableHttpClient client = HttpClients.createDefault(); String responseText = "" ; CloseableHttpResponse response = null ; try { HttpPost method = new HttpPost(url); if (paramsMap != null ) { List<NameValuePair> paramList = new ArrayList<NameValuePair>(); for (Map.Entry<String, String> param : paramsMap.entrySet()) { NameValuePair pair = new BasicNameValuePair(param.getKey(), param.getValue()); paramList.add(pair); } method.setEntity( new UrlEncodedFormEntity(paramList, ENCODING)); } response = client.execute(method); HttpEntity entity = response.getEntity(); if (entity != null ) { responseText = EntityUtils.toString(entity, ENCODING); } } catch (Exception e) { e.printStackTrace(); } finally { try { response.close(); } catch (Exception e) { e.printStackTrace(); } } return responseText; } } |
代碼看上去有點亂了,不過我們用到的API接口也就那么幾個,具體的可以看這篇文章如何使用云片API發送短信驗證碼,只要把那三個接口搞定了,無論是國際短信、國內短信還是短信驗證碼、手機驗證碼,都可以輕松搞定,so easy!
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.jianshu.com/p/59bcca9692c3