- //封装协议头
- CString CQueenMainDlg::MakeRequestHeaders(CString &strBoundary)// strBoundary 为协议中的boundary
- {
- CString strFormat=_T("");
- CString strData =_T("");
- strFormat += "User-Agent: Mozilla/4.0\r\n";
- strFormat += "Connection: Keep-Alive\r\n";
- strFormat += "Accept:*/*\r\n";
- strFormat += "Accept-Language: zh-cn\r\n";
- strFormat += _T("Accept-Encoding:gzip,deflate\r\n");
- strFormat += _T("Content-Type: multipart/form-data; boundary=%s\r\n");
- strFormat +=_T("Host: %s:%d\r\n");
- strData.Format(strFormat, strBoundary,m_strSeverName, m_nPort);
- return strData;
- }
- //封装数据前面的描述部分
- CString CQueenMainDlg::MakePreFileData(CString &strBoundary, CString &strFileName)
- {
- //Content-Type:
- //JPG image/pjpeg
- //PNG image/x-png
- //BMP image/bmp
- //TIF image/tiff
- //GIF image/gif
- CString strFormat=_T("");
- CString strData=_T("");
- strFormat += _T("--%s");
- strFormat += _T("\r\n");
- strFormat += _T("Content-Disposition: form-data; name=\"file\"; filename=\"%s\"");
- strFormat += _T("\r\n");
- strFormat += _T("Content-Type:image/bmp");//
- strFormat += _T("\r\n");
- strFormat += _T("Content-Transfer-Encoding: binary");
- strFormat += _T("\r\n\r\n");
- strData.Format(strFormat, strBoundary, strFileName);
- return strData;
- }
- //封装协议尾
- CString CQueenMainDlg::MakePostFileData(CString &strBoundary)
- {
- CString strFormat;
- CString strData;
- strFormat = _T("\r\n");
- strFormat += _T("--%s");
- strFormat += _T("\r\n");
- strFormat += _T("Content-Disposition: form-data; name=\"submitted\"");
- strFormat += _T("\r\n\r\n");
- strFormat += _T("submit");
- strFormat += _T("\r\n");
- strFormat += _T("--%s--");
- strFormat += _T("\r\n");
- strData.Format(strFormat, strBoundary, strBoundary);
- return strData;
- }
- BOOL CQueenMainDlg::SendTrack()
- {
- CString m_Url = "http://192.168.1.66:8080/queen_web/imgupload/imgupload.do";// "http://192.168.1.13:65001/aaaa";//
- LoadSvrAddress(m_Url);
- CString m_strFilePath = "E:\\lobby1\\queen\\lobby\\";//theApp.m_strAppPath;
- m_strFilePath += _T("ScreenShot.bmp");
- CString strFileName = _T("ScreenShot.bmp");
- UpdateData(TRUE);
- CString defServerName =m_strSeverName;
- CString defObjectName =m_strObject;
- // USES_CONVERSION;
- CInternetSession Session;
- CHttpConnection *pHttpConnection = NULL;
- INTERNET_PORT nPort = m_nPort;
- CFile fTrack;
- CHttpFile* pHTTP;
- CString strRequestHeader=_T("");
- CString strHTTPBoundary=_T("");
- CString strPreFileData=_T("");
- CString strPostFileData=_T("");
- CString strResponse =_T("");
- DWORD dwTotalRequestLength;
- DWORD dwChunkLength;
- DWORD dwReadLength;
- DWORD dwResponseLength;
- TCHAR szError[MAX_PATH];
- void* pBuffer =NULL;
- LPSTR szResponse;
- BOOL bSuccess = TRUE;
- CString strDebugMessage =_T("");
- if (FALSE == fTrack.Open(m_strFilePath, CFile::modeRead | CFile::shareDenyWrite))
- {
- AfxMessageBox(_T("Unable to open the file."));
- return FALSE;
- }
- strHTTPBoundary = _T("-----------------------------7d86d16250370");
- strRequestHeader =MakeRequestHeaders(strHTTPBoundary);
- strPreFileData = MakePreFileData(strHTTPBoundary, strFileName);
- strPostFileData = MakePostFileData(strHTTPBoundary);
- MessageBox(strRequestHeader,"RequestHeader",MB_OK | MB_ICONINFORMATION);
- MessageBox(strPreFileData,"PreFileData",MB_OK | MB_ICONINFORMATION);
- MessageBox(strPostFileData,"PostFileData",MB_OK | MB_ICONINFORMATION);
- dwTotalRequestLength = strPreFileData.GetLength() + strPostFileData.GetLength()+ fTrack.GetLength();
- dwChunkLength = 64 * 1024;
- pBuffer = malloc(dwChunkLength);
- if (NULL == pBuffer)
- {
- return FALSE;
- }
- try
- {
- pHttpConnection = Session.GetHttpConnection(defServerName,nPort);
- if(pHttpConnection == NULL)
- {
- throw 0 ; //连接服务器失败!
- }
- pHTTP = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, defObjectName);
- pHTTP->AddRequestHeaders(strRequestHeader);
- 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;
- int count = 1;
- while (0 != dwReadLength)
- {
- strDebugMessage.Format(_T("%u / %u\n"), fTrack.GetPosition(), fTrack.GetLength());
- TRACE(strDebugMessage);
- dwReadLength = fTrack.Read(pBuffer, dwChunkLength);
- CString m_str;
- m_str.Format("count = %d,dwReadLength = %d\n",count,dwReadLength);
- count++;
- OutputDebugString(m_str);
- 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();
- }
- //MessageBox(strResponse,"Response",MB_OK | MB_ICONINFORMATION);
- }
- 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;
- }