求wininet异步下载网页的资料 ( 积分: 200 )

  • 主题发起人 主题发起人 coolice823
  • 开始时间 开始时间
C

coolice823

Unregistered / Unconfirmed
GUEST, unregistred user!
小弟在做一个项目,使用wininet异步下载网页的时候遇到了难题,求各位高手提供这方面的资料,有相关源码最好,谢谢!
 
这是上传的,改一下就可以了
void CTask::DoTaskHTTP()
{
OnTaskBegin();
short sTaskID = nTaskID;
DWORD dwctxConn = nTaskID << 16 | 1;
DWORD dwctxReq = nTaskID << 16 | 2;


hInstance = InternetOpen(&quot;test&quot;,
INTERNET_OPEN_TYPE_PRECONFIG,
NULL,
NULL,
INTERNET_FLAG_ASYNC|INTERNET_FLAG_SECURE);

if (hInstance == NULL)
{
TRACELV(LEVEL_Ingore,&quot;InternetOpen failed, error %d&quot;, GetLastError());
OnTaskFailed(e_TaskInitInetFailed);
return;
}
TRACELV(LEVEL_Ingore,&quot;InternetOpen suc/n&quot;);

m_hCallbackSetThread = ::CreateThread(NULL,0,AsyncCallbackThread,this,NULL,&amp;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,&quot;InternetConnect failed, error %d&quot;, 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,
&quot;PUT&quot;,
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,&quot;HttpOpenRequest failed, error %d/n&quot;, 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(&amp;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,
&amp;IntBuff,
NULL,
HSR_INITIATE|HSR_ASYNC ,
dwctxReq))
{
if (GetLastError() != ERROR_IO_PENDING)
{
TRACELV(LEVEL_Ingore,&quot;HttpSendRequestEx failed, error %d/n&quot;, GetLastError());;
OnTaskFailed(e_TaskSendHttpRequestFailed);
return;
}
TRACELV(LEVEL_Ingore,&quot;HttpSendRequestEx called pending successfully/n&quot;);

// 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,&quot;HttpSendRequestEx called back successfully/n&quot;);
}
if (GetState() == e_Task_Canceling)
{
OnTaskCanceled();
return;
}

DWORD dwBytesWritten = 0;
DWORD dwBytesRead = 0;
DWORD dwsumWrite = 0;
do
{

BOOL bRead = ReadFile (hLocalFile, pFileBuf, sizeof(pFileBuf),&amp;dwBytesRead, NULL);
if (!bRead)
{
TRACELV(LEVEL_2,&quot;ReadFile failed, error %d/n&quot;, GetLastError());
OnTaskFailed(e_TaskReadLocalFileFailed);
return;
}

DWORD dwBytesWritten;

if(!InternetWriteFile(m_hInetFile,
pFileBuf,
dwBytesRead,
&amp;dwBytesWritten))
{
if (GetLastError() != ERROR_IO_PENDING)
{
TRACELV(LEVEL_Ingore,&quot;InternetWriteFile failed, error %d/n&quot;, GetLastError());;
OnTaskFailed(e_TaskInetWriteFileFailed);
return;
}
else
{
TRACELV(LEVEL_Ingore,&quot;InternetWriteFile pending/n&quot;);;
// 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,&quot;HttpEndRequest pending/n&quot;);;
// 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,&quot;HttpEndRequest failed, error %d/n&quot;,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;
}
}
 
谢谢,我先试一下
 
楼上的兄台,这好像不是delphi的吧,有delphi版本的吗?
 
求呀,还有别的兄弟知道吗
 
后退
顶部