http协议 文件上传 POST
2014-10-30 19:13
369 查看
//封装协议头
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;
}
[cpp]
view plaincopy
//封装数据前面的描述部分
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;
}
[cpp]
view plaincopy
//封装协议尾
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;
}
[cpp]
view plaincopy
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;
}
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;
}
[cpp]
view plaincopy
//封装数据前面的描述部分
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;
}
[cpp]
view plaincopy
//封装协议尾
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;
}
[cpp]
view plaincopy
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;
}
相关文章推荐
- C#实现http协议支持上传下载文件的GET、POST请求
- Android Http POST文件上传之-----RFC1867协议
- 使用WinHttp接口实现HTTP协议Get、Post和文件上传功能
- 实现HTTP协议Get、Post和文件上传功能——使用libcurl接口实现
- android http通过post上传文件和提交参数(通过拼装协议)
- C#实现http协议支持上传下载文件的GET、POST请求
- 实现HTTP协议Get、Post和文件上传功能——使用WinHttp接口实现
- 实现HTTP协议Get、Post和文件上传功能——使用libcurl接口实现
- 使用WinHttp接口实现HTTP协议Get、Post和文件上传功能
- C#实现http协议支持上传下载文件的GET、POST请求
- 实现HTTP协议Get、Post和文件上传功能——设计和模块
- HTTP协议下用Web Service上传大文件的解决方案
- 文件上传 方法之一------通过 http 协议上传文件
- 如何用Java客户端/applet通过HTTP POST上传文件
- 将文件通过http的post方法上传到服务器
- WinForm使用webclient通过http的POST方式上传文件
- HTTP协议下用Web Service上传大文件的解决方案
- 转:vc中如何通过http的post方式上传文件
- java的http协议文件上传 (二)
- HTTP协议下用Web Service上传大文件的解决方案