YunPianSms.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. package com.szwl.util;
  2. import org.apache.http.HttpEntity;
  3. import org.apache.http.NameValuePair;
  4. import org.apache.http.client.entity.UrlEncodedFormEntity;
  5. import org.apache.http.client.methods.CloseableHttpResponse;
  6. import org.apache.http.client.methods.HttpPost;
  7. import org.apache.http.impl.client.CloseableHttpClient;
  8. import org.apache.http.impl.client.HttpClients;
  9. import org.apache.http.message.BasicNameValuePair;
  10. import org.apache.http.util.EntityUtils;
  11. import org.json.JSONObject;
  12. import java.io.IOException;
  13. import java.net.URISyntaxException;
  14. import java.net.URLEncoder;
  15. import java.util.ArrayList;
  16. import java.util.HashMap;
  17. import java.util.List;
  18. import java.util.Map;
  19. /**
  20. * Created by x on 2017
  21. */
  22. public final class YunPianSms {
  23. //查账户信息的http地址
  24. private static String URI_GET_USER_INFO = "https://sms.yunpian.com/v2/user/get.json";
  25. //智能匹配模版发送接口的http地址
  26. private static String URI_SEND_SMS = "https://sms.yunpian.com/v2/sms/single_send.json";
  27. //模板发送接口的http地址
  28. private static String URI_TPL_SEND_SMS = "https://sms.yunpian.com/v2/sms/tpl_single_send.json";
  29. //发送语音验证码接口的http地址
  30. private static String URI_SEND_VOICE = "https://voice.yunpian.com/v2/voice/send.json";
  31. //编码格式。发送编码格式统一用UTF-8
  32. private static String ENCODING = "UTF-8";
  33. public static JSONObject sendSms(String phone, String content) throws IOException, URISyntaxException {
  34. //修改为您的apikey.apikey可在官网(http://www.yuanpian.com)登录后获取
  35. String apikey = "07784f5fedb508046c841b391005b7de";
  36. //修改为您要发送的手机号
  37. String mobile = URLEncoder.encode(phone, ENCODING);
  38. /**************** 查账户信息调用示例 *****************/
  39. // System.out.println(YunPianSms.getUserInfo(apikey));
  40. /**************** 使用智能匹配模版接口发短信(推荐) *****************/
  41. //设置您要发送的内容(内容必须和某个模板匹配。以下例子匹配的是系统提供的1号模板)
  42. String text = content;
  43. //发短信调用示例
  44. String result = YunPianSms.sendSms(apikey, text, mobile);
  45. JSONObject resultJson = new JSONObject(result);
  46. System.out.println("短信发送结果:" + resultJson.get("msg"));
  47. return resultJson;
  48. /**************** 使用指定模板接口发短信(不推荐,建议使用智能匹配模版接口) *****************/
  49. //设置模板ID,如使用1号模板:【#company#】您的验证码是#code#
  50. // long tpl_id = 1;
  51. //设置对应的模板变量值
  52. // String tpl_value = URLEncoder.encode("#code#",ENCODING) +"="
  53. // + URLEncoder.encode("1234", ENCODING) + "&"
  54. // + URLEncoder.encode("#company#",ENCODING) + "="
  55. // + URLEncoder.encode("云片网",ENCODING);
  56. //模板发送的调用示例
  57. // System.out.println(tpl_value);
  58. // System.out.println(YunPianSms.tplSendSms(apikey, tpl_id, tpl_value, mobile));
  59. /**************** 使用接口发语音验证码 *****************/
  60. // String code = "1234";
  61. //System.out.println(YunPianSms.sendVoice(apikey, mobile ,code));
  62. }
  63. /**
  64. * 取账户信息
  65. *
  66. * @return json格式字符串
  67. * @throws IOException
  68. */
  69. public static String getUserInfo(String apikey) throws IOException, URISyntaxException {
  70. Map<String, String> params = new HashMap<String, String>();
  71. params.put("apikey", apikey);
  72. return post(URI_GET_USER_INFO, params);
  73. }
  74. /**
  75. * 智能匹配模版接口发短信
  76. *
  77. * @param apikey apikey
  78. * @param text  短信内容
  79. * @param mobile  接受的手机号
  80. * @return json格式字符串
  81. * @throws IOException
  82. */
  83. public static String sendSms(String apikey, String text, String mobile) throws IOException {
  84. Map<String, String> params = new HashMap<String, String>();
  85. params.put("apikey", apikey);
  86. params.put("text", text);
  87. params.put("mobile", mobile);
  88. String response = post(URI_SEND_SMS, params);
  89. if (response != null) {
  90. JSONObject json = new JSONObject(response);
  91. return json.getString("msg");
  92. }
  93. return null;
  94. }
  95. /**
  96. * 通过模板发送短信(不推荐)
  97. *
  98. * @param apikey apikey
  99. * @param tpl_id  模板id
  100. * @param tpl_value  模板变量值
  101. * @param mobile  接受的手机号
  102. * @return json格式字符串
  103. * @throws IOException
  104. */
  105. public static String tplSendSms(String apikey, long tpl_id, String tpl_value, String mobile) throws IOException {
  106. Map<String, String> params = new HashMap<String, String>();
  107. params.put("apikey", apikey);
  108. params.put("tpl_id", String.valueOf(tpl_id));
  109. params.put("tpl_value", tpl_value);
  110. params.put("mobile", mobile);
  111. return post(URI_TPL_SEND_SMS, params);
  112. }
  113. /**
  114. * 通过接口发送语音验证码
  115. *
  116. * @param apikey apikey
  117. * @param mobile 接收的手机号
  118. * @param code 验证码
  119. * @return
  120. */
  121. public static String sendVoice(String apikey, String mobile, String code) {
  122. Map<String, String> params = new HashMap<String, String>();
  123. params.put("apikey", apikey);
  124. params.put("mobile", mobile);
  125. params.put("code", code);
  126. return post(URI_SEND_VOICE, params);
  127. }
  128. /**
  129. * 基于HttpClient 4.3的通用POST方法
  130. *
  131. * @param url 提交的URL
  132. * @param paramsMap 提交<参数,值>Map
  133. * @return 提交响应
  134. */
  135. public static String post(String url, Map<String, String> paramsMap) {
  136. CloseableHttpClient client = HttpClients.createDefault();
  137. String responseText = "";
  138. CloseableHttpResponse response = null;
  139. try {
  140. HttpPost method = new HttpPost(url);
  141. if (paramsMap
  142. != null) {
  143. List<NameValuePair> paramList = new ArrayList<NameValuePair>();
  144. for (Map.Entry<String, String> param : paramsMap.entrySet()) {
  145. NameValuePair pair = new BasicNameValuePair(param.getKey(), param.getValue());
  146. paramList.add(pair);
  147. }
  148. method.setEntity(new UrlEncodedFormEntity(paramList, ENCODING));
  149. }
  150. response = client.execute(method);
  151. HttpEntity entity = response.getEntity();
  152. if (entity != null) {
  153. responseText = EntityUtils.toString(entity);
  154. }
  155. } catch (Exception e) {
  156. e.printStackTrace();
  157. } finally {
  158. try {
  159. response.close();
  160. } catch (Exception e) {
  161. e.printStackTrace();
  162. }
  163. }
  164. return responseText;
  165. }
  166. public static void main(String[] args) {
  167. try {
  168. //String content="【申泽智能】您好,机器二次取棍异常报警,机器名:MG301测试机,设备号:aaa863010031925055";
  169. String tpl_value = URLEncoder.encode("#content#", ENCODING) + "=" +
  170. URLEncoder.encode("二次", ENCODING) + "&" + URLEncoder.encode(
  171. "#name#", ENCODING) + "=" + URLEncoder.encode("MG机",
  172. ENCODING) + "&" + URLEncoder.encode("#clientId#", ENCODING) + "=" + URLEncoder.encode("aaa8635", ENCODING);
  173. //您好,机器#content#,机器名:#name#,设备号:#clientId#
  174. System.out.println(tpl_value.length());
  175. String content = "您好,机器二次取棍异常警告,机器名:MG301测试机,设备号:aaa863010031925055";
  176. System.out.println(content.length());
  177. //String result = tplSendSms("07784f5fedb508046c841b391005b7de", 2890698L, tpl_value, "15875317659");
  178. String result = sendSms("07784f5fedb508046c841b391005b7de", content, "15875317659");
  179. JSONObject resultJson = new JSONObject(result);
  180. System.out.println("短信发送结果:" + resultJson.get("msg"));
  181. } catch (IOException e) {
  182. e.printStackTrace();
  183. }
  184. }
  185. }