栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java实现短信验证码和国际短信群发功能的示例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java实现短信验证码和国际短信群发功能的示例

最近由于公司的业务拓展,需要给国外用户发送国际短信,像西班牙、葡萄牙、意大利这些国家都要发,还有中国的香港、澳门、台湾(港澳台)这些地区也要发,不过现在已经有许多公司提供国际短信的业务了,之前使用过云片的验证码业务,顺便看到他们也有国际短信的业务,并且更重要的是,不需要修改任何代码,只要添加下国际短信模板,就可以直接使用之前的代码继续发送国际短信,简直太方便了。

废话不多说,直接上代码。



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;



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));
  }

  

  public static String getUserInfo(String apikey) throws IOException, URISyntaxException {
    Map params = new HashMap();
    params.put("apikey", apikey);
    return post(URI_GET_USER_INFO, params);
  }

  

  public static String sendSms(String apikey, String text, String mobile) throws IOException {
    Map params = new HashMap();
    params.put("apikey", apikey);
    params.put("text", text);
    params.put("mobile", mobile);
    return post(URI_SEND_SMS, params);
  }

  

  public static String tplSendSms(String apikey, long tpl_id, String tpl_value, String mobile) throws IOException {
    Map params = new HashMap();
    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);
  }

  

  public static String sendVoice(String apikey, String mobile, String code) {
    Map params = new HashMap();
    params.put("apikey", apikey);
    params.put("mobile", mobile);
    params.put("code", code);
    return post(URI_SEND_VOICE, params);
  }

  

  public static String bindCall(String apikey, String from, String to , Integer duration ) {
    Map params = new HashMap();
    params.put("apikey", apikey);
    params.put("from", from);
    params.put("to", to);
    params.put("duration", String.valueOf(duration));
    return post(URI_SEND_BIND, params);
  }

  
  public static String unbindCall(String apikey, String from, String to) {
    Map params = new HashMap();
    params.put("apikey", apikey);
    params.put("from", from);
    params.put("to", to);
    return post(URI_SEND_UNBIND, params);
  }

  

  public static String post(String url, Map paramsMap) {
    CloseableHttpClient client = HttpClients.createDefault();
    String responseText = "";
    CloseableHttpResponse response = null;
      try {
 HttpPost method = new HttpPost(url);
 if (paramsMap != null) {
   List paramList = new ArrayList();
   for (Map.Entry 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!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/147815.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号