An example of Java implementing SMS verification code and international SMS group sending function

  • 2020-06-03 06:34:22
  • OfStack

Recently due to the company's business development, need to foreign users to send international SMS, countries such as Spain, Portugal, Italy, all want to send, and China's Hong Kong, Macao, Taiwan, Hong Kong, Macao and Taiwan) also want to send these areas, but now there are many companies provide international business messages, use before the clouds captcha business, by the way, could you see they have international SMS business, and more importantly, do not need to modify any code, just add the international message template, you can directly use the previous code to send international SMS, it is too easy.

Without further ado, just code.


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

/**
*  SMS http Of the interface java Code invocation example 
*  Based on the Apache HttpClient 4.3
*
* @author songchao
* @since 2015-04-03
*/

public class JavaSmsApi {

  // Checking account information http address 
  private static String URI_GET_USER_INFO = "https://sms.yunpian.com/v2/user/get.json";

  // Smart match template send interface http address 
  private static String URI_SEND_SMS = "https://sms.yunpian.com/v2/sms/single_send.json";

  // Template send interface http address 
  private static String URI_TPL_SEND_SMS = "https://sms.yunpian.com/v2/sms/tpl_single_send.json";

  // Sending voice verification code interface http address 
  private static String URI_SEND_VOICE = "https://voice.yunpian.com/v2/voice/send.json";

  // The interface that binds the calling/called relationship http address 
  private static String URI_SEND_BIND = "https://call.yunpian.com/v2/call/bind.json";

  // The interface that unties the caller/caller relationship http address 
  private static String URI_SEND_UNBIND = "https://call.yunpian.com/v2/call/unbind.json";

  // Coding format. Send encoding system 1 with UTF-8
  private static String ENCODING = "UTF-8";

  public static void main(String[] args) throws IOException, URISyntaxException {

    // Change to yours apikey.apikey Available on the official website ( http://www.yunpian.com) Get after logging in 
    String apikey = "xxxxxxxxxxxxxxxxxxxxx";

    // Change to the phone number you want to send 
    String mobile = "130xxxxxxxx";

    /****************  Account check information invocation example  *****************/
    System.out.println(JavaSmsApi.getUserInfo(apikey));

    /****************  Text using the Smart Match template interface ( recommended ) *****************/
    // Set what you want to send ( The content must match a template. The following examples match what the system provides 1 The template) 
    String text = " [Cloud Chip network] Your verification code is 1234";
    // Text message call example 
    // System.out.println(JavaSmsApi.sendSms(apikey, text, mobile));

    /****************  Text using the specified template interface ( Smart match template interface is not recommended ) *****************/
    // Set the template ID , such as using 1 The template : 【 #company# Your captcha is #code#
    long tpl_id = 1;
    // Set the value of the corresponding template variable 

    String tpl_value = URLEncoder.encode("#code#",ENCODING) +"="
    + URLEncoder.encode("1234", ENCODING) + "&"
    + URLEncoder.encode("#company#",ENCODING) + "="
    + URLEncoder.encode(" Network clouds ",ENCODING);
    // A sample call sent by the template 
    System.out.println(tpl_value);
    System.out.println(JavaSmsApi.tplSendSms(apikey, tpl_id, tpl_value, mobile));

    /****************  Use the interface to send a voice verification code  *****************/
    String code = "1234";
    //System.out.println(JavaSmsApi.sendVoice(apikey, mobile ,code));

    /****************  Use the interface to bind the primary called number  *****************/
    String from = "+86130xxxxxxxx";
    String to = "+86131xxxxxxxx";
    Integer duration = 30*60;//  The binding 30 minutes 
    //    System.out.println(JavaSmsApi.bindCall(apikey, from ,to , duration));

    /****************  Unbind the host called number using the interface  *****************/
    //    System.out.println(JavaSmsApi.unbindCall(apikey, from, to));
  }

  /**
  *  Access account information 
  *
  * @return json Format string 
  * @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);
  }

  /**
  *  Intelligent matching template interface to send text messages 
  *
  * @param apikey apikey
  * @param text   Message content 
  * @param mobile  Accepted cell phone number 
  * @return json Format string 
  * @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);
  }

  /**
  *  Send text messages via templates ( Is not recommended )
  *
  * @param apikey  apikey
  * @param tpl_id   The template id
  * @param tpl_value  Template variable value 
  * @param mobile   Accepted cell phone number 
  * @return json Format string 
  * @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);
  }

  /**
  *  Send a voice verification code through the interface 
  * @param apikey apikey
  * @param mobile  Received cell phone number 
  * @param code   Verification 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);
  }

  /**
  *  Bind the primary called number through the interface 
  * @param apikey apikey
  * @param from  calling 
  * @param to   Was called 
  * @param duration  Effective time, unit: seconds 
  * @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);
  }

  /**
  *  Unbind the primary called number through the interface 
  * @param apikey apikey
  * @param from  calling 
  * @param to   Was called 
  * @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);
  }

  /**
  *  Based on the HttpClient 4.3 The general POST methods 
  *
  * @param url     The submitted URL
  * @param paramsMap  submit < Parameter, the value >Map
  * @return  The submission response 
  */

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

Code looks a bit messy, but we use API interface also few, specific to see how to use this article API clouds send message authentication code, as long as the three interfaces done, both domestic and international SMS text messages and message authentication code, mobile phone verification code, can be easily calm, so easy!


Related articles: