136 lines
5.0 KiB
C#
136 lines
5.0 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// 发送短信
|
|
/// </summary>
|
|
public class SendSMSService
|
|
{
|
|
private static string smsApi = "http://dysmsapi.aliyuncs.com";
|
|
private static string smsAuthorization = "Basic cGJsOjEyMzQ1NmFh";
|
|
private static string smsAppId = smsAppId = "weiyuwuye";
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 发送短信
|
|
/// </summary>
|
|
/// <param name="Content">发送内容</param>
|
|
/// <param name="mobile">多个手机号逗号分隔</param>
|
|
/// <returns></returns>
|
|
public async static Task<SMSDataResponse> 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<string, string> headers = new Dictionary<string, string>();
|
|
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<SMSDataResponse>(res) ;
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// post请求
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <param name="postData"></param>
|
|
/// <param name="contentType"></param>
|
|
/// <param name="timeOut"></param>
|
|
/// <param name="headers"></param>
|
|
/// <returns></returns>
|
|
public static string HttpPost(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> 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<string> HttpPostAsync(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> 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<T>
|
|
{
|
|
/// <summary>
|
|
/// 业务状态码
|
|
/// </summary>
|
|
public string Code { get; set; }
|
|
|
|
/// <summary>
|
|
/// 业务消息,如:操作失败消息
|
|
/// </summary>
|
|
public string Message { get; set; }
|
|
|
|
/// <summary>
|
|
/// 业务实体数据
|
|
/// </summary>
|
|
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; }
|
|
}
|
|
}
|
|
}
|