18,356
社区成员
发帖
与我相关
我的任务
分享
USES_CONVERSION;
CInternetSession Session;
CHttpConnection *pHttpConnection = NULL;
INTERNET_PORT nPort = 8080;
CFile fTrack;
CHttpFile* pHTTP;
CString strHTTPBoundary = _T("BSQHttpBoundary");
CString strPreFileData;
CString strPostFileData;
DWORD dwTotalRequestLength;
DWORD dwChunkLength;
DWORD dwReadLength;
DWORD dwResponseLength;
TCHAR szError[MAX_PATH];
void* pBuf;
LPSTR szResponse;
CString strResponse;
BOOL blnSuccess = TRUE;
CString strDebugMessage;
if (FALSE == fTrack.Open(strFilePath, CFile::modeRead | CFile::shareDenyWrite))
{
AfxMessageBox(_T("Unable to open the file."));
return FALSE;
}
CString strFileName = strFilePath.Mid(strFilePath.ReverseFind('\\') + 1);
strPreFileData = MakePreFileData(strHTTPBoundary, strFileName);
strPostFileData = MakePostFileData(strHTTPBoundary);
dwTotalRequestLength = strPreFileData.GetLength() + strPostFileData.GetLength() + fTrack.GetLength();
dwChunkLength = 64 * 1024;
pBuf = malloc(dwChunkLength);
if (NULL == pBuf)
return FALSE;
try
{
pHttpConnection = Session.GetHttpConnection(defServerName,nPort);
pHTTP = pHttpConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, defObjectName);
pHTTP->AddRequestHeaders(MakeRequestHeaders(strHTTPBoundary));//"Content-Type: multipart/form-data; boundary=%s\r\n"
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(pBuf, dwChunkLength);
if (0 != dwReadLength)
pHTTP->Write(pBuf, 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(); <-- 这里一直都是“0”
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);
blnSuccess = FALSE;
}
pHTTP->Close();
delete pHTTP;
fTrack.Close();
if (NULL != pBuf)
{
free(pBuf);
}
return blnSuccess;
private void writeXmlToResp(HttpServletResponse resp,String xml)
{
try{
resp.setContentType("text/html;charset=GBK");
resp.setHeader("Cache-Control","no-cache");
PrintWriter out=resp.getWriter();
out.write(xml);
out.flush();
out.close();
}catch(Exception e){e.printStackTrace();}
}