Java realizes SMS function through SMS platform

Preface

This paper records the use of SMS platform built by China net, which needs to be registered before use. Here, only the use of SMS interface is recorded.

1. Login http://sms.webchinese.cn/User/?action=key Website registration.

2. Log in directly after registration.

3. After successful login, first find the secret key of SMS, which is required by the interface.

After the secret key is found, the signature content should be filled in first, so that the signature will be verified after the interface is called. The website also has API calling cases (SMS API menu).

4. POM introduction

        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.10</version>
        </dependency>

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
        </dependency>

5. SendMsgUtil utility class.

This tool class uses GBK code sending interface, and each registered user has 5 free ones.

GBK code sending interface address:
http://gbk.sms.webchinese.cn/?Uid = user name & key = interface security password & smsmob = mobile number & smstext = SMS content
UTF-8 code sending interface address:
http://utf8.sms.webchinese.cn/?Uid = user name & key = interface security password & smsmob = mobile number & smstext = SMS content
Get the number of SMS interface address (UTF8):
Http: / / SMS. Webchinese. CN / Web? API / SMS /? Action = SMS? Num & uid = local user name & key = interface security key
Get the number of SMS interface address (GBK):
Http: / / SMS. Webchinese. CN / Web? API / SMS / GBK /? Action = SMS? Num & uid = local user name & key = interface security password

package com.example.mybaties.utils;

import lombok.extern.slf4j.Slf4j;
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 java.io.IOException;
import java.util.HashMap;

/**
 * @author LST
 * @version 1.0
 * @Description: SMS sending class
 * @date 2020-1-16 14:45
 */
@Slf4j
public class SendMsgUtil {

    public static HashMap<String,String> getMessageStatus(String phone){
        HashMap<String,String> map = new HashMap<String,String>();

        HttpClient client = new HttpClient();

        PostMethod post = new PostMethod("http://GBK. SMS. Webchinese. CN "; / / SMS platform interface
        post.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=gbk");//Format encoding set in header file

        int num = (int)((Math.random()*9+1)*100000);//6-digit verification code
        String code= String.valueOf(num);

        NameValuePair[] data ={new NameValuePair("Uid", "User name"),//User name of sms platform
                new NameValuePair("Key", "SMS secret key"),//SMS secret key, not password
                new NameValuePair("smsMob",phone),//Destination phone number, multiple phone numbers separated by half comma
                new NameValuePair("smsText","The verification code is:"+code+""+",Valid for 5 minutes")//SMS content, custom
                 };
        map.put("code", code);
        post.setRequestBody(data);

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

        //Print status code and response header information
        int statusCode = post.getStatusCode();
        log.info("statusCode:{}",statusCode);
        Header[] headers = post.getResponseHeaders();
        for(Header h : headers) {
            log.info(h.toString());
        }

        String result = null;
        try {
            result = new String(post.getResponseBodyAsString().getBytes("gbk"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        log.info(result); //Print returned message status
        map.put("result", result);

        post.releaseConnection();
        return map;
    }

    public static void main(String[] args) {
        getMessageStatus("Phone number");
    }

}

Below is the text sent to my mobile phone

6. Return to code

Return value after SMS sending Speak clearly
-1 No such user account
-2 Incorrect key (not user password)
-3 Insufficient number of SMS
-11 The user is disabled
-14 Illegal characters in SMS content
-4 Incorrect phone number format
-41 Mobile number is empty
-42 SMS content is empty
Greater than 0 Number of SMS sent

 

 

 

Published 13 original articles, won praise 7, visited 520
Private letter follow

Tags: Mobile Apache codec Java

Posted on Thu, 16 Jan 2020 04:27:50 -0500 by spyke01