springboot+網建短信通發送短信

Springboot發送短信驗證碼

前言

短信驗證碼是通過發送驗證碼到手機的一種有效的驗證碼系統。主要用於驗證用戶手機的合法性及敏感操作的身份驗證。

第三方短信發送平臺有很多種,各個平臺有各自的優缺點,在選擇的時候可以根據自己的具體實際情況定奪。此篇文章講述了Springboot集成SMS發送短信。

準備

  1.用戶註冊。百度SMS短信通,進入官方頁面,註冊賬號;官網:http://sms.webchinese.cn/

  

  2.登陸獲取必要信息

  

  3.記錄下重要的信息 

   賬號 、密碼、短信密鑰。(編碼時必要)   

        綁定手機、郵箱。(賬號安全)

編碼

1.搭建springboot項目。

2.添加依賴。

<dependencies>
   <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.1.1</version>
   </dependency>
   <dependency>
      <groupId>commons-codec</groupId>
      <artifactId>commons-codec</artifactId>
      <version>1.4</version>
   </dependency>
   <dependency>
      <groupId>commons-httpclient</groupId>
      <artifactId>commons-httpclient</artifactId>
      <version>3.0.1</version>
   </dependency>
</dependencies>                        

3.編寫工具類。

package com.wcl.test.utils;

import java.util.Random;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.junit.Test;

public class SendMessageUtil {
    private static final String SMS_Url = "http://sms.webchinese.cn/web_api/";

    /**
     * @param Uid SMS用戶id  : lvfang123
     * @param Key 接口祕鑰:SMS登錄可查(非登錄密碼)
     * @param sendPhoneNum 短信發送目標號碼
     * @param desc 短信內容
     * @return Integer(1:成功碼,其他失敗,具體參見注釋)
     */
    public static Integer send(String Uid,String Key,String sendPhoneNum,String desc){

        HttpClient client = new HttpClient();
        PostMethod post = new PostMethod(SMS_Url);
        post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=gbk");// 在頭文件中設置轉碼

        //設置參數
        NameValuePair[] data = {
                new NameValuePair("Uid", Uid),
                new NameValuePair("Key", Key),//祕鑰
                new NameValuePair("smsMob", sendPhoneNum),
                new NameValuePair("smsText", desc)
        };

        post.setRequestBody(data);

        try {
            client.executeMethod(post);
        } catch (Exception e) {  e.printStackTrace();  }


        Header[] headers = post.getResponseHeaders();
        int statusCode = post.getStatusCode();
        System.out.println("statusCode:" + statusCode);
        for (Header h : headers) {
            System.out.println(h.toString());
        }

        String result ="";
        try {
            result = new String(post.getResponseBodyAsString().getBytes("gbk"));
        } catch (Exception e) {  e.printStackTrace();  }

        post.releaseConnection();

        return Integer.parseInt(result);
    }
    /**
     *  -1  沒有該用戶賬戶
     -2 接口密鑰不正確 [查看密鑰]不是賬戶登陸密碼
     -21    MD5接口密鑰加密不正確
     -3 短信數量不足
     -11    該用戶被禁用
     -14    短信內容出現非法字符
     -4 手機號格式不正確
     -41    手機號碼爲空
     -42    短信內容爲空
     -51    短信簽名格式不正確接口簽名格式爲:【簽名內容】
     -6 IP限制
     大於0    短信發送數量
     以上作爲補充
     */
    public static String getMessage(Integer code){
        String message;
        if(code > 0 ) {
            message = "SMS-f發送成功!短信量還有" + code + "條";
        }else if(code == -1){
            message = "SMS-沒有該用戶賬戶";
        }else if(code == -2){
            message = "SMS-接口密鑰不正確";
        }else if(code == -21){
            message = "SMS-MD5接口密鑰加密不正確";
        }else if(code == -3){
            message = "SMS-短信數量不足";
        }else if(code == -11){
            message = "SMS-該用戶被禁用";
        }else if(code == -14){
            message = "SMS-短信內容出現非法字符";
        }else if(code == -4){
            message = "SMS-手機號格式不正確";
        }else if(code == -41){
            message = "SMS-手機號碼爲空";
        }else if(code == -42){
            message = "SMS-短信內容爲空";
        }else if(code == -51){
            message = "SMS-短信簽名格式不正確接口簽名格式爲:【簽名內容】";
        }else if(code == -6){
            message = "SMS-IP限制";
        }else{
            message = "其他錯誤";
        }
        return message;
    }

    /**
     * 隨機生成6位驗證碼
     * @return
     */
    public static String getRandomCode(Integer code){
        Random random = new Random();
        StringBuffer result= new StringBuffer();
        for (int i=0;i<code;i++){
            result.append(random.nextInt(10));
        }
        return result.toString();
    }
    @Test
    public void testSendMessage(){
        //SendMessageUtil.send("SMS賬戶","接口祕鑰","目標號碼","發送內容");
        Integer resultCode = SendMessageUtil.send("疾風***","d41d8cd98f**********","159********","驗證碼:"+getRandomCode(6)); 
        System.out.println(SendMessageUtil.getMessage(resultCode)); 
  } 
}

4.測試。

//SendMessageUtil.send("SMS賬戶","接口祕鑰","目標號碼","發送內容");
使用junit測試,正確填寫send方法的參數。無論成功失敗都會返回響碼,根據響應碼判斷是否成功,如果失敗也可查出失敗原因。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章