using System;
using System.Text;
using System.IO;
using System.Net;
using System.Data;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
namespace FangYar.FYMQTT
{
public class RemindMessages
{
public static string ShortLetterUrl = System.Configuration.ConfigurationManager.AppSettings["ShortLetterUrl"].ToString();
///
/// 单人发送短信
///
/// 用户手机号
/// 模版id
/// 这个模版中的参数,根据模版内容进行替换
public string AloneVerificationCodePush(string phone, string templateCode, string param)
{
try
{
string param_s = "{\"templateCode\":\"" + templateCode + "\",\"phone\":[\"" + phone + "\"],\"param\":" + param + "}";
return InvokeWebApiPost(ShortLetterUrl, param_s);
}
catch (Exception e)
{
return "";
}
}
///
/// 调用指定的url以post方式调用,默认超时时间为3000毫秒。
///
/// 待调用链接。
/// 参数列表,格式如 api=value1&arg1=value2
/// 超时时间(单位毫秒),默认值为3000。
/// 执行结果。
public static string InvokeWebApiPost(string url, string param, int timeout = 3000)
{
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var resuleJson = string.Empty;
var req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/json"; //Defalt
req.Accept = "accept";
using (var postStream = new StreamWriter(req.GetRequestStream()))
{
postStream.Write(param);
}
req.GetResponse();
return "";
//System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
//// 将参数字节化
//byte[] postData = Encoding.UTF8.GetBytes(param);
//var resuleJson = string.Empty;
//var req = (HttpWebRequest)WebRequest.Create(url);
//req.Method = "POST";
//req.ContentType = "application/json"; //Defalt
//req.Accept = "accept";
//req.ContentLength = postData.Length;
//using (var postStream = req.GetRequestStream())
//{
// //postStream.Write(param);
// //传输字节数据
// postStream.Write(postData, 0, postData.Length);
//}
//req.GetResponse();
//return "";
}
}
}