18,356
社区成员
发帖
与我相关
我的任务
分享
int CHttpClient::ExecuteRequestUpload(LPCTSTR strFileName, LPCTSTR strUrl, string &strResponse)
{
CString strServer;
CString strObject;
DWORD dwServiceType;
INTERNET_PORT nPort;
strResponse = "";
AfxParseURL(strUrl, dwServiceType, strServer, strObject, nPort);
if(AFX_INET_SERVICE_HTTP != dwServiceType && AFX_INET_SERVICE_HTTPS != dwServiceType)
{
return FAILURE;
}
CFile fTrack;
if (FALSE == fTrack.Open(strFileName, CFile::modeRead | CFile::shareDenyWrite))
return -1;
CString strRequestHeader=_T("");
CString strHTTPBoundary=_T("");
CString strPreFileData=_T("");
CString strPostFileData=_T("");
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 = _T("-----------------------------7d86d16250370");
strRequestHeader =MakeRequestHeaders(strHTTPBoundary,strServer,nPort);
strPreFileData = MakePreFileData(strHTTPBoundary, szFileName,fTrack.GetLength());
strPostFileData = MakeRequestEnders(strHTTPBoundary);
dwTotalRequestLength = strPreFileData.GetLength() + strPostFileData.GetLength() + fTrack.GetLength();
dwChunkLength = 64 * 1024;
void* pBuffer = malloc(dwChunkLength);
if (NULL == pBuffer)
{
return -1;
}
else
{
try
{
m_pConnection = m_pSession->GetHttpConnection(strServer,
dwServiceType == AFX_INET_SERVICE_HTTP ? NORMAL_CONNECT : SECURE_CONNECT,
nPort);
m_pFile = m_pConnection->OpenRequest("POST", strObject,
NULL, 1, NULL, NULL,
(dwServiceType == AFX_INET_SERVICE_HTTP ? NORMAL_REQUEST : SECURE_REQUEST));
m_pFile->AddRequestHeaders(strRequestHeader);
m_pFile->SendRequestEx(dwTotalRequestLength, HSR_SYNC | HSR_INITIATE);
#ifdef _UNICODE
m_pFile->Write(W2A(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(W2A(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 OUTTIME;
}
else
{
return FAILURE;
}
}
}
return SUCCESS;
}
LPCTSTR CHttpClient::MakeRequestHeaders( LPCTSTR strBoundary, LPCTSTR strServerName, const int nPort)
{
TCHAR strFormat[1024] = "";
static TCHAR strData[1024];
strcat(strFormat,"Accept-Language: zh-cn");
strcat(strFormat,"Content-Type: multipart/form-data;boundary=%s\r\n");
strcat(strFormat,"Connection: keep-alive\r\n");
strcat(strFormat,"Host:%s\r\n");
sprintf_s(strData,strFormat,strBoundary,strServerName);
return strData;
}
LPCTSTR CHttpClient::MakePreFileData( LPCTSTR strBoundary, LPCTSTR strFileName ,DWORD dsize)
{
TCHAR strFormat[1024] = "";
static TCHAR strData[1024];
strcat(strFormat,"--%s\r\n");
strcat(strFormat,"Content-Disposition: form-data; name=\"media\"; filename=\"%s\"\r\n");
strcat(strFormat,"Content-Type:image/bmp;image/jpeg;image/x-png;application/zip\r\n");
// strcat(strFormat,"Content-Transfer-Encoding:binary\r\n\r\n");
sprintf_s(strData,strFormat,strBoundary,strFileName,dsize);
return strData;
}
LPCTSTR CHttpClient::MakeRequestEnders( LPCTSTR strBoundary )
{
TCHAR strFormat[256] = "";
static TCHAR 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_s(strEnd,strFormat,strBoundary);
return strEnd;
}