云平台短信验证码通知短信java/php/.net开发实现

发布时间:2020-03-03 22:34:41 作者:cyzshenzhen
来源:网络 阅读:241

云平台短信验证码通知短信java/php/.net开发实现

一、本文目的

二、发送短信接口请求

参数名 说明
url 请求地址
app_key 客户在供应商注册的唯一key码
app_secret 客户在供应商注册的唯一secret码
mobile 接收短信的用户手机号
sign 短信内容的产品名称

四、各开发语言实现

1、Java

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 短信发送工具类

 * <p>
 *      http接口请求依赖 okhttp-3.14.2.jar, okio-1.17.2.jar
 *</p>
 */
public class SmsUtil {

    private static final String HOST = "https://api.zhuanxinyun.com/api/v2/sendSms.json";
    private static final String SIGN = "【签名】";
    private static final String APP_KEY = "app_key";
    private static final String APP_SECRET = "app_secret";

    /**
     * 测试请求
     * @param args
     */
    public static void main(String[] args) {
        String mobile = "18566770000";
        String code = "666666";
        String body = sendSmsCode(mobile, code);
        System.out.println(body);
    }

    /**
     * 发送短信验证码
     * @param mobile        接收手机号
     * @param code          验证码
     */
    public static String sendSmsCode(String mobile, String code) {
        StringBuffer content = new StringBuffer().append("验证码:" ).append(code).append(",如非本人操作请忽略。");
        return sendSms(mobile, content.toString());
    }

    /**
     * 发送短信信息
     * @param mobile        接收手机号
     * @param content       短信内容
     */
    public static String sendSms(String mobile, String content) {
        // 拼接请求参数
        StringBuffer url = new StringBuffer().append(HOST).append("?appKey=").append(APP_KEY).append("&appSecret=")
                .append(APP_SECRET).append("&phones=").append(mobile).append("&content=").append(SIGN).append(content);
        // 封装请求参数
        Request request = new Request.Builder().url(url.toString()).get().build();
        OkHttpClient client = new OkHttpClient();
        try {
            // 发起请求
            Response response = client.newCall(request).execute();
            return response.body().string();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

}

2、php

<?php
        $host = "https://api.zhuanxinyun.com/api/v2/sendSms.json";
        $sign = "【签名】";
        $app_key = "app_key";
        $app_secret = "app_secret";
        $code = "6666";
        $phones = "18088888888";
        $content = "本次验证码是".code.",如非本人操作请忽略。";
        $uri = $host."?appKey=".$app_key."&appSecret=".$app_secret."&phones=".$phones."&content=".$sign.$content;
        $ch = curl_init();
        $timeout = 5;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $contents = curl_exec($ch);
        curl_close($ch);
        var_dump($contents);//输入返回内容
?>

3、.Net

using System;
using System.IO;
using System.Net;
using System.Text;

namespace SmsCode
{
    class Program
    {
        private static  String HOST = "https://api.zhuanxinyun.com/api/v2/sendSms.json";
        private static  String SIGN = "【签名】";
        private static  String APP_KEY = "app_key";
        private static  String APP_SECRET = "APP_SECRET";

        static void Main(string[] args)
        {
            string mobile = "手机号";
            string code = "内容";
            string body = sendSmsCode(mobile, code);

            Console.WriteLine(body);
        }
        /**
        * 发送短信验证码
        * @param mobile     接收手机号
        * @param code           验证码
        */
        public static String sendSmsCode(String mobile, String code)
        {
            StringBuilder content = new StringBuilder().Append("验证码:").Append(code).Append(",如非本人操作请忽略。");
            return sendSms(mobile, content.ToString());

           // StringBuilder
        }

        /**
        * 发送短信信息
        * @param mobile     接收手机号
        * @param content        短信内容
        */
        public static string sendSms(String mobile, String content)
        {
            string msg = string.Empty;
            // 拼接请求参数
            StringBuilder url = new StringBuilder().Append(HOST).Append("?appKey=").Append(APP_KEY).Append("&appSecret=")
                    .Append(APP_SECRET).Append("&phones=").Append(mobile).Append("&content=").Append(SIGN).Append(content);
            // 封装请求参数
            try
            {
                // 发起请求

                 msg = Request_GET(url.ToString(), "UTF-8");

            }
            catch (Exception ex)
            {
                msg=ex.Message;
            }

            return msg;
        }

        public static string Request_GET(string urlString, string encoding)
        {
            HttpWebRequest httpWebRequest = null;
            HttpWebResponse httpWebResponse = null;
            Stream stream = null;
            StreamReader streamReader = null;
            string result = string.Empty;
            try
            {
                httpWebRequest = (WebRequest.Create(urlString) as HttpWebRequest);
                httpWebRequest.Method = "GET";
                //httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; Maxthon 2.0)";
                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                stream = httpWebResponse.GetResponseStream();
                streamReader = new StreamReader(stream, Encoding.GetEncoding(encoding));
                result = streamReader.ReadToEnd();
            }
            catch (SystemException ex)
            {
                result = "err:" + ex.Message;
            }
            finally
            {
                if (httpWebRequest != null)
                {
                    httpWebRequest.Abort();
                }
                if (httpWebResponse != null)
                {
                    httpWebResponse.Close();
                }
                if (stream != null)
                {
                    stream.Close();
                }
                if (streamReader != null)
                {
                    streamReader.Close();
                }
            }
            return result;
        }
    }
}

五、返回值

{"errorCode":"000000","errorMsg":"提交成功"}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xml>
    <errorCode>000000</errorCode>
    <errorMsg>提交成功</errorMsg>
</xml>

技术交流

云平台短信验证码通知短信java/php/.net开发实现

My Blog

blog.guijianpan.com

推荐阅读:
  1. 【短信插件】 NiuShop单商户如何调用短信验证码接口
  2. 【源码分享】短信验证码如何实现微米CMS

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

短信通道 短信验证码 ava

上一篇:自建git服务器连接Pycharm系列三:在Win10上,安

下一篇:Azure Storage系列之生命周期管理

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》