• java DEMO

    java DEMO

    本样例基于 Apache HttpClient v4.3 , 可下载完整样例项目。 同时,也可 下载基于3.1版的完整样例项目。 样例项目包含代码、依赖 jar包和pom.xml配置。Windows用户请注意:下载的代码及说明文件都是UTF-8格式编码,且换行风格为Linux的。 用记事本或写字板直接打开显示会不正常。建议将其导入(或复制)到Eclipse等IDE, 并将项目编码格式设置为UTF-8后再打开这些文件查看。若直接查看请用Notepad++、EditPlus等编辑器。

    功能说明:该接口要求提前在云片后台添加模板,提交短信时,系统会自动匹配审核通过的模板,匹配成功任意一个模板即可发送。系统已提供的默认模板添加签名后可以直接使用。

    1. /**
    2. * Created by bingone on 15/12/16.
    3. */
    4. import org.apache.http.HttpEntity;
    5. import org.apache.http.NameValuePair;
    6. import org.apache.http.client.entity.UrlEncodedFormEntity;
    7. import org.apache.http.client.methods.CloseableHttpResponse;
    8. import org.apache.http.client.methods.HttpPost;
    9. import org.apache.http.impl.client.CloseableHttpClient;
    10. import org.apache.http.impl.client.HttpClients;
    11. import org.apache.http.message.BasicNameValuePair;
    12. import org.apache.http.util.EntityUtils;
    13. import java.io.IOException;
    14. import java.net.URISyntaxException;
    15. import java.net.URLEncoder;
    16. import java.util.ArrayList;
    17. import java.util.HashMap;
    18. import java.util.List;
    19. import java.util.Map;
    20. /**
    21. * 短信http接口的java代码调用示例
    22. * 基于Apache HttpClient 4.3
    23. *
    24. * @author songchao
    25. * @since 2015-04-03
    26. */
    27. public class JavaSmsApi {
    28. //查账户信息的http地址
    29. private static String URI_GET_USER_INFO =
    30. "https://sms.yunpian.com/v2/user/get.json";
    31. //智能匹配模板发送接口的http地址
    32. private static String URI_SEND_SMS =
    33. "https://sms.yunpian.com/v2/sms/single_send.json";
    34. //模板发送接口的http地址
    35. private static String URI_TPL_SEND_SMS =
    36. "https://sms.yunpian.com/v2/sms/tpl_single_send.json";
    37. //发送语音验证码接口的http地址
    38. private static String URI_SEND_VOICE =
    39. "https://voice.yunpian.com/v2/voice/send.json";
    40. //绑定主叫、被叫关系的接口http地址
    41. private static String URI_SEND_BIND =
    42. "https://call.yunpian.com/v2/call/bind.json";
    43. //解绑主叫、被叫关系的接口http地址
    44. private static String URI_SEND_UNBIND =
    45. "https://call.yunpian.com/v2/call/unbind.json";
    46. //编码格式。发送编码格式统一用UTF-8
    47. private static String ENCODING = "UTF-8";
    48. public static void main(String[] args) throws IOException,
    49. URISyntaxException {
    50. //修改为您的apikey.apikey可在官网(http://www.yunpian.com)登录后获取
    51. String apikey = "xxxxxxxxxxxxxxxxxxxxx";
    52. //修改为您要发送的手机号
    53. String mobile = "130xxxxxxxx";
    54. /**************** 查账户信息调用示例 *****************/
    55. System.out.println(JavaSmsApi.getUserInfo(apikey));
    56. /**************** 使用智能匹配模板接口发短信(推荐) *****************/
    57. //设置您要发送的内容(内容必须和某个模板匹配。以下例子匹配的是系统提供的1号模板)
    58. String text = "【云片网】您的验证码是1234";
    59. //发短信调用示例
    60. // System.out.println(JavaSmsApi.sendSms(apikey, text, mobile));
    61. /**************** 使用指定模板接口发短信(不推荐,建议使用智能匹配模板接口) ******/
    62. //设置模板ID,如使用1号模板:【#company#】您的验证码是#code#
    63. long tpl_id = 1;
    64. //设置对应的模板变量值
    65. String tpl_value = URLEncoder.encode("#code#", ENCODING) + "=" +
    66. URLEncoder.encode("1234", ENCODING) + "&" + URLEncoder.encode(
    67. "#company#", ENCODING) + "=" + URLEncoder.encode("云片网",
    68. ENCODING);
    69. //模板发送的调用示例
    70. System.out.println(tpl_value);
    71. System.out.println(JavaSmsApi.tplSendSms(apikey, tpl_id, tpl_value,
    72. mobile));
    73. /**************** 使用接口发语音验证码 *****************/
    74. String code = "1234";
    75. //System.out.println(JavaSmsApi.sendVoice(apikey, mobile ,code));
    76. /**************** 使用接口绑定主被叫号码 *****************/
    77. //String from = "+86130xxxxxxxx";
    78. //String to = "+86131xxxxxxxx";
    79. //Integer duration = 30 * 60; // 绑定30分钟
    80. //System.out.println(JavaSmsApi.bindCall(apikey, from ,to , duration));
    81. /**************** 使用接口解绑主被叫号码 *****************/
    82. //System.out.println(JavaSmsApi.unbindCall(apikey, from, to));
    83. }
    84. /**
    85. * 取账户信息
    86. *
    87. * @return json格式字符串
    88. * @throws java.io.IOException
    89. */
    90. public static String getUserInfo(String apikey) throws IOException,
    91. URISyntaxException {
    92. Map < String, String > params = new HashMap < String, String > ();
    93. params.put("apikey", apikey);
    94. return post(URI_GET_USER_INFO, params);
    95. }
    96. /**
    97. * 智能匹配模板接口发短信
    98. *
    99. * @param apikey apikey
    100. * @param text  短信内容
    101. * @param mobile  接受的手机号
    102. * @return json格式字符串
    103. * @throws IOException
    104. */
    105. public static String sendSms(String apikey, String text,
    106. String mobile) throws IOException {
    107. Map < String, String > params = new HashMap < String, String > ();
    108. params.put("apikey", apikey);
    109. params.put("text", text);
    110. params.put("mobile", mobile);
    111. return post(URI_SEND_SMS, params);
    112. }
    113. /**
    114. * 通过模板发送短信(不推荐)
    115. *
    116. * @param apikey apikey
    117. * @param tpl_id  模板id
    118. * @param tpl_value  模板变量值
    119. * @param mobile  接受的手机号
    120. * @return json格式字符串
    121. * @throws IOException
    122. */
    123. public static String tplSendSms(String apikey, long tpl_id, String tpl_value,
    124. String mobile) throws IOException {
    125. Map < String, String > params = new HashMap < String, String > ();
    126. params.put("apikey", apikey);
    127. params.put("tpl_id", String.valueOf(tpl_id));
    128. params.put("tpl_value", tpl_value);
    129. params.put("mobile", mobile);
    130. return post(URI_TPL_SEND_SMS, params);
    131. }
    132. /**
    133. * 通过接口发送语音验证码
    134. * @param apikey apikey
    135. * @param mobile 接收的手机号
    136. * @param code 验证码
    137. * @return
    138. */
    139. public static String sendVoice(String apikey, String mobile, String code) {
    140. Map < String, String > params = new HashMap < String, String > ();
    141. params.put("apikey", apikey);
    142. params.put("mobile", mobile);
    143. params.put("code", code);
    144. return post(URI_SEND_VOICE, params);
    145. }
    146. /**
    147. * 通过接口绑定主被叫号码
    148. * @param apikey apikey
    149. * @param from 主叫
    150. * @param to 被叫
    151. * @param duration 有效时长,单位:秒
    152. * @return
    153. public static String bindCall(String apikey, String from, String to,
    154. Integer duration) {
    155. Map < String, String > params = new HashMap < String, String > ();
    156. params.put("apikey", apikey);
    157. params.put("from", from);
    158. params.put("to", to);
    159. params.put("duration", String.valueOf(duration));
    160. return post(URI_SEND_BIND, params);
    161. }
    162. * 通过接口解绑绑定主被叫号码
    163. * @param apikey apikey
    164. * @param from 主叫
    165. * @param to 被叫
    166. * @return
    167. public static String unbindCall(String apikey, String from, String to) {
    168. Map < String, String > params = new HashMap < String, String > ();
    169. params.put("apikey", apikey);
    170. params.put("from", from);
    171. params.put("to", to);
    172. return post(URI_SEND_UNBIND, params);
    173. }
    174. */
    175. /**
    176. * 基于HttpClient 4.3的通用POST方法
    177. *
    178. * @param url 提交的URL
    179. * @param paramsMap 提交<参数,值>Map
    180. * @return 提交响应
    181. */
    182. public static String post(String url, Map < String, String > paramsMap) {
    183. CloseableHttpClient client = HttpClients.createDefault();
    184. String responseText = "";
    185. CloseableHttpResponse response = null;
    186. try {
    187. HttpPost method = new HttpPost(url);
    188. if (paramsMap != null) {
    189. List < NameValuePair > paramList = new ArrayList <
    190. NameValuePair > ();
    191. for (Map.Entry < String, String > param: paramsMap.entrySet()) {
    192. NameValuePair pair = new BasicNameValuePair(param.getKey(),
    193. param.getValue());
    194. paramList.add(pair);
    195. }
    196. method.setEntity(new UrlEncodedFormEntity(paramList,
    197. ENCODING));
    198. }
    199. response = client.execute(method);
    200. HttpEntity entity = response.getEntity();
    201. if (entity != null) {
    202. responseText = EntityUtils.toString(entity, ENCODING);
    203. }
    204. } catch (Exception e) {
    205. e.printStackTrace();
    206. } finally {
    207. try {
    208. response.close();
    209. } catch (Exception e) {
    210. e.printStackTrace();
    211. }
    212. }
    213. return responseText;
    214. }
    215. }