收藏
回答

企业微信上传临时素材44001错误

语言c++,https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE上传临时素材,报错44001
代码及问题截图如下,请问我需要怎么修改



#include "stdafx.h"
#include "CHttpClient.h"
#include <winInet.h>
#include <afxinet.h>
CHttpClient::CHttpClient()
{
}




CHttpClient::~CHttpClient()
{
}
int CHttpClient::ExecuteRequestUpload(LPCTSTR strFileName, LPCTSTR strUrl, string &strResponse)
{
	CString strServer;
	CString strObject;
	DWORD dwServiceType;
	INTERNET_PORT nPort;
	strResponse = "";
	CHttpConnection *m_pConnection = NULL;
	CInternetSession pSession;
	CHttpFile* m_pFile = NULL;
	AfxParseURL(strUrl, dwServiceType, strServer, strObject, nPort);


	if (AFX_INET_SERVICE_HTTP != dwServiceType && AFX_INET_SERVICE_HTTPS != dwServiceType)
	{
		return 1;
	}


	CFile fTrack;
	if (FALSE == fTrack.Open(strFileName, CFile::shareDenyNone | CFile::modeRead))
		return -1;


	CString strRequestHeader = L"";
	CString strHTTPBoundary = L"";
	CString strPreFileData = L"";
	CString strPostFileData = L"";


	DWORD dwTotalRequestLength = 0;
	DWORD dwChunkLength = 0;
	DWORD dwReadLength = 0;
	DWORD dwResponseLength = 0;


	LPSTR szResponse = NULL;




	CString strFilePath = strFileName;
	int startp = strFilePath.ReverseFind('\\');
	int namelen = strFilePath.GetLength() - startp - 1;


	CString szFileName = strFilePath.Mid(startp + 1, namelen);


	strHTTPBoundary = L"-------------------------acebdf13572468";
	strPreFileData = MakePreFileData(strHTTPBoundary, szFileName, fTrack.GetLength()).c_str();
	strPostFileData = MakeRequestEnders(strHTTPBoundary).c_str();


	dwTotalRequestLength = strPreFileData.GetLength() + strPostFileData.GetLength() + fTrack.GetLength();
	strRequestHeader = MakeRequestHeaders(strHTTPBoundary, strServer, nPort, dwTotalRequestLength).c_str();
	dwChunkLength = 64 * 1024;


	void* pBuffer = malloc(dwChunkLength);
	if (NULL == pBuffer)
	{
		return -1;
	}
	else
	{
		try
		{
			m_pConnection = pSession.GetHttpConnection(strServer, nPort, NULL, NULL);
			m_pFile = m_pConnection->OpenRequest(L"POST",strObject,NULL, 1, NULL, NULL, INTERNET_FLAG_EXISTING_CONNECT);


			m_pFile->AddRequestHeaders(strRequestHeader);
			m_pFile->SendRequestEx(dwTotalRequestLength, HSR_SYNC | HSR_INITIATE);


#ifdef _UNICODE  
			m_pFile->Write(strPreFileData, strPreFileData.GetLength());
#else  
			m_pFile->Write((LPSTR)(LPCSTR)strPreFileData, strPreFileData.GetLength());
#endif  


			dwReadLength = -1;
			while (0 != dwReadLength)
			{
				dwReadLength = fTrack.Read(pBuffer, dwChunkLength);
				if (0 != dwReadLength)
				{
					m_pFile->Write(pBuffer, dwReadLength);
				}
			}


#ifdef _UNICODE  
			m_pFile->Write(strPostFileData, strPostFileData.GetLength());
#else  
			m_pFile->Write((LPSTR)(LPCSTR)strPostFileData, strPostFileData.GetLength());
#endif  


			m_pFile->EndRequest(HSR_SYNC);


			dwResponseLength = m_pFile->GetLength();
			while (0 != dwResponseLength)
			{
				szResponse = (LPSTR)malloc(dwResponseLength + 1);
				szResponse[dwResponseLength] = '\0';
				m_pFile->Read(szResponse, dwResponseLength);
				strResponse += szResponse;
				free(szResponse);
				dwResponseLength = m_pFile->GetLength();
			}
		}
		catch (CInternetException* e)
		{
			//Clear();
			DWORD dwErrorCode = e->m_dwError;
			e->Delete();


			DWORD dwError = GetLastError();


			if (ERROR_INTERNET_TIMEOUT == dwErrorCode)
			{
				return 3;
			}
			else
			{
				return 4;
			}
		}
	}


	return SUCCESS;
}






