using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Hncore.Infrastructure.Common;
using Hncore.Infrastructure.Serializer;
using Newtonsoft.Json;
namespace Hncore.Infrastructure.SMS
{
///
/// 发送短信
///
public class SendSMSService
{
private static string smsApi = "http://dysmsapi.aliyuncs.com";
private static string smsAuthorization = "Basic cGJsOjEyMzQ1NmFh";
private static string smsAppId = smsAppId = "weiyuwuye";
///
/// 发送短信
///
/// 发送内容
/// 多个手机号逗号分隔
///
public async static Task SendSMS(string Content, string mobile)
{
if (string.IsNullOrEmpty(Content) || string.IsNullOrEmpty(mobile))
{
throw new ArgumentException("SendSMS参数错误");
}
mobile = mobile.Replace(';', ',');
var postData = new SMSData() {
Mobile=mobile,
SmsType=4,
Content=Content
};
Dictionary headers = new Dictionary();
headers.Add("Authorization", smsAuthorization);
headers.Add("AppId", smsAppId);
var res= await HttpPostAsync(smsApi, JsonConvert.SerializeObject(postData), "application/json", 30, headers);
// return response.Data; {"Code":"100000","Message":"发送成功","Data":null}
return JsonConvert.DeserializeObject(res) ;
}
///
/// post请求
///
///
///
///
///
///
///
public static string HttpPost(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary headers = null)
{
postData = postData ?? "";
using (HttpClient client = new HttpClient())
{
if (headers != null)
{
foreach (var header in headers)
client.DefaultRequestHeaders.Add(header.Key, header.Value);
}
using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8))
{
if (contentType != null)
httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
HttpResponseMessage response = client.PostAsync(url, httpContent).Result;
return response.Content.ReadAsStringAsync().Result;
}
}
}
public static async Task HttpPostAsync(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary headers = null)
{
postData = postData ?? "";
using (HttpClient client = new HttpClient())
{
client.Timeout = new TimeSpan(0, 0, timeOut);
if (headers != null)
{
foreach (var header in headers)
client.DefaultRequestHeaders.Add(header.Key, header.Value);
}
using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8))
{
if (contentType != null)
httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
HttpResponseMessage response = await client.PostAsync(url, httpContent);
return await response.Content.ReadAsStringAsync();
}
}
}
public class APIResponse
{
///
/// 业务状态码
///
public string Code { get; set; }
///
/// 业务消息,如:操作失败消息
///
public string Message { get; set; }
///
/// 业务实体数据
///
public T Data { get; set; }
}
public class SMSData
{
public string Mobile { get; set; }
public string Content { get; set; }
public int SmsType { get; set; }
}
public class SMSDataResponse
{
public int Code { get; set; }
public string Message { get; set; }
public object Data { get; set; }
}
}
}