惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
GbyAI
GbyAI
Google DeepMind News
Google DeepMind News
Jina AI
Jina AI
B
Blog
aimingoo的专栏
aimingoo的专栏
酷 壳 – CoolShell
酷 壳 – CoolShell
T
The Blog of Author Tim Ferriss
Last Week in AI
Last Week in AI
月光博客
月光博客
H
Help Net Security
V
Visual Studio Blog
量子位
A
About on SuperTechFans
博客园 - Franky
人人都是产品经理
人人都是产品经理
N
Netflix TechBlog - Medium
云风的 BLOG
云风的 BLOG
雷峰网
雷峰网
Martin Fowler
Martin Fowler
Microsoft Security Blog
Microsoft Security Blog
博客园 - 叶小钗
P
Proofpoint News Feed
MongoDB | Blog
MongoDB | Blog

博客园 - 鹅是程序猿

net core The library 'libhostpolicy.so' required to execute the application was not found的解决办法 国内CMS建站系统行业:从工具到数字化平台的转型之路 什么是低代码?国内主流低代码平台产品介绍 企业微信授权开发修改Windows hosts模拟外网域名的教程 PageAdmin CMS实现后台内网和前台外网分离部署的教程 2026主流的自助建站系统比较 外贸独立站用什么建站系统好 记一次火狐浏览器被www.2345.com恶意劫持主页处理过程。 2026常用的网页制作软件大全 5个常用的网页制作软件和建站系统,省时又省力! 网站建设主流的4款CMS建站系统对比,WebpyCMS才是轻量级的选择 PageAdmin CMS建站系统承载千万级内容和高并发的架构讲解 2026三款顶尖的CMS建站系统对比!选哪个最香? 2026支持多站点的站群CMS系统推荐 PageAdmin和简道云,明道云、宜搭等低代码平台的区别 2026年做网站建站工具全面对比分析,SaaS平台、开源系统与新兴工具的选择策略 PageAdmin,Wordpres,Drupal三大顶级CMS内容管理系统的区别 2026做网站用什么建站软件或平台?企业站怎么搭建? 常用的cms内容管理系统和建站软件,看看你用过几个? 2026年主流CMS建站工具深度解析与选型指南 两款主流顶尖级的CMS建站工具软件对比及选型技巧 2026集团网站建设指南及CMS站群软件系统的选型 推荐4款功能强大的CMS建站系统 2026主流建站软件工具大比拼,哪个适合你? PageAdmin CMS建站系统启用双因子身份认证的教程 国内外主流CMS建站系统及其特点总结 swiper不同版本多行滚动的代码 支持即时通讯聊天功能的cms内容管理系统有哪些 2026支持站群集约化的cms建站系统分享 PageAdmin CMS推出第五代内容管理系统,重新定义新一代的数字化平台标杆典范
Net 8 微信公众号上传图片永久图片素材和内容中图片素材的封...
鹅是程序猿 · 2026-03-29 · via 博客园 - 鹅是程序猿

测试了n次,不能用net自带的 MultipartFormDataContent,否则微信公众号会一直报错41005错误,具体原因不详,只能拼装head字符串实现,下面是封装好的上传永久素材和临时图片素材的方法。

   //上传图片 isPermanentImage=true表示永久素材  filePath为http地址,如果本地文件的自行读取
//isPermanentImage 表示是否是永久素材
private async Task<string> UploadImageAsync(string accessToken, string filePath, bool isPermanentImage) { var http = new HttpClient();//net core中改为通过IHttpClientFactory.CreateClient()实现 string url; if (isPermanentImage){
//封装图片 url
= $"https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={accessToken}&type=image"; } else{
//文章内容中的图片 url
= $"https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token={accessToken}&type=image"; } string boundary = "Upload----" + DateTime.Now.Ticks.ToString("x"); string fileName = System.IO.Path.GetFileName(filePath); var fileData = await http.GetByteArrayAsync(filePath); using var ms = new MemoryStream(); // 1. 写入 boundary byte[] boundaryBytes = Encoding.UTF8.GetBytes($"--{boundary}\r\n"); ms.Write(boundaryBytes, 0, boundaryBytes.Length); // 2. 写入媒体文件描述头 string header =$"Content-Disposition: form-data; name=\"media\"; filename=\"{fileName}\"\r\n" + $"Content-Type: {GetMime(fileName)}\r\n\r\n"; byte[] headerBytes = Encoding.UTF8.GetBytes(header); ms.Write(headerBytes, 0, headerBytes.Length); // 3. 写入文件内容 ms.Write(fileData, 0, fileData.Length); // 4. 写入结束 boundary byte[] endBytes = Encoding.UTF8.GetBytes($"\r\n--{boundary}--\r\n"); ms.Write(endBytes, 0, endBytes.Length); ms.Position = 0; // 发送请求 using HttpClient client = new HttpClient(); HttpContent content = new StreamContent(ms); content.Headers.ContentType = MediaTypeHeaderValue.Parse($"multipart/form-data; boundary={boundary}"); var response = await client.PostAsync(url, content); string json = await response.Content.ReadAsStringAsync(); Console.WriteLine(json); if (isPermanentImage) { return Newtonsoft.Json.Linq.JObject.Parse(json)["media_id"]?.ToString(); } else { return Newtonsoft.Json.Linq.JObject.Parse(json)["url"]?.ToString(); } }