利用winInet实现文件上传到HTTP服务器
CHttpConnection* httpConnection = internetSession.GetHttpConnection(strServerIP,intServerPort); //获得链接
CHttpFile* httpFile = httpConnection->OpenRequest(CHttpConnection::HTTP_VERB_PUT,strRemoteFile,NULL,0,NULL,NULL,INTERNET_FLAG_DONT_CACHE); //发送请求
...
CHttpConnection* httpConnection=internetSession.GetHttpConnection(strServerIP,intServerPort); //获得链接
CHttpFile* httpFile = httpConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST,strRemoteFile,NULL,0,NULL,NULL,INTERNET_FLAG_DONT_CACHE); //发送请求
...
{
}
UpLoadFile::~UpLoadFile(void)
{
}
BOOL UpLoadFile::UseHttpSendReqEx(CHttpFile* httpFile, DWORD dwPostSize,CString strLocalFile)
{
try
{
DWORD dwRead,dwRet;
BYTE* buffer;
TRACE("Local file:%s/n",strLocalFile);
FILE* fLocal;
if((fLocal=fopen(strLocalFile,"rb"))==NULL)
{
TRACE("Can't open the file:%s,maybe it doesn't exist!/n",strLocalFile);
return false;
}
fseek(fLocal,0L,SEEK_END);
dwRead=ftell(fLocal);
rewind(fLocal);
buffer=(BYTE *)malloc(dwRead);
if(!buffer){
TRACE("not enough memory!/n");
return false;
}
TRACE("length of file:%d/n",dwRead);
dwRead=fread(buffer,1,dwRead,fLocal);
dwPostSize=dwRead;
INTERNET_BUFFERS BufferIn;
DWORD dwBytesWritten;
BOOL bRet;
BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS ); // Must be set or error will occur
BufferIn.Next = NULL;
BufferIn.lpcszHeader = NULL;
BufferIn.dwHeadersLength = 0;
BufferIn.dwHeadersTotal = 0;
BufferIn.lpvBuffer = NULL;
BufferIn.dwBufferLength = 0;
BufferIn.dwBufferTotal = dwPostSize; // This is the only member used other than dwStructSize
BufferIn.dwOffsetLow = 0;
BufferIn.dwOffsetHigh = 0;
httpFile->SendRequestEx(&BufferIn,NULL,HSR_INITIATE,0);
//httpFile->SendRequestEx(dwPostSize);
httpFile->Write( buffer, dwPostSize);
if(!httpFile->EndRequest(0,0,0))
{
TRACE( "Error on HttpEndRequest %lu /n", GetLastError());
return FALSE;
}
fclose(fLocal);
free(buffer);
return TRUE;
}
catch (CInternetException* pEx)
{
//catch errors from WinInet
}
return FALSE;
}
BOOL UpLoadFile::Upload(CString strLocalFile,CString strServerIP,CString strServerPort,CString strRemoteFile)
{
try
{
DWORD dwPostSize=0;
INTERNET_PORT intServerPort=atoi(strServerPort);
CInternetSession internetSession("my session");
CHttpConnection* httpConnection = internetSession.GetHttpConnection(strServerIP,intServerPort);
if(httpConnection == NULL)
{
TRACE( "Failed to connect/n" );
return FALSE;
}
CHttpFile* httpFile = httpConnection->OpenRequest(CHttpConnection::HTTP_VERB_PUT,strRemoteFile,NULL,0,NULL,NULL,INTERNET_FLAG_DONT_CACHE);
//CHttpFile* httpFile = httpConnection->OpenRequest(CHttpConnection::HTTP_VERB_PUT,strRemoteFile);
if(httpFile == NULL)
{
TRACE( "Failed to open request handle/n" );
return FALSE;
}
if(UseHttpSendReqEx(httpFile, dwPostSize,strLocalFile))
{
TRACE( "/nSend Finished./n" );
httpFile->Close();
httpConnection->Close();
internetSession.Close();
return TRUE;
}
httpFile->Close();
httpConnection->Close();
internetSession.Close();
}
catch (CInternetException* pEx)
{
//catch errors from WinInet
}
return FALSE;
}
{
TCHAR tempFilePath[MAX_PATH];
tempFilePath[0] = 0;
GetEnvironmentVariable(_T("ProgramFiles"), tempFilePath, MAX_PATH);
if (tempFilePath[0] == 0)
{
strcpy(tempFilePath, "C://Program Files");
}
strncat(tempFilePath, "//VoiceX//upload.pcm", MAX_PATH);
monWave.Save(tempFilePath);
int startp = m_StandardWavURL.ReverseFind('/');
int namelen = m_StandardWavURL.GetLength()-startp-1;
CString pcmname = m_StandardWavURL.Mid(startp+1,namelen);
CString defServerName ="www.bingoenglish.com";
CString defObjectName ="/upload/upload.jsp";
// USES_CONVERSION;
CInternetSession Session;
CHttpConnection *pHttpConnection = NULL;
CHttpFile* pHTTP;
CString strHTTPBoundary;
CString strPreFileData;
CString strPostFileData;
DWORD dwTotalRequestLength;
DWORD dwChunkLength;
DWORD dwReadLength;
DWORD dwResponseLength;
TCHAR szError[MAX_PATH];
void* pBuffer;
LPSTR szResponse;
CString strResponse;
BOOL bSuccess = TRUE;
CString strDebugMessage;
if (FALSE == fTrack.Open(tempFilePath, CFile::modeRead | CFile::shareDenyWrite))
{
AfxMessageBox(_T("Unable to open the file."));
return FALSE;
}
CString strFileName = "upload.pcm";
int iRecordID = 1;
strPreFileData = MakePreFileData(strHTTPBoundary, pcmname, iRecordID);
strPostFileData = MakePostFileData(strHTTPBoundary);
AfxMessageBox(strPreFileData);
AfxMessageBox(strPostFileData);
dwTotalRequestLength = strPreFileData.GetLength() + strPostFileData.GetLength() + fTrack.GetLength();
dwChunkLength = 64 * 1024;
pBuffer = malloc(dwChunkLength);
if (NULL == pBuffer)
{
return FALSE;
}
try
{
pHttpConnection = Session.GetHttpConnection(defServerName,nPort);
pHTTP->AddRequestHeaders(MakeRequestHeaders(strHTTPBoundary));
pHTTP->SendRequestEx(dwTotalRequestLength, HSR_SYNC | HSR_INITIATE);
#ifdef _UNICODE
pHTTP->Write(W2A(strPreFileData), strPreFileData.GetLength());
#else
pHTTP->Write((LPSTR)(LPCSTR)strPreFileData, strPreFileData.GetLength());
#endif
dwReadLength = -1;
while (0 != dwReadLength)
{
strDebugMessage.Format(_T("%u / %u/n"), fTrack.GetPosition(), fTrack.GetLength());
TRACE(strDebugMessage);
dwReadLength = fTrack.Read(pBuffer, dwChunkLength);
if (0 != dwReadLength)
{
pHTTP->Write(pBuffer, dwReadLength);
}
}
#ifdef _UNICODE
pHTTP->Write(W2A(strPostFileData), strPostFileData.GetLength());
#else
pHTTP->Write((LPSTR)(LPCSTR)strPostFileData, strPostFileData.GetLength());
#endif
pHTTP->EndRequest(HSR_SYNC);
dwResponseLength = pHTTP->GetLength();
while (0 != dwResponseLength)
{
szResponse = (LPSTR)malloc(dwResponseLength + 1);
szResponse[dwResponseLength] = '/0';
pHTTP->Read(szResponse, dwResponseLength);
strResponse += szResponse;
free(szResponse);
dwResponseLength = pHTTP->GetLength();
}
AfxMessageBox(strResponse);
}
catch (CException* e)
{
e->GetErrorMessage(szError, MAX_PATH);
e->Delete();
AfxMessageBox(szError);
bSuccess = FALSE;
}
pHTTP->Close();
delete pHTTP;
fTrack.Close();
if (NULL != pBuffer)
{
free(pBuffer);
}
return bSuccess;
CString CVoiceXCtrl::MakeRequestHeaders(CString& strBoundary)
{
CString strData;
{
CString strData;
strFormat += _T("/r/n");
strFormat += _T("Content-Disposition: form-data; name=/"recordid/"");
strFormat += _T("/r/n/r/n");
strFormat += _T("%i");
strFormat += _T("/r/n");
strFormat += _T("--%s");
strFormat += _T("/r/n");
strFormat += _T("Content-Disposition: form-data; name=/"trackdata/"; filename=/"%s/"");
strFormat += _T("/r/n");
strFormat += _T("Content-Type: audio/wav");
strFormat += _T("/r/n");
strFormat += _T("Content-Transfer-Encoding: binary");
strFormat += _T("/r/n/r/n");
{
CString strData;
strFormat += _T("--%s");
strFormat += _T("/r/n");
strFormat += _T("Content-Disposition: form-data; name=/"submitted/"");
strFormat += _T("/r/n/r/n");
strFormat += _T("hello");
strFormat += _T("/r/n");
strFormat += _T("--%s--");
strFormat += _T("/r/n");