java实现发送国际短信的功能

由于公司的客户遍布全球,最近会有一些要发送海外的短信的需求,所以今天想说下发送国际短信的功能,接入的手续和一般的短信验证码也差不太多。由于之前已经合作了互亿无线短信平台的短信验证码功能,那么顺理成章看看他们家有没有国际短信,结果对接起来也太方便了,代码也不怎么需要修改,接口改一下好了,很方便,用了个把小时就弄完了,新功能做得飞快,现在送上代码,你们可以参考一下,如果也同样接入互亿无线短信平台的朋友,可以直接用哈哈。

?
/**
* 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;
    }
--------------------- 
作者:hanjun1213 
来源:CSDN 
原文:https://blog.csdn.net/hanjun1213/article/details/84548751 
版权声明:本文为博主原创文章,转载请附上博文链接!

原文地址:https://www.cnblogs.com/feiyunbuyu/p/10021124.html

时间: 2024-08-07 04:00:25

java实现发送国际短信的功能的相关文章

第一次发博,发个简单的Java程序发送手机短信验证

最近在准备一个项目,想的登录时候用手机验证,就通过上网查阅了一下手机验证的实现方法,原来超级简单,下面将一步一步介绍. 1.去中国网建注册一个账号密码,首次注册送五条免费短信和3条免费彩信.具体的网址是 http://www.smschinese.cn/api.shtml 2.注册完成之后进去查看给你的短信秘钥 3.有了这个秘钥就超级简单了,导入jar包,下面的代码第一个基本不用该,直接粘贴,第二个改成自己的信息就可以了 1 package duanxinyanzheng; 2 3 4 impo

ava调用WebService接口实现发送手机短信验证码功能

二:前台的注册页面的代码:reg.jsp  <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags"%> <%@page import="cn.gov.csrc.base.action.FindAllData&

Java --webservice发送验证短信

一.短信平台 --莫名短信 www.duanxin.cm/api.html 该平台可以加后缀和自己定义发送内容,用户接收到的短信示例如下: 123456[圈圈科技] 二.webservice核心代码 这个是springMVC框架下的实现层代码,注释都在里面 @Service public class SendPhoneMessageServiceImpl implements SendPhoneMessageService{          @Autowired     private Mes

JAVA发送手机短信

<p><span>JAVA发送手机短信,流传有几种方法:(1)使用webservice接口发送手机短信,这个可以使用sina提供的webservice进行发送,但是需要进行注册;(2)使用短信mao的方式进行短信的发送,这种方式应该是比较的常用,前提是需要购买硬件设备,呵呵;</span></p> import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient

跟阿根一起学Java Web开发四:邮件发送与短信发送的实现

邮件发送与短信发送常见于用户注册认证以及系统消息提示功能模块,但实现代码过于繁琐:使用JSPGen后,深感其对邮件发送.短信发送的封装让复杂活变得简单很多. 一.基础配置 1.jspgen-config.xml 在框架基础配置文件中,找到mail节点及sms节点,按如下配置: <!-- 邮件服务 --> <mail status="true"> <smtp> <!-- 发送类型(SMTP MX) --> <type>SMTP&

java开通短信验证功能详解

具体步骤: 步骤 1 创建阿里云账号 为了访问短信服务,您需要有一个阿里云账号.如果没有,可首先按照如下步骤创建阿里云账号: 访问阿里云 官方网站,单击页面上的 免费注册 按钮. 按照屏幕提示完成注册流程并进行实名认证,短信服务只支持实名认证用户使用.为了更好地使用阿里云服务,建议尽快完成实名认证,否则部分阿里云服务将无法使用.具体实名认证流程,请参考 这里. 步骤 2 获取阿里云访问密钥 为了使用短信发送API-JAVA SDK,您必须申请阿里云的访问密钥. 阿里云访问秘钥是阿里云为用户使用

利用java实现的一个发送手机短信的小例子

今天闲来无事,在微博上看到一个关于用java实现的一个发送手机短信的程序,看了看,写的不太相信,闲的没事,把他整理下来,以后可能用得着 JAVA发送手机短信,流传有几种方法:(1)使用webservice接口发送手机短信,这个可以使用sina提供的webservice进行发送,但是需要进行注册;(2)使用短信mao的方式进行短信的发送,这种方式应该是比较的常用,前提是需要购买硬件设备,呵呵(3)使用中国网建提供的SMS短信平台(申请账号地址:http://sms.webchinese.cn/de

简单实现发送手机短信

C#简单实现发送手机短信 偶然想起,像编写一个从电脑向手机发送短信的程序,从网上查找到有三种方式:(1)使用webservice接口发送手机短信,这个可以使用sina提供的webservice进行发送,但是需要进行注册;(2)使用短信mao的方式进行短信的发送,这种方式应该是比较的常用,前提是需要购买硬件设备,这个就不考虑了(3)使用中国网建提供的SMS短信平台,但是,用完几条免费的后,就要收费了. 首先,我用C#实现第一种方法,发现总是错误,这个不解,后来从网上查找原因,有的说,新浪这个功能已

C#_发送手机短信

偶然想起,像编写一个从电脑向手机发送短信的程序,从网上查找到有三种方式:(1)使用webservice接口发送手机短信,这个可以使用sina提供的webservice进行发送,但是需要进行注册;(2)使用短信mao的方式进行短信的发送,这种方式应该是比较的常用,前提是需要购买硬件设备,这个就不考虑了(3)使用中国网建提供的SMS短信平台,但是,用完几条免费的后,就要收费了. 首先,我用C#实现第一种方法,发现总是错误,这个不解,后来从网上查找原因,有的说,新浪这个功能已经不用了,我也不太清楚,就