string CHttpClient::MakeRequestHeaders(LPCTSTR strBoundary, LPCTSTR strServerName, const int nPort,DWORD dwTotalRequestLength)
{
	//CHAR strFormat[1024] = "";
	//static CHAR strData[1024];
	//const char* str = "Content-Type: multipart/form-data;boundary=-------------------------acebdf13572468\r\n";
	//strcat(strFormat, "Accept-Language: zh-cn;\r\n");
	//strcat(strFormat, str);
	//strcat(strFormat, "Connection: keep-alive\r\n");
	////strcat(strFormat, "Host:%s;\r\n");


	//sprintf(strData, strFormat, strBoundary);
	string len =  to_string(long long(dwTotalRequestLength));
	string strData = "Accept-Language: zh-cn\r\nContent-Type: multipart/form-data;boundary=-------------------------acebdf13572468\r\nConnection: keep-alive\r\nContent-Length:";
	strData += len + "\r\n";
	
	return strData;
}


string CHttpClient::MakePreFileData(LPCTSTR strBoundary, LPCTSTR strFileName, DWORD dsize)
{
	//CHAR strFormat[1024] = "";
	//static CHAR strData[1024];
	////const char* fileName = strFileName;
	//strcat(strFormat, "\r\n-------------------------acebdf13572468\r\n");
	//strcat(strFormat, "Content-Disposition: form-data; name=\"media\"; filename=\"%s\";filelength=d%\r\n");
	//strcat(strFormat, "Content-Type:image/bmp;image/jpeg;image/x-png;application/zip;application/octet-stream\r\n\r\n");
	//  //      strcat(strFormat,"Content-Transfer-Encoding:binary\r\n\r\n");


	//sprintf(strData, strFormat, strBoundary, strFileName, dsize);
	string str = CW2A(strFileName);
	string size = to_string(long long(dsize));
	string strData = "-------------------------acebdf13572468\r\n";
	strData +="Content-Disposition: form-data; name=\"media\"; filename=\""+ str +"\";filelength="+ size +"\r\n";
	strData += "Content-Type:application/octet-stream\r\n";
	
	return strData;
}


string CHttpClient::MakeRequestEnders(LPCTSTR strBoundary)
{
	//CHAR strFormat[256] = "";
	//static CHAR strEnd[256];


	//strcat(strFormat, "\r\n%s--\r\n");
	////        strcat(strFormat,"Content-Disposition: form-data;name =\"submitted\"\r\n\r\n");
	////        strcat(strFormat,"submit\r\n");
	////        strcat(strFormat,"--%s--\r\n");


	//sprintf(strEnd, strFormat, strBoundary);
	string strEnd = "-------------------------acebdf13572468--\r\n";
	return strEnd;
}


回答关注问题邀请回答
收藏

3 个回答

  • 企业微信运营专员-千夜
    企业微信运营专员-千夜
    2020-01-17

    您好,看截图报错是媒体文件为空,确认选header和body的内容是否正确,如果找不到问题可以把hint信息贴下出来

    2020-01-17
    有用
    回复 2
    • 怪蜀黍
      怪蜀黍
      2020-01-20
      我对比了官方示例文档的请求包跟我的发送的请求包是一样的,hint信息在本文上方的截图中体现了,每次返回的信息都是这样的
      2020-01-20
      回复
    • 堂吉诃德分德
      堂吉诃德分德
      2022-08-09
      "errcode":44001,"errmsg":"empty media data, hint: [1660029245235730820162753]
      2022-08-09
      回复
  • 桦桦
    桦桦
    2020-03-11

    我也遇到同样问题,不过我是用node实现出现这样的问题的,坐等解答:https://developers.weixin.qq.com/community/develop/doc/0002c0f2ae47e80f8f0ab4ea35b800?fromCreate=0

    2020-03-11
    有用
    回复
  • 怪蜀黍
    怪蜀黍
    2020-01-23

    最终还是没用c++成功解决这个问题,转换成用c#写了一个上传临时素材的方法,然后封装为com组件,用c++程序调用,虽然有点麻烦,但是c++我实在无能为力了


    2020-01-23
    有用
    回复 1
    • H.X.C
      H.X.C
      2021-05-06
      能分享一下C#代码吗
      2021-05-06
      回复
登录 后发表内容
问题标签