小程序
小游戏
企业微信
微信支付
扫描小程序码分享
在调用https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token={0}接口时候出现下面的提示
{"errcode":41005,"errmsg":"media data missing hint: [A5xKlA00450022] rid: 5fa26845-61692912-4f6634a1"}
我用的是C#,能帮我查一下什么原因吗?
3 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
你好,
详情请参考:https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Global_Return_Code.html
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
使用HttpWebRequest 发送请求成功,代码如下
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
request.Method = "POST";
string boundary = DateTime.Now.Ticks.ToString("X");
request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary;
byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
int pos = path.LastIndexOf("\\");
string fileName = path.Substring(pos + 1);
StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name=\"" + formName + "\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n", fileName));
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString());
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
byte[] bArr = new byte[fs.Length];
fs.Read(bArr, 0, bArr.Length);
fs.Close();
Stream postStream = request.GetRequestStream();
postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
postStream.Write(bArr, 0, bArr.Length);
byte[] bSplit = Encoding.UTF8.GetBytes("--" + boundary + "\r\n");
postStream.Write(bSplit, 0, bSplit.Length);
if (!string.IsNullOrEmpty(additionInfo))
{
byte[] additionBytes = Encoding.UTF8.GetBytes("Content-Disposition: form-data; name=\"description\";\r\n\r\n");
postStream.Write(additionBytes, 0, additionBytes.Length);
byte[] additionContentBytes = Encoding.UTF8.GetBytes(additionInfo);
postStream.Write(additionContentBytes, 0, additionContentBytes.Length);
postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
}
postStream.Close();
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Stream instream = response.GetResponseStream();
StreamReader sr = new StreamReader(instream, Encoding.UTF8);
content = sr.ReadToEnd();
但是使用HttpClient就一直提示41005,代码如下
using (var client = _clientFactory.CreateClient())
var file = new FileInfo(filePath);
byte[] buffur = File.ReadAllBytes(filePath);
using (var formData = new MultipartFormDataContent())
ByteArrayContent fileContent = new ByteArrayContent(buffur);
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") { Name = formName, FileName = file.Name };
formData.Add(fileContent);
var response = await client.PostAsync(url, formData);
if (response.IsSuccessStatusCode)
var result = await response.Content.ReadAsStringAsync();
return result;
else
throw new BadHttpResponseException(url, response.StatusCode);
对比了一下貌似区别在换行符上"\r\n",但是封装好的HttpClient又不知道如何添加相应的换行符。
不知道这个是不是根本原因。
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
你好,
详情请参考:https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Global_Return_Code.html
使用HttpWebRequest 发送请求成功,代码如下
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
request.Method = "POST";
string boundary = DateTime.Now.Ticks.ToString("X");
request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary;
byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
int pos = path.LastIndexOf("\\");
string fileName = path.Substring(pos + 1);
StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name=\"" + formName + "\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n", fileName));
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString());
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
byte[] bArr = new byte[fs.Length];
fs.Read(bArr, 0, bArr.Length);
fs.Close();
Stream postStream = request.GetRequestStream();
postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
postStream.Write(bArr, 0, bArr.Length);
byte[] bSplit = Encoding.UTF8.GetBytes("--" + boundary + "\r\n");
postStream.Write(bSplit, 0, bSplit.Length);
if (!string.IsNullOrEmpty(additionInfo))
{
byte[] additionBytes = Encoding.UTF8.GetBytes("Content-Disposition: form-data; name=\"description\";\r\n\r\n");
postStream.Write(additionBytes, 0, additionBytes.Length);
byte[] additionContentBytes = Encoding.UTF8.GetBytes(additionInfo);
postStream.Write(additionContentBytes, 0, additionContentBytes.Length);
postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
}
postStream.Close();
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Stream instream = response.GetResponseStream();
StreamReader sr = new StreamReader(instream, Encoding.UTF8);
content = sr.ReadToEnd();
但是使用HttpClient就一直提示41005,代码如下
using (var client = _clientFactory.CreateClient())
{
var file = new FileInfo(filePath);
byte[] buffur = File.ReadAllBytes(filePath);
using (var formData = new MultipartFormDataContent())
{
ByteArrayContent fileContent = new ByteArrayContent(buffur);
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") { Name = formName, FileName = file.Name };
formData.Add(fileContent);
var response = await client.PostAsync(url, formData);
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsStringAsync();
return result;
}
else
{
throw new BadHttpResponseException(url, response.StatusCode);
}
}
}
对比了一下貌似区别在换行符上"\r\n",但是封装好的HttpClient又不知道如何添加相应的换行符。
不知道这个是不是根本原因。