C#使用MultipartFormDataContent请求微信上传备案媒体材料接口响应41005,media data missing。应该怎么处理?
微信的接口文档:https://developers.weixin.qq.com/doc/oplatform/openApi/OpenApiDoc/miniprogram-management/record/uploadIcpMedia.html
代码如下:
//上传备案媒体材料
private string uploadIcpMedia(HttpContext context)
{
string accessToken = context.Request.QueryString["access_token"];
string type = context.Request.Form["type"];
string certificateType = context.Request.Form["certificate_type"];
string icpOrderField = context.Request.Form["icp_order_field"];
HttpPostedFile file = context.Request.Files["media"];
byte[] mediaByte= null;
if (type== "video")
{
mediaByte = GetMediaBytes(file);
}
if (!string.IsNullOrEmpty(accessToken) && !string.IsNullOrEmpty(type) && !string.IsNullOrEmpty(icpOrderField))
{
if (file != null)
{
string apiUrl = $"https://api.weixin.qq.com/wxa/icp/upload_icp_media?access_token={accessToken}";
var httpClient = new HttpClient();
string boundary = Guid.NewGuid().ToString("N");
var content = new MultipartFormDataContent(boundary);
content.Headers.ContentType = MediaTypeHeaderValue.Parse($"multipart/form-data;boundary=\"{boundary}\"");
content.Add(new StringContent(type), "type");
content.Add(new StringContent(certificateType), "certificate_type");
content.Add(new StringContent(icpOrderField), "icp_order_field");
file.InputStream.Seek(0, SeekOrigin.Begin);
if (type == "video")
{
content.Add(new ByteArrayContent(mediaByte), "media", $"\"{file.FileName}\"");
}
else
{
content.Add(new StreamContent(file.InputStream), "media", $"\"{file.FileName}\"");
}
string requestString = content.ReadAsStringAsync().Result;
var postResult = httpClient.PostAsync(apiUrl, content).Result;
string result = postResult.Content.ReadAsStringAsync().Result;
return result;
}
else
{
return "No media data provided";
}
}
else
{
return "Missing required parameters: access_token, type, and/or icp_order_field";
}
}
我觉得应该是content的内容有问题,但是不知道怎么正确处理。
这是在visual studio中调试content的内容,因为输出的是字符串,所以文件的data乱码没有显示
--9ca34941f422418ca91d5e34adc98572
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=type
image
--9ca34941f422418ca91d5e34adc98572
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=certificate_type
5
--9ca34941f422418ca91d5e34adc98572
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=icp_order_field
icp_subject.principal_info.certificate_photo_front
--9ca34941f422418ca91d5e34adc98572
Content-Disposition: form-data; name=media; filename="bg.png"; filename*=utf-8''%22bg.png%22
�PNG
下面是使用上传临时素材成功的代码,两个方法都是用的相同方法,但是不知道为什么,上传备案材料的接口就不行。可能是form-data的参数比较多吧。上传备案的方法的boundary得加双引号,但是我看到有个人说得去掉双引号才能成功,所以下面上传素材的方法就成功了。如果上传备案的方法去掉双引号,反而会报缺失参数。真的不知道怎么处理了。
上传临时素材文档:https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/Mini_Program_Basic_Info/setnickname.html
//上传临时素材
private string uploadMedia(HttpContext context)
{
string accessToken = context.Request.QueryString["access_token"];
string type = context.Request.QueryString["type"];
HttpPostedFile file = context.Request.Files["media"];
if (file != null && file.ContentLength > 0)
{
string apiurl = $"https://api.weixin.qq.com/cgi-bin/media/upload?access_token={accessToken}&type={type}";
var httpClient = new HttpClient();
string boundary = Guid.NewGuid().ToString("N");
var content = new MultipartFormDataContent(boundary);
content.Headers.ContentType = MediaTypeHeaderValue.Parse($"multipart/form-data;boundary={boundary}");
file.InputStream.Seek(0, SeekOrigin.Begin);
content.Add(new StreamContent(file.InputStream), "media", $"\"{file.FileName}\"");
string requestString = content.ReadAsStringAsync().Result;
var postResult = httpClient.PostAsync(apiurl, content).Result;
string result = postResult.Content.ReadAsStringAsync().Result;
return result;
}
else
{
return "No file uploaded";
}
}