当前位置: 移动技术网 > IT编程>开发语言>Java > Java实现短信验证码和国际短信群发功能的示例

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

2019年07月22日  | 移动技术网IT编程  | 我要评论

三国之我是皇太子txt,大学入党申请书,新西游记蜘蛛精演员

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

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

/**
* 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!

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网