这是上传的,改一下就可以了
void CTask:
oTaskHTTP()
{
OnTaskBegin();
short sTaskID = nTaskID;
DWORD dwctxConn = nTaskID << 16 | 1;
DWORD dwctxReq = nTaskID << 16 | 2;
hInstance = InternetOpen("test",
INTERNET_OPEN_TYPE_PRECONFIG,
NULL,
NULL,
INTERNET_FLAG_ASYNC|INTERNET_FLAG_SECURE);
if (hInstance == NULL)
{
TRACELV(LEVEL_Ingore,"InternetOpen failed, error %d", GetLastError());
OnTaskFailed(e_TaskInitInetFailed);
return;
}
TRACELV(LEVEL_Ingore,"InternetOpen suc/n"
;
m_hCallbackSetThread = ::CreateThread(NULL,0,AsyncCallbackThread,this,NULL,&m_dwCallBackSetThreadID);
WaitForSingleObject(m_hSetCallBackEvent,INFINITE);
hConnect = InternetConnect(hInstance,
m_strServer,
INTERNET_DEFAULT_HTTP_PORT,
NULL,
NULL,
INTERNET_SERVICE_HTTP,
INTERNET_FLAG_RELOAD,
dwctxConn);
if (hConnect == NULL)
{
if (GetLastError() != ERROR_IO_PENDING)
{
TRACELV(LEVEL_Ingore,"InternetConnect failed, error %d", GetLastError());;
OnTaskFailed(e_TaskOpenHttpConnectFailed);
return;
}
HANDLE hnds[2] ={hConnectedEvent,hDeadEvent};
DWORD dwRet = WaitForMultipleObjects(2,hnds,false,UPLOAD_TIMEOUT);
if (dwRet == WAIT_TIMEOUT)
{
OnTaskFailed(e_TaskOpenHttpConnectFailed);
return;
};
// WaitForSingleObject(hConnectedEvent, INFINITE);
}
if (GetState() == e_Task_Canceling)
{
OnTaskCanceled();
return;
}
char* destobj = getDestURLObject();
m_hInetFile = HttpOpenRequest(hConnect,
"PUT",
destobj,
NULL,
NULL,
NULL,
// INTERNET_FLAG_IGNORE_CERT_CN_INVALID |INTERNET_FLAG_SECURE | INTERNET_FLAG_IGNORE_CERT_DATE_INVALID |
INTERNET_FLAG_KEEP_CONNECTION|INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE||INTERNET_FLAG_PRAGMA_NOCACHE,
dwctxReq);
if (m_hInetFile == NULL)
{
if (GetLastError() != ERROR_IO_PENDING)
{
TRACELV(LEVEL_Ingore,"HttpOpenRequest failed, error %d/n", GetLastError());;
OnTaskFailed(e_TaskOpenHttpRequestFailed);
return;
}
HANDLE hnds[2] ={hRequestOpenCompleteEvent,hDeadEvent};
DWORD dwRet = WaitForMultipleObjects(2,hnds,false,UPLOAD_TIMEOUT);
if (dwRet == WAIT_TIMEOUT)
{
OnTaskFailed(e_TaskOpenHttpRequestFailed);
return;
};
// WaitForSingleObject(hRequestOpenCompleteEvent, INFINITE);
}
if (GetState() == e_Task_Canceling)
{
OnTaskCanceled();
return;
}
INTERNET_BUFFERS IntBuff;
FillMemory(&IntBuff, sizeof(IntBuff), 0);
IntBuff.dwStructSize= sizeof(IntBuff);
IntBuff.dwBufferTotal = m_nFileLen;
BOOL bRet = InternetSetOption(m_hInetFile,INTERNET_OPTION_USERNAME,m_taskInfo.strUserName,strlen(m_taskInfo.strUserName) + 1);
bRet = InternetSetOption(m_hInetFile,INTERNET_OPTION_PASSWORD,m_taskInfo.strPassword,strlen(m_taskInfo.strPassword) + 1);
if (!HttpSendRequestEx(m_hInetFile,
&IntBuff,
NULL,
HSR_INITIATE|HSR_ASYNC ,
dwctxReq))
{
if (GetLastError() != ERROR_IO_PENDING)
{
TRACELV(LEVEL_Ingore,"HttpSendRequestEx failed, error %d/n", GetLastError());;
OnTaskFailed(e_TaskSendHttpRequestFailed);
return;
}
TRACELV(LEVEL_Ingore,"HttpSendRequestEx called pending successfully/n"
;
// WaitForSingleObject(hRequestCompleteEvent, INFINITE);
HANDLE hnds[2] ={hRequestCompleteEvent,hDeadEvent};
DWORD dwRet = WaitForMultipleObjects(2,hnds,false,UPLOAD_TIMEOUT);
if (WAIT_FAILED == dwRet )
{
DWORD dwError = GetLastError();
OnTaskFailed(e_TaskSendHttpRequestFailed);
return;
}
if (dwRet == WAIT_TIMEOUT)
{
OnTaskFailed(e_TaskSendHttpRequestFailed);
return;
};
TRACELV(LEVEL_Ingore,"HttpSendRequestEx called back successfully/n"
;
}
if (GetState() == e_Task_Canceling)
{
OnTaskCanceled();
return;
}
DWORD dwBytesWritten = 0;
DWORD dwBytesRead = 0;
DWORD dwsumWrite = 0;
do
{
BOOL bRead = ReadFile (hLocalFile, pFileBuf, sizeof(pFileBuf),&dwBytesRead, NULL);
if (!bRead)
{
TRACELV(LEVEL_2,"ReadFile failed, error %d/n", GetLastError());
OnTaskFailed(e_TaskReadLocalFileFailed);
return;
}
DWORD dwBytesWritten;
if(!InternetWriteFile(m_hInetFile,
pFileBuf,
dwBytesRead,
&dwBytesWritten))
{
if (GetLastError() != ERROR_IO_PENDING)
{
TRACELV(LEVEL_Ingore,"InternetWriteFile failed, error %d/n", GetLastError());;
OnTaskFailed(e_TaskInetWriteFileFailed);
return;
}
else
{
TRACELV(LEVEL_Ingore,"InternetWriteFile pending/n"
;;
// WaitForSingleObject(hRequestCompleteEvent, INFINITE);
HANDLE hnds[2] ={hRequestCompleteEvent,hDeadEvent};
DWORD dwRet = WaitForMultipleObjects(2,hnds,false,UPLOAD_TIMEOUT);
if (dwRet == WAIT_TIMEOUT)
{
OnTaskFailed(e_TaskInetWriteFileFailed);
return;
}
else
{
if (dwRet == WAIT_OBJECT_0 + 1)
{
OnTaskCanceled();
return;
}
else
{
dwsumWrite += dwBytesWritten;
OnBlockTransmitSuc(dwsumWrite);
}
}
}
}
else
{
if (GetState() == e_Task_Canceling)
{
OnTaskCanceled();
return;
}
else
{
dwsumWrite += dwBytesWritten;
OnBlockTransmitSuc(dwsumWrite);
Sleep(100);
}
}
} while (sizeof(pFileBuf) == dwBytesRead);
if (!HttpEndRequest(m_hInetFile, NULL, HSR_INITIATE, dwctxReq))
{
if (GetLastError() == ERROR_IO_PENDING)
{
TRACELV(LEVEL_Ingore,"HttpEndRequest pending/n"
;;
// WaitForSingleObject(hRequestCompleteEvent, INFINITE);
HANDLE hnds[2] ={hRequestCompleteEvent,hDeadEvent};
DWORD dwRet = WaitForMultipleObjects(2,hnds,false,UPLOAD_TIMEOUT);
if (dwRet == WAIT_TIMEOUT)
{
OnTaskFailed(e_TaskRecvingHttpResponseFailed);
return;
}
else
{
if (dwRet == WAIT_OBJECT_0 + 1)
{
OnTaskCanceled();
return;
}
}
}
else
{
TRACELV(LEVEL_Ingore,"HttpEndRequest failed, error %d/n",GetLastError());
OnTaskFailed(e_TaskRecvingHttpResponseFailed);
return;
}
}
int nrtchk = CheckReqResult(m_hInetFile);
switch(nrtchk)
{
case e_HTTP_OK:
OnTaskSuc();
break;
case e_HTTP_Fail:
OnTaskFailed(e_TaskRecvedHttpResponseRejected);
break;
case e_HTTP_AuthFail:
OnAuthFail();
default:
break;
}
}