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 = "/mms/v1/batchSend";
private const string method = "POST";
static void Main(string[] args)
{
// 定义请求的数据
var values = new Dictionary<string, object>
{
{"api_id": "mms-xxxxxxxx"}, //APIID(用户中心【富媒体短信】-【5G视频短信】-【产品总览】查看)
{"signature": "xxxxxxxxx"}, //请求验证加密签名(非彩信签名);
签名生成方式:
仅公共参数以ASCII码从小到大排序值,key=value,多值以“&”隔开,拼接之后md5 32位小写; 如:md5(api_id=xxxx&api_key=xxxx&request_id=xxxxxxxx×tamp=xxxxxxx)
APIKEY(用户中心【富媒体短信】-【5G视频短信】-【产品总览】查看)
2、动态密码(生成动态密码方式请看该文档末尾的说明)
{"timestamp": 1623643787}, //东八时区;10位时间戳,时间允许相差±60S
{"request_id": "xxxxxxxxxxxx"}, //请求方请求ID,建议使用唯一ID,比如使用uuid;我方系统会2小时内去重验证处理,防止网络重复攻击;
{"product_id": 1001}, //产品ID
{"phone": new List<string> { "18800000000", "18800000001" }}, //手机号数组(最多1万个号码)
{"sign_name": "xxxxxxxx"}, //彩信签名(template_id未填写则必填)
{"title": "xxxxxxxxxxxx"}, //彩信标题(template_id未填写则必填)
{"content": new List<Dictionary<string, string>> {
new Dictionary<string, string> { {"con_type", "txt"}, {"ext_type", ""}, {"data", "将原始内容base64编码"} },
new Dictionary<string, string> { {"con_type", "img"}, {"ext_type", "jpg"}, {"data", "将原始内容base64编码"} }
}}, //彩信内容和模板ID必须传入1个;当彩信内容和模板ID都传入时,传入内容生效,模板ID属性失效 彩信元素DataItem结构:(具体参照文档9.01)
{"template_id": 1}, //模板ID(内容为空则必填)
{"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;
}
}
在线咨询
150,000家
企业客户
21年
行业经验
2V1
2对1客户支持