首页 > 编程语言 >在Asp

在Asp

时间:2024-04-10 20:04:10浏览次数:22  
标签:Asp string get token set weiXinDto public

在Asp.Net Core中发送企业微信信息

起因

将原.net framework通知程序升级到.net core,原先直接用的别人的包(最后更新时间:2015年),百度没找到想要的,于是自己写吧。

实现

企业微信文档链接https://developer.work.weixin.qq.com/document/path/90236#文本卡片消息

appsettings.json添加:

 "WeiXin": {
   "AppId": "填自己的",
   "Secret": "填自己的",
   "agentid": "填自己的"
 },

使用IConfiguration注入,Configuration["WeiXin:AppId"]读取

构建相关类:

public class WeiXinDto
{
    public string? touser { get; set; }
    public string? msgtype { get; set; }
    public int? agentid { get; set; }
    public object? textcard { get; set; }
}
public class AccessTokenDto
{
    public int? errcode { get; set; }
    public string? errmsg { get; set; }
    public string? access_token { get; set; }
    public int? expires_in { get; set; }
}

使用IHttpClientFactory接口:在program.cs中添加builder.Services.AddHttpClient();

获取token:

public async Task GetToken()
{
    using HttpResponseMessage httpResponse = await client.GetAsync("请求链接");
    var result = httpResponse.Content.ReadAsStringAsync().Result;
    var responseObject = JsonSerializer.Deserialize<AccessTokenDto>(result);
    access_token = responseObject?.access_token;
}

发送信息:

public  async Task SendWinXinMsg(string msgtype, string touser,int agentid, string title,string description,string url)
{
    WeiXinDto weiXinDto = new WeiXinDto();
    weiXinDto.touser = touser;
    weiXinDto.agentid = agentid;
    weiXinDto.msgtype = msgtype;
    weiXinDto.textcard = new
    {
        title=title,
        description=description,
        url=url
    };
    await GetToken();//获取access_token
    using StringContent json = new(
JsonSerializer.Serialize(weiXinDto, new JsonSerializerOptions(JsonSerializerDefaults.Web)),
Encoding.UTF8,
MediaTypeNames.Application.Json);
        using HttpResponseMessage httpResponse=await client.PostAsync("请求链接", json);
    httpResponse.EnsureSuccessStatusCode();
}

标签:Asp,string,get,token,set,weiXinDto,public
From: https://www.cnblogs.com/ssz0312/p/18127270

相关文章

  • graspnet复现保姆级教程
    ubuntu22.04复现graspnet保姆级教程1.配置环境1.1确定环境Ubuntu22.04显卡驱动:DriverVersion:470.239.06CUDA版本(作者使用的是10.0.221)根据CUDA版本确定cudnn()版本根据CUDA和cudnn版本下载对应的pytorch版本python==3.7.16(必须大于等于3.6)pytorch==1.7.0......
  • 想要建立一个 Raspberry Pi 5 集群吗?
    NurgaliyevShakhizat用三台RaspberryPi5创建了一个神奇的Ceph集群。这是一个色彩缤纷的高级项目,适合技术特别精通的人,他花了大约六个小时才完成。RaspberryPi通过1Gbit交换机连接在专用网络中,由三个256GBSSD驱动器提供存储空间。Ceph是一种开源软件定义存储......
  • Metasploit Pro 4.22.3-2024040301 (Linux, Windows) - 专业渗透测试框架
    MetasploitPro4.22.3-2024040301(Linux,Windows)-专业渗透测试框架Rapid7Penetrationtesting,ReleaseApr03,2024请访问原文链接:MetasploitPro4.22.3-2024040301(Linux,Windows)-专业渗透测试框架,查看最新版。原创作品,转载请保留出处。作者主页:sysin.org无......
  • 界面控件DevExtreme JS & ASP.NET Core 2024年度产品规划预览(二)
    在本文中我们将介绍今年即将发布的v24.1附带的主要特性,这些特性既适用于DevExtreme JavaScript(Angular、React、Vue、jQuery),也适用于基于DevExtreme的ASP.NETMVC/Core控件。注意:本文中列出的功能和特性说明官方当前/预计的发展计划,此信息仅供参考之用,其中列出的功能/产品可......
  • asp.net KindEditor编辑器整合百度WebUploader插件批量上传图片
    集成背景说明多个图片进行批量上传新版本浏览器兼容性问题处理,原编辑器上传图片功能使用flash上传插件,由于最新版本浏览器不再支持flash,重新集成新的上传插件,对编辑器进行改造升级。一、KindEditor介绍KindEditor是一套开源的HTML可视化编辑器,主要用于让用户在网站上获得所见即......
  • ASP.NET MVC WebApi 接口返回 JOSN 日期格式化 date format
    ASP.NETMVCWebApi接口返回JOSN日期格式化dateformat全局配置WebApiConfigpublicstaticclassWebApiConfig{publicstaticvoidRegister(HttpConfigurationconfig){//WebAPI配置和服务//WebAPI路由config.MapHttpAttributeRoutes......
  • asp.core生成docker镜像(使用本地nuget)
    生成Dockerfilevs自带生成Dockerfile功能了使用本地的nuget包加入nuget配置文件NuGet.ConfigNuGet.Config配置文件,配置地址如果没有配置生成镜像会报错,没找到package生成镜像生成Docker映像(想深入了解,可以网上看看dockerbuild的参数)dockerbuild-f"C:\Projec......
  • 如何用asp.net制作网站
    ASP.NET是一种基于微软公司的.NET平台的Web应用程序框架,它允许开发人员使用多种编程语言来构建动态Web网站和Web应用程序。准备工作在开始制作ASP.NET网站之前,需要有以下几个基本条件:安装VisualStudio或者VisualStudioCode等IDE.NETFrameworkRuntime环境IIS服务器创......
  • 深圳阿里云代理商:asp.net如何提交数据库
    深圳阿里云代理商:asp.net如何提交数据库简介:飞机@luotuoemo本文由(阿里云代理商:【金推云】www.jintui.cn)撰写如何在ASP.NET中提交数据库阿里云代理商的优势作为深圳阿里云代理商,我们提供高效、安全、稳定的云计算服务,能够满足客户的各种需求。通过阿里云的弹性计算、数据......
  • Spring Data JPA应用之动态查询JpaSpecificationExecutor
    JPA提供了基于准则查询的方式即Criterial查询——Specification接口。该接口定义了一个toPredicate方法用例构造查询条件。在SpringBoot对SpringDataJPA的支持案例的基础上对该接口实操进行探讨。1)数据访问接口必须实现JpaSpecificationExecutor......