收藏
回答

上传素材的接口总是41005?

在调用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 个回答

  • 社区技术运营专员--许涛
    社区技术运营专员--许涛
    2020-11-04

    你好,

    详情请参考:https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Global_Return_Code.html

    2020-11-04
    有用
    回复 1
    • dongbo
      dongbo
      2020-11-04
      微信的这个文件上传的接口有什么特别要求吗?我用HttpClient的代码去请求别的API接口都可以成功上传,但是微信的这个就41005了
      2020-11-04
      回复
  • dongbo
    dongbo
    2020-11-04

    使用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又不知道如何添加相应的换行符。

    不知道这个是不是根本原因。

    2020-11-04
    有用 1
    回复
  • 鑫壬
    鑫壬
    发表于移动端
    01-22
    解决了吗,兄弟
    01-22
    有用
    回复
登录 后发表内容
问题标签