返回介绍

C# DEMO

发布于 2024-06-22 00:52:22 字数 4160 浏览 0 评论 0 收藏 0

功能说明:该接口要求提前在云片后台添加模板,提交短信时,系统会自动匹配审核通过的模板,匹配成功任意一个模板即可发送。系统已提供的默认模板添加签名后可以直接使用。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace yunpianSmsClient
{
    class Program
    {
        static void Main(string[] args)
        {
            // 设置为您的apikey(https://www.yunpian.com)可查
            string apikey = "xxxxxxxxxxxxxxxxxxxxxxx";
            // 发送的手机号
            string mobile = "xxxxxxxxxxxxxxxxxxxxxxx";
            // 发送模板编号
            int tpl_id = 376841;
            // 发送模板内容
            mobile = HttpUtility.UrlEncode(mobile, Encoding.UTF8);
            string tpl_value = HttpUtility.UrlEncode(
            HttpUtility.UrlEncode("#code#", Encoding.UTF8)    + "=" +
            HttpUtility.UrlEncode("1234", Encoding.UTF8)      + "&" +
            HttpUtility.UrlEncode("#min#", Encoding.UTF8) + "=" +
            HttpUtility.UrlEncode("5", Encoding.UTF8), Encoding.UTF8);
            // 发送内容
            string text = "【云片网】您的验证码是1234,5分钟内有效。";
            // 获取user信息url
            string url_get_user     = "https://sms.yunpian.com/v2/user/get.json";
            // 智能模板发送短信url
            string url_send_sms     = "https://sms.yunpian.com/v2/sms/single_send.json";
            // 指定模板发送短信url
            string url_tpl_sms      =
                "https://sms.yunpian.com/v2/sms/tpl_single_send.json";
            // 发送语音短信url
            string url_send_voice   = "https://voice.yunpian.com/v2/voice/send.json";

            string data_get_user = "apikey=" + apikey;
            string data_send_sms = "apikey=" + apikey + "&mobile=" + mobile + "&text=" +
                text;
            string data_tpl_sms = "apikey=" + apikey + "&mobile=" + mobile +
                "&tpl_id=" + tpl_id.ToString() + "&tpl_value=" + tpl_value;
            string data_send_voice = "apikey=" + apikey + "&mobile=" + mobile +
                "&code=" + "1234";

            HttpPost(url_get_user, data_get_user);
            HttpPost(url_send_sms, data_send_sms);
            HttpPost(url_tpl_sms, data_tpl_sms);
            HttpPost(url_send_voice, data_send_voice);
        }
        public static void HttpPost(string Url, string postDataStr)
        {
            byte[] dataArray = Encoding.UTF8.GetBytes(postDataStr);
           // Console.Write(Encoding.UTF8.GetString(dataArray));

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = dataArray.Length;
            //request.CookieContainer = cookie;
            Stream dataStream = request.GetRequestStream();
            dataStream.Write(dataArray, 0, dataArray.Length);
            dataStream.Close();
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader reader =
                    new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                String res = reader.ReadToEnd();
                reader.Close();
                Console.Write("\nResponse Content:\n" + res + "\n");
            }
            catch(WebException e)
            {
                Console.Write(e.Message + e.ToString());
                Stream stream = e.Response.GetResponseStream();

                StreamReader reader =
                    new StreamReader(stream, Encoding.UTF8);
                String res = reader.ReadToEnd();
                reader.Close();
                Console.Write("\nResponse Content:\n" + res + "\n");
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文