123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- package com.kmall.api.util;
- import com.kmall.manager.manager.common.CommonPropertiesBuilder;
- 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.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- /**
- * @author huangyaqin
- * @version 1.0
- * 2018-10-16 11:27
- */
- public class SendMsgUtil {
- //编码格式。发送编码格式统一用UTF-8
- private static String ENCODING = "UTF-8";
- // 请求地址
- private static String SMS_URL = "https://sms.yunpian.com/v2/sms/single_send.json";
- // 中网cw apikey
- private static String API_KEY = "1dd75986321871d706334e60b89c4021";
- /**
- * 发送短信消息
- * 方法说明
- * @Discription:扩展说明
- * @param phones
- * @param text
- * @return
- * @return String
- */
- @SuppressWarnings("deprecation")
- public static String sendMsg(String phones,String text) {
- System.out.println(text);
- String apikey = API_KEY;
- String url = SMS_URL;
- if(apikey != null && url != null){
- Map<String, String> params = new HashMap<String, String>();
- params.put("apikey", apikey);
- //手机号码,多个号码使用英文逗号进行分割
- params.put("mobile", phones);
- //将短信内容进行URLEncoder编码
- params.put("text", text);
- return post(url, params);
- }else{
- return "";
- }
- }
- /**
- * 随机生成6位随机验证码
- * 方法说明
- * @Discription:扩展说明
- * @return
- * @return String
- */
- public static String createRandomVcode(){
- //验证码
- String vcode = "";
- for (int i = 0; i < 6; i++) {
- vcode = vcode + (int)(Math.random() * 9);
- }
- return vcode;
- }
- /**
- * 基于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;
- }
- /**
- * 测试
- * 方法说明
- * @Discription:扩展说明
- * @param args
- * @return void
- */
- public static void main(String[] args) {
- // System.out.println(SendMsgUtil.createRandomVcode());
- // System.out.println("&ecb=12".substring(1));
- System.out.println(sendMsg("13229855975", "【CW惠州门店】惠州港惠店 尊敬的CW会员,您购买的订单56151,已清关成功,感谢您的耐心等待。"));
- }
- }
|