检测网页是否可以打开, 再使用IE打开网页
//检测是否能连接网页 BOOL CanLinkWebPage(string strUrl) { /*clock_t start, finish; double duration; start = clock(); char buf[100];*/ //使用mfc的CHttpFile来检测 BOOL CanLink = FALSE; CInternetSession session("HttpClient"); session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 5000); // 5000毫秒的连接超时 session.SetOption(INTERNET_OPTION_SEND_TIMEOUT, 5000); // 5000毫秒的发送超时 session.SetOption(INTERNET_OPTION_RECEIVE_TIMEOUT, 5000); // 5000毫秒的接收超时 session.SetOption(INTERNET_OPTION_DATA_SEND_TIMEOUT, 5000); // 5000毫秒的发送超时 session.SetOption(INTERNET_OPTION_DATA_RECEIVE_TIMEOUT, 5000); // 5000毫秒的接收超时 session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1); // 1次重试 try { CHttpFile* pFile = (CHttpFile*)session.OpenURL(strUrl.c_str(), 1, INTERNET_FLAG_RELOAD | INTERNET_FLAG_TRANSFER_BINARY); DWORD dwStatusCode; pFile-> QueryInfoStatusCode(dwStatusCode); /*sprintf_s(buf, "%s返回状态码是:%lu", strUrl.c_str(), dwStatusCode); UpdateDebugLogToFile(buf);*/ //200 302 206状态码都可以 if(dwStatusCode == HTTP_STATUS_OK || dwStatusCode == HTTP_STATUS_REDIRECT || dwStatusCode == HTTP_STATUS_PARTIAL_CONTENT) CanLink = TRUE; pFile -> Close(); delete pFile; } catch (CException* e) { e->Delete(); } session.Close(); /*finish = clock(); duration = (double)(finish - start) / CLOCKS_PER_SEC; sprintf_s(buf, "%s%s%f%s, CanLink值:%d", strUrl.c_str()," 连接时间: ", duration, "秒", CanLink); UpdateDebugLogToFile(buf);*/ return CanLink; } //测试几个URL的速度 unsigned __stdcall TestUrlSpeed(void* _pParam) { string _Url; string* _pStr = (string*)_pParam; if (_pStr) { _Url = *_pStr; if(_pStr != NULL) { delete _pStr; _pStr = NULL; } } if(CanLinkWebPage(_Url)) return 1; else return 0; } // 打开网页 unsigned __stdcall OpenUrlUseIEThread(void* _pParam) { string _Url; string* _pStr = (string*)_pParam; if (_pStr) { _Url = *_pStr; SAFE_DELETE(_pStr); } if (!_Url.empty()) { vector<string> _UrlList; CVarTypeCvt::split_by(_Url,_UrlList,';'); vector<string> urls; for (INT _Idx = 0; _Idx < (INT)_UrlList.size(); _Idx++) { _Url = _UrlList[_Idx]; CVarTypeCvt::trim(_Url," \t\r\n"); /*if (!_Url.empty() && CanOpenWebPage(_Url)) { ::ShellExecute(NULL, "open",_Url.c_str(), NULL, NULL, SW_SHOWNORMAL); break; }*/ if (!_Url.empty()) { urls.push_back(_Url); } } HANDLE* pHandles = new HANDLE[urls.size()]; for(int i = 0; i < urls.size(); ++i) { unsigned _ThreadID; HANDLE _hThread = (HANDLE)_beginthreadex(NULL,NULL,TestUrlSpeed,(void*)new string(urls[i]),NULL,&_ThreadID); pHandles[i] = _hThread; } DWORD dw = WaitForMultipleObjects(urls.size(), pHandles, FALSE, 10000); if(dw != WAIT_FAILED && dw != WAIT_TIMEOUT) { DWORD dwRet; GetExitCodeThread(pHandles[dw - WAIT_OBJECT_0], &dwRet); if(dwRet) { ::ShellExecute(NULL, "open",urls[dw - WAIT_OBJECT_0].c_str(), NULL, NULL, SW_SHOWNORMAL); } } for(int i = 0; i < urls.size(); ++i) { if(pHandles[i] != NULL) { CloseHandle(pHandles[i]); pHandles[i] = NULL; } } delete [] pHandles; pHandles = NULL; } return 0; } void OpenUrlUseIE(string _Url, BOOL _Async = TRUE) { if (_Url.empty()) return; if (_Async) { unsigned _ThreadID; HANDLE _hThread = (HANDLE)_beginthreadex(NULL,NULL,OpenUrlUseIEThread,(void*)new string(_Url),NULL,&_ThreadID); CloseHandle(_hThread); _hThread = NULL; } else { ::ShellExecute(NULL, "open",_Url.c_str(), NULL, NULL, SW_SHOWNORMAL); } }