初始提交
This commit is contained in:
46
Infrastructure/Hncore.Infrastructure/SMS/AliSmsService.cs
Normal file
46
Infrastructure/Hncore.Infrastructure/SMS/AliSmsService.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using Aliyun.Acs.Core;
|
||||
using Aliyun.Acs.Core.Exceptions;
|
||||
using Aliyun.Acs.Core.Http;
|
||||
using Aliyun.Acs.Core.Profile;
|
||||
using Hncore.Infrastructure.Serializer;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
namespace Hncore.Infrastructure.SMS
|
||||
{
|
||||
/// <summary>
|
||||
/// 发送短信
|
||||
/// </summary>
|
||||
public class AliSmsService
|
||||
{
|
||||
public static bool Send(string TemplateCode,object TemplateParam, string SignName="", params string[] PhoneNumbers)
|
||||
{
|
||||
IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", "LTAI4FmSkDSwFuXeLxsDB3jB", "r8FfRmoeWcCJyZSqqkQP2G3dKPPl2N");
|
||||
DefaultAcsClient client = new DefaultAcsClient(profile);
|
||||
CommonRequest request = new CommonRequest();
|
||||
request.Method = MethodType.POST;
|
||||
request.Domain = "dysmsapi.aliyuncs.com";
|
||||
request.Version = "2017-05-25";
|
||||
request.Action = "SendSms";
|
||||
// request.Protocol = ProtocolType.HTTP;
|
||||
request.AddQueryParameters("PhoneNumbers", string.Join(",", PhoneNumbers));
|
||||
request.AddQueryParameters("SignName", SignName);
|
||||
request.AddQueryParameters("TemplateCode", TemplateCode);
|
||||
request.AddQueryParameters("TemplateParam", TemplateParam.ToJson());
|
||||
try
|
||||
{
|
||||
CommonResponse response = client.GetCommonResponse(request);
|
||||
Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
|
||||
return true;
|
||||
}
|
||||
catch (ServerException e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
catch (ClientException e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
135
Infrastructure/Hncore.Infrastructure/SMS/SendSMSService.cs
Normal file
135
Infrastructure/Hncore.Infrastructure/SMS/SendSMSService.cs
Normal file
@@ -0,0 +1,135 @@
|
||||
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; }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user