vc++返回一个网站网页的状态响应码-解决了设置超时不起作用的微软问题bug


//这个线程是单独为了检测openurl是否超时的子线程
DWORD WINAPI OpenURLThread(
LPVOID lpParameter //thread data
)
{
//bOpenURLOver = false;
//pdlg->AddRecord(L"子线程");
//while(!bOpenURLOver)
//{
CHttpConnection * m_http;
CInternetSession CIS;
//strURL = L"http://www.baidu.com";
//m_http = CIS.OpenURL(strURL);

//CHttpFile * pHttpFile = (CHttpFile*) CIS.OpenURL(strURL);
//设置超时时间
CIS.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,3000);
CHttpFile * pHttpFile = NULL;
//CString strURL;
//strURL = “http://www.hnsljd.cn/news”;

TRY
{
pHttpFile = (CHttpFile*) CIS.OpenURL(strURL);

DWORD dwordStatus;
pHttpFile->QueryInfoStatusCode(dwordStatus);
if (dwordStatus == HTTP_STATUS_OK)
{
CString strStatus;
strStatus.Format(L”%d”,dwordStatus);
pdlg->m_CtrlList.SetItemText(nRow,3,L”OK(” + strStatus + “)”);
}
else
{
CString strStatus;
strStatus.Format(L”%d”,dwordStatus);
pdlg->m_CtrlList.SetItemText(nRow,3,L”网址可能已失效(” + strStatus +”)”);
}
}
CATCH(CInternetException,e)
{
//AfxMessageBox(L”异常”);
pdlg->m_CtrlList.SetItemText(nRow,3,L”异常”);
//bOpenURLOver = true;
}
END_CATCH
//bOpenURLOver = true;
//return 0;
//}

//bOpenURLOver = true;
//pdlg->AddRecord(L”子线程over”);
return 0;
}

这里是你的过程处理函数,用来访问一个网址,部分变量请自己补全吧

//创建线程吧
HANDLE hThread2;
hThread2 = CreateThread(NULL,0,OpenURLThread,NULL,0,NULL);
//m_bSubmit = true;
//CloseHandle(hThread2);

if(WaitForSingleObject(hThread2,5000)==WAIT_TIMEOUT)
{
pdlg->m_CtrlList.SetItemText(nTotalItem,3,L”5秒超时异常”);

// Wait until the worker thread exits
//WaitForSingleObject ( hThread2, INFINITE );
//cout << “Thread has exited” << endl;
//return ;
}

微软的chttpfile的openurl超时问题,微软官方已证实是一个bug,自己开始搜索了下解决办法,然后就找了个这么个办法,自己证实此法有效!
代码不是很系统,请您自己查找补全吧。绝对可用!
vc++返回一个网站网页的状态响应码-解决了设置超时不起作用的微软问题bug

Leave a Reply