关于设置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;
}
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;
}
相关文章推荐
- 利用volley进行http设置请求头、超时及请求参数设置(post)
- System.Web.HttpException: 请求已超时 的解决办法。
- socket 请求接收完整的一个http响应(设置recv 接收超时选项SO_RCVTIMEO)
- 使用HttpURLConnection设置请求超时时间,请求超时时会导致自动重发。
- C# 发送HTTP请求超时解决办法
- android网络编程注意事项之一:移动网络下,防止网络超时甚至连接不上,解决办法--为网络请求设置代理
- angular学习笔记(二十六)-$http(4)-设置请求超时
- 关于通过http请求访问Linux下的ftp的问题以及解决办法
- 关于"Ueditor 请求后台配置项http错误,上传功能将不能正常使用"的解决办法
- Java HTTP请求时设置超时
- okhttp3+retrofit2进行HTTP请求以及设置请求超时示例
- 关于http socket timeout 超时时间 未设置 导致线程一直在等待(线程饥饿),微信公众号开发过程遇到的。java
- 解决办法-HTTP 错误 401.3 - 未经授权:访问由于 ACL 对所请求资源的设置被拒绝。
- socket 请求接收完整的一个http响应(设置recv 接收超时选项SO_RCVTIMEO)
- 关于HttpClinet4.5超时设置无效(一直无法返回数据)
- Android http请求数据 设置超时
- 为ServerXMLHTTP对象的HTTP请求设置超时时间
- 利用volley进行http设置请求头、超时及请求参数设置(post)
- http请求设置setConnectTimeout()方法超时无响应
- android网络编程注意事项之一:移动网络下,防止网络超时甚至连接不上,解决办法--为网络请求设置代理