本帖最后由 诗仙·1957 于 2023-3-23 10:17 编辑
我用机器人制造了这样一段c#
using GrapeCity.Forguncy.ServerApi;
using Microsoft.AspNetCore.Http;
using System.Security.Cryptography;
using System.Text;
using System;
using System.Threading.Tasks;
using System.IO;
using System.Net;
using System.Reflection.Metadata;
using System.Xml.Linq;
namespace wxapi
{
public class wxapi : ForguncyApi
{
[Post]
public async Task wxsucai()
{
var form = await Context.Request.ReadFormAsync();
var access_token = form["token"][0];
var serverurl = form["serverurl"][0];
var media_url = form["url"][0];
// Create a new HTTP request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serverurl);
request.Method = "POST";
request.ContentType = "multipart/form-data; boundary=---------------------------" + DateTime.Now.Ticks.ToString("x");
// Build the request body
string boundary = "-----------------------------" + DateTime.Now.Ticks.ToString("x");
byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
byte[] trailerBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
byte[] access_tokenBytes = Encoding.UTF8.GetBytes("Content-Disposition: form-data; name=\"access_token\"\r\n\r\n" + access_token + "\r\n");
byte[] mediaBytes = File.ReadAllBytes(media_url);
byte[] mediaHeaderBytes = Encoding.UTF8.GetBytes("Content-Disposition: form-data; name=\"media\"; filename=\"" + Path.GetFileName(media_url) + "\"\r\nContent-Type: application/octet-stream\r\n\r\n");
// Calculate the total request length
long contentLength = boundaryBytes.Length + access_tokenBytes.Length + boundaryBytes.Length + mediaHeaderBytes.Length + mediaBytes.Length + trailerBytes.Length;
// Set the content length header
request.ContentLength = contentLength;
// Write the request body
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
requestStream.Write(access_tokenBytes, 0, access_tokenBytes.Length);
requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
requestStream.Write(mediaHeaderBytes, 0, mediaHeaderBytes.Length);
requestStream.Write(mediaBytes, 0, mediaBytes.Length);
requestStream.Write(trailerBytes, 0, trailerBytes.Length);
}
// Send the request and get the response
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
// Handle the response
Console.WriteLine("Response status code: " + response.StatusCode);
using (Stream responseStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
Console.WriteLine("Response body: " + reader.ReadToEnd());
}
}
}
}
}
从代码上看,没多大毛病
但测试结果如下
测试内容上,向这个webAPI发送的文件路径,中有2个下划线,这个文件的路径应该/home/aaa.jpg
还是http://abc.com/aaa.jpg
测试发送 http://abc.com/aaa.jpg返回信息也是这样。
微信公众号素材接口,有没有什么插件可用?
|