.NET CORE3.1实现微信小程序发送订阅消息

一、appsettings.json定义小程序配置信息

"WX": {
  "AppId": "wx88822730803edd44",
  "AppSecret": "75b269042e8b5026e6ed14aa24ba9353",
  "Templates": {
  "Audit": {
    "TemplateId": "aBaIjTsPBluYtj2tzotzpowsDDBGLhXQkwrScupnQsM",
    "PageUrl": "/pages/index/formAudit?formId={0}&tableId={1}",
    "MiniprogramState": "developer",
    "Lang": "zh_TW",
    "Data": {
        "Title": "thing6",
        "Content": "thing19",
        "Date": "date9"
      }
    }
  },
  "SignatureToken": "aaaaaa",
  "MessageSendUrl": "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token={0}",
  "AccessTokenUrl": "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}"
}

二、编写通用类加载配置

using System;
using System.Text;
using System.Security.Cryptography;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;

namespace WXERP.Services
{
  /// <summary>
  /// 项目公有静态类
  /// </summary>
  public class Common
  {
    /// <summary>
    /// 獲取根目錄
    /// </summary>
    public static string AppRoot => Environment.CurrentDirectory;// AppContext.BaseDirectory;
    /// <summary>
    /// 獲取項目配置
    /// </summary>
    public static IConfiguration Configuration { get; set; }
    /// <summary>
    /// 加載項目配置
    /// </summary>
    static Common()
    {
      Configuration = new ConfigurationBuilder()
      .Add(new JsonConfigurationSource
      {
        Path = "appsettings.json",
        ReloadOnChange = true //当appsettings.json被修改时重新加载 
      })
      .Build();
    }

    /// <summary>
    /// SHA1加密
    /// </summary>
    /// <param name="content">需要加密的字符串</param>
    /// <returns>返回40位大寫字符串</returns>
    public static string SHA1(string content)
    {
      try
      {
        SHA1 sha1 = new SHA1CryptoServiceProvider();
        byte[] bytes_in = Encoding.UTF8.GetBytes(content);
        byte[] bytes_out = sha1.ComputeHash(bytes_in);
        sha1.Dispose();
        string result = BitConverter.ToString(bytes_out);
        result = result.Replace("-", "");
        return result;
      }
      catch (Exception ex)
      {
        throw new Exception("Error in SHA1: " + ex.Message);
      }
    }

  }
}

三、编写HttpHelper请求类

using System;
using System.Text;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Collections.Generic;

namespace WXERP.Services
{
  /// <summary>
  /// HTTP請求輔助類
  /// </summary>
  public class HttpHelper
  {
    /// <summary>
    /// post同步請求
    /// </summary>
    /// <param name="url">地址</param>
    /// <param name="postData">數據</param>
    /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
    /// <param name="headers">請求頭</param> 
    /// <returns></returns>
    public static string HttpPost(string url, string postData = http://www.cppcns.com/wangluo/aspnet/null, string contentType = null, Dictionary headers = null)
    {
      using HttpClient client = new HttpClient();

      if (headers != null)
      {
        foreach (var header in headers)
        client.DefaultRequestHeaders.Add(header.Key, header.Value);
      }

      postData ??="";
      using HttpContent httpContent = new StringContent(postData, Encoding.UTF8);
      if (contentType != null)
      httpContent.Headers.ContentType = new MediaTypeHeaderValue(contentType);

      HttpResponseMessage response = client.PostAsync(url, httpContent).Result;
      return response.Content.ReadAsStringAsync().Result;
    }

    /// <summary>
    /// post異步請求
    /// </summary>
    /// <param name="url">地址</param>
    /// <param name="postData">數據</param>
    /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
    /// <param name="timeOut">請求超時時間</param> 
    /// <param name="headers">請求頭</param> 
    /// <returns></returns>
    public static async Task<string> HttpPostAsync(string url, string postData = http://www.cppcns.com/wangluo/aspnet/null, string contentType = null, int timeOut = 30, Dictionary

.NET CORE3.1实现微信小程序发送订阅消息

扫一扫手机访问