HTTP协议上传
CString CHotpimUploadDlg::MakeRequestHeaders(CString &strBoundary)//包头
{
CString strFormat;
CString strData;
strFormat = _T("Content-Type: multipart/form-data; boundary=%s/r/n");//二进制文件传送Content-Type类型为: multipart/form-data
strData.Format(strFormat, strBoundary);
return strData;
}
CString CHotpimUploadDlg::MakePreFileData(CString &strBoundary, CString &strFileName, CString &strURL)
{
/**/
//Content-Type:
//JPG image/pjpeg
//PNG image/x-png
//BMP image/bmp
//TIF image/tiff
//GIF image/gif
CString strFormat;
CString strData;
strFormat += _T("--%s");
strFormat += _T("/r/n");
strFormat += _T("Content-Disposition: form-data; name=/"thisurl/"");//传给网络上的参数,根据网站抓包查看到底是需要哪些
strFormat += _T("/r/n/r/n");
strFormat += _T("%s");
strFormat += _T("/r/n");
strFormat += _T("--%s");
strFormat += _T("/r/n");
strFormat += _T("Content-Disposition: form-data; name=/"filePath/"; filename=/"%s/"");//文件地址信息
strFormat += _T("/r/n");
strFormat += _T("Content-Type: application/msword");
strFormat += _T("/r/n/r/n");
strData.Format(strFormat, strBoundary,strURL,strBoundary,strFileName);//
return strData;
}
CString CHotpimUploadDlg::MakePostFileData(CString &strBoundary)//发送请求包
{
CString strFormat;
CString strData;
strFormat = _T("/r/n");
strFormat += _T("--%s--");
strFormat += _T("/r/n");
strData.Format(strFormat, strBoundary, strBoundary);
return strData;
}
BOOL CHotpimUploadDlg::SendTrack()//发送数据
{
CString _mFilePath;
_mFilePath = "c://Multi1.pdf";//要传的本地文件地址
int startp = _mFilePath.ReverseFind('//');
int namelen = _mFilePath.GetLength()-startp-1;
CString pcmname = _mFilePath;//.Mid(startp+1,namelen);
// USES_CONVERSION;
CInternetSession Session;
CHttpConnection *pHttpConnection = NULL;
CString defServerName,defObjectName;
DWORD dwServiceType;
INTERNET_PORT nPort ;
CFile fTrack;
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;
CString m_strRequest="http://172.17.5.*/uploadFile.php";//输入自己的URL地址
AfxParseURL(m_strRequest,dwServiceType,defServerName,defObjectName,nPort);
CFileException ex;
TCHAR sErr[1024];
if (FALSE == fTrack.Open(_mFilePath, CFile::modeRead | CFile::shareDenyWrite,&ex))
{
ex.GetErrorMessage(sErr,1024);
AfxMessageBox(_T("Unable to open the file."));
return FALSE;
}
int iRecordID = 1;
strHTTPBoundary = _T("---------------------------7b4a6d158c9");//定义边界值
CString sURL=("vivian");
strPreFileData = MakePreFileData(strHTTPBoundary, pcmname, sURL);
strPostFileData = MakePostFileData(strHTTPBoundary);
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 = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, defObjectName);//,NULL,0,NULL,NULL,INTERNET_FLAG_DONT_CACHE);
DWORD dwRet;
pHTTP->SendRequest();
pHTTP->QueryInfoStatusCode(dwRet);
if (dwRet!=HTTP_STATUS_OK)
{
pHTTP->Close();
pHttpConnection->Close();
if (dwRet == HTTP_STATUS_PROXY_AUTH_REQ)
{
INTERNET_PROXY_INFO proxyinfo;
proxyinfo.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
proxyinfo.lpszProxy = m_strRequest;
proxyinfo.lpszProxyBypass = NULL;
Session.SetOption(INTERNET_OPTION_PROXY, (LPVOID)&proxyinfo, sizeof(INTERNET_PROXY_INFO));
char strUsername[30]="vivi";
char strPassword[30]="810111";
Session.SetOption(INTERNET_OPTION_PROXY_USERNAME, strUsername, strlen(strUsername)+1);
Session.SetOption(INTERNET_OPTION_PROXY_PASSWORD, strPassword, strlen(strPassword)+1);
pHttpConnection = Session.GetHttpConnection(defServerName,nPort);
pHTTP = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, defObjectName);//,NULL,0,NULL,NULL,INTERNET_FLAG_DONT_CACHE);
pHTTP->SendRequest();
pHTTP->QueryInfoStatusCode(dwRet);
if (dwRet==HTTP_STATUS_PROXY_AUTH_REQ)
return FALSE;
}
else
{
CString sErr;
sErr.Format("Error code is %d",dwRet);
return FALSE;
}
}
pHTTP->Close();
pHTTP = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, defObjectName);//,NULL,0,NULL,NULL,INTERNET_FLAG_DONT_CACHE);
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;
}
客户端:
服务端:uploadFile.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>文件上传</title>
</head>
<body>
<?
$uploaddir = 'D:/uploads/';
$uploadfile = $uploaddir . basename($_FILES['filePath']['name']);
if (move_uploaded_file($_FILES['filePath']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded./n";
}
?>
<div align="center">
<form enctype="multipart/form-data" action="uploadFile.php" method="post">
<input type="file" name="filePath" style="width:300px; height:30px"/><input type="submit" value="上传" style="height:30px"/>
</form>
</div>
</body>
</html>