您的位置:首页 > 理论基础 > 计算机网络

关于设置Http请求超时的办法

2005-11-15 17:59 429 查看
不太好找。

BOOL CCrawlThread::InitInetSession()
{
DWORD dwContext = m_nIndex+1;
// DWORD dwContext = 0;
try
{
m_pSession = new CMyInetSession(AgentName, dwContext);
m_pSession->m_pMainWnd = m_pMainWnd; //设置父窗口
int ntimeOut = 30; // very important, can cause a Server time-out if set to low
// or hang the thread if set to high.

//The time-out value in milliseconds to use for Internet connection requests.
//If a connection request takes longer than this timeout, the request is canceled.
//The default timeout is infinite.
// m_pSession->SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,1000* ntimeOut);
m_pSession->SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,1000*10);
m_pSession->SetOption(INTERNET_OPTION_DATA_RECEIVE_TIMEOUT, 1000*15);
m_pSession->SetOption(INTERNET_OPTION_DATA_SEND_TIMEOUT, 1000*10);

// The delay value in milliseconds to wait between connection retries.
m_pSession->SetOption(INTERNET_OPTION_CONNECT_BACKOFF,1000);

// The retry count to use for Internet connection requests. If a connection
// attempt still fails after the specified number of tries, the request is canceled.
// The default is five.
m_pSession->SetOption(INTERNET_OPTION_CONNECT_RETRIES,2);
// m_pSession->EnableStatusCallback(TRUE);
// m_pSession->EnableStatusCallback(FALSE); //出系统错,不知道为什么

}
catch (CInternetException* pEx)
{
// catch errors from WinINet
// pEx->ReportError();
m_pSession = NULL;
pEx->Delete();

return FALSE ;
}

return TRUE;
}

/////////////////////////////////////////
// 检查新的 URL 状态
DWORD CCrawlThread::CheckHttpStatus(LPCTSTR strURL)
{
// Split apart the URL
CString strServer,strObject,strUser,strPassword, strLen;
INTERNET_PORT nPort = INTERNET_DEFAULT_HTTP_PORT;
DWORD dwServiceType, dwRet;

AfxParseURLEx(strURL,dwServiceType,strServer,strObject,nPort,strUser,
strPassword,ICU_NO_ENCODE);

CString strResult;

CHttpConnection* pServer = NULL;
CHttpFile* pFile = NULL;

try
{
pServer = m_pSession->GetHttpConnection(strServer,nPort);
}
catch (CInternetException* pEx)
{
//pEx->ReportError();
dwRet = pEx->m_dwError;
pServer = NULL;
pEx->Delete();
}

if (pServer == NULL) return 1;

try
{
pFile = pServer->OpenRequest(_T("GET"),strObject,NULL, m_nIndex+1, NULL, NULL, dwHttpRequestFlags);
// pFile = pServer->OpenRequest(_T("GET"),strObject,NULL, 0, NULL, NULL, dwHttpRequestFlags);
pFile->SendRequest();
pFile->QueryInfoStatusCode(dwRet);
}

catch (CInternetException* pEx)
{
//pEx->ReportError();
dwRet = pEx->m_dwError;
pFile = NULL;
pEx->Delete();
// m_strStatus.Format(_T("打开http: %d"), dwRet);
}

if (pFile == NULL)
{
pServer->Close();
delete pServer;
return 2;
}

pFile->Close();
pServer->Close();

delete pFile;
delete pServer;

return dwRet;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