C#对接营销短信API接口代码示例

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text.Json;

class Program
{
    private const string host = "https://api.ihuyi.com";
    private const string path = "/sms-yx/v1/batchSend";
    private const string method = "POST";

    static void Main(string[] args)
    {
        // 定义请求的数据
        var values = new Dictionary<string, object>
        {
        {"api_id": "sms-yx-xxxxxxxx"}, //APIID(用户中心【文本短信】-【会员营销短信】-【产品总览】查看)
        {"signature": "xxxxxxxxx"}, //请求验证加密签名(非短信签名);签名生成方式:仅公共参数以ASCII码从小到大排序值,key=value,多值以“&”隔开,拼接之后md5 32位小写; 如:md5(api_id=xxxx&api_key=xxxx&request_id=xxxxxxxx&timestamp=xxxxxxx)APIKEY(用户中心【文本短信】-【会员营销短信】-【产品总览】查看)2、动态密码(生成动态密码方式请看该文档末尾的说明)
        {"timestamp": 1623643787}, //东八时区;10位时间戳,时间允许相差±60S
        {"request_id": "xxxxxxxxxxxx"}, //请求方请求ID,建议使用唯一ID,比如使用uuid;我方系统会2小时内去重验证处理,防止网络重复攻击;
        {"product_id": 1001}, //产品ID
        {"phone": new List { "18800000000", "18800000001" }}, //手机号数组(最多1万个号码)
        {"sign_name": "xxxxxxxx"}, //短信签名(template_id未填写则必填)
        {"content": "尊敬的会员您好:${name},您的订单号是:${order_no},拒收请回复R"}, //template_id为空时必填;短信内容,如:您的短信群发功能已开通,请在3个工作日之内至平台进行企业认证! 短信内容和模板ID必须传入1个;当短信内容和模板ID都传入时,传入内容生效,模板ID属性失效;
        {"template_id": 1}, //模板ID(内容为空则必填)
        {"template_var": new Dictionary<string, string> { {"${name}", "张三"}, {"${order_no}", "202009041156181103"} }}, //选择模板时,且模板是变量模板时,可以传入变量值,需要传入json格式;
        {"send_time": "2020-08-26 16:08:14"}, //定时发送时间
        };

        // 将数据转换为JSON字符串
        string jsonBody = JsonSerializer.Serialize(values);
        byte[] data = Encoding.UTF8.GetBytes(jsonBody);

        string url = host + path;
        HttpWebRequest httpRequest = null;
        HttpWebResponse httpResponse = null;

        if (host.Contains("https://"))
        {
            ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
            httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
        }
        else
        {
            httpRequest = (HttpWebRequest)WebRequest.Create(url);
        }
        
        httpRequest.Method = method;
        httpRequest.ContentType = "application/json";
        httpRequest.ContentLength = data.Length;
        
        // 将JSON数据写入请求体
        using (Stream stream = httpRequest.GetRequestStream())
        {
            stream.Write(data, 0, data.Length);
        }
        
        try
        {
            httpResponse = (HttpWebResponse)httpRequest.GetResponse();
        }
        catch (WebException ex)
        {
            httpResponse = (HttpWebResponse)ex.Response;
        }

        Console.WriteLine(httpResponse.StatusCode);
        Console.WriteLine(httpResponse.Method);
        Console.WriteLine(httpResponse.Headers);
        Stream st = httpResponse.GetResponseStream();
        StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
        Console.WriteLine(reader.ReadToEnd());
        Console.WriteLine("\n");

        // 释放资源
        reader.Close();
        st.Close();
        if (httpResponse != null) 
            httpResponse.Close();
    }

    public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
    {
        return true;
    }
}