웹 페이지 를 열 수 있 는 지 확인 하고 IE 로 웹 페이지 를 엽 니 다.

3786 단어 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
"); /*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); } }

좋은 웹페이지 즐겨찾기