Jakein2006
2006-12-05 09:09:31 UTC
I want to use the following code to detect internet connection:
but when the program run into the workfunction, it return 1, so when the
internet connection is good, it return false, can you help me identify what
is wrong with my program.
I appreciate your help.
BOOL
bOnline=m_VirtualEarth.OnLine(TEXT("dev.virtualearth.net/mapcontrol/v3/mapcontrol.js"));
HINTERNET g_hRequest = NULL;
BOOL CVirtualEarth::OnLine(CString url)
{
int index,nLen;
CString strHost = url;
CString strObject = "";
nLen = url.GetLength();
index = url.Find(TEXT("//"));
if (index != -1)
{
strHost = url.Left(index);
if (nLen>index)
{
strObject = url.Right(nLen-index);
}
}
HINTERNET hInetSession = NULL;
HINTERNET hHttpConnection = NULL;
HINTERNET hRequest = NULL;
if(!(hInetSession = InternetOpen(TEXT("HtmlStream"),
INTERNET_OPEN_TYPE_PRECONFIG,
NULL,
NULL,
0)))
{
AfxMessageBox(_T("InternetOpen failed"));
return FALSE;
}
DWORD flag = INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE |
INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_KEEP_CONNECTION;
hHttpConnection = InternetConnect(hInetSession, strHost,
INTERNET_DEFAULT_HTTP_PORT,
NULL,
NULL,
INTERNET_SERVICE_HTTP,
0,
0);
g_hRequest = HttpOpenRequest(hHttpConnection, TEXT("GET"), strObject,
TEXT("HTTP/1.0"), NULL, NULL, flag, 0);
if(g_hRequest == NULL)
{
InternetCloseHandle(hHttpConnection);
InternetCloseHandle(hInetSession);
return FALSE;
}
DWORD dwTimeout;
DWORD m_uThreadId;
HANDLE hThread = CreateThread(NULL,
0,
(LPTHREAD_START_ROUTINE)CVirtualEarth::WorkFunction,
this,
0,
&m_uThreadId);
dwTimeout = 25000;
if ( WaitForSingleObject ( hThread, dwTimeout ) == WAIT_TIMEOUT )
{
CString str;
str.Format(TEXT("Can not connect to http server in %d
milliseconds"),dwTimeout);
AfxMessageBox(str);
if ( hInetSession )
InternetCloseHandle ( hInetSession );
// Wait until the worker thread exits
WaitForSingleObject ( hThread, INFINITE );
return FALSE;
}
DWORD dwExitCode = 0;
if ( !GetExitCodeThread( hThread, &dwExitCode ) )
{
return FALSE;
}
CloseHandle (hThread);
if ( dwExitCode )
// Worker function failed
return FALSE;
if (hRequest)
{
InternetCloseHandle(hRequest);
}
if (hHttpConnection)
{
InternetCloseHandle(hHttpConnection);
}
if (hInetSession)
{
InternetCloseHandle(hInetSession);
}
return TRUE;
}
DWORD CVirtualEarth::WorkFunction(LPVOID lpParam)
{
BOOL b = HttpSendRequest(g_hRequest, NULL, 0, NULL, 0);
if (!b)
{
return 1;
}
return 0;
}
but when the program run into the workfunction, it return 1, so when the
internet connection is good, it return false, can you help me identify what
is wrong with my program.
I appreciate your help.
BOOL
bOnline=m_VirtualEarth.OnLine(TEXT("dev.virtualearth.net/mapcontrol/v3/mapcontrol.js"));
HINTERNET g_hRequest = NULL;
BOOL CVirtualEarth::OnLine(CString url)
{
int index,nLen;
CString strHost = url;
CString strObject = "";
nLen = url.GetLength();
index = url.Find(TEXT("//"));
if (index != -1)
{
strHost = url.Left(index);
if (nLen>index)
{
strObject = url.Right(nLen-index);
}
}
HINTERNET hInetSession = NULL;
HINTERNET hHttpConnection = NULL;
HINTERNET hRequest = NULL;
if(!(hInetSession = InternetOpen(TEXT("HtmlStream"),
INTERNET_OPEN_TYPE_PRECONFIG,
NULL,
NULL,
0)))
{
AfxMessageBox(_T("InternetOpen failed"));
return FALSE;
}
DWORD flag = INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE |
INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_KEEP_CONNECTION;
hHttpConnection = InternetConnect(hInetSession, strHost,
INTERNET_DEFAULT_HTTP_PORT,
NULL,
NULL,
INTERNET_SERVICE_HTTP,
0,
0);
g_hRequest = HttpOpenRequest(hHttpConnection, TEXT("GET"), strObject,
TEXT("HTTP/1.0"), NULL, NULL, flag, 0);
if(g_hRequest == NULL)
{
InternetCloseHandle(hHttpConnection);
InternetCloseHandle(hInetSession);
return FALSE;
}
DWORD dwTimeout;
DWORD m_uThreadId;
HANDLE hThread = CreateThread(NULL,
0,
(LPTHREAD_START_ROUTINE)CVirtualEarth::WorkFunction,
this,
0,
&m_uThreadId);
dwTimeout = 25000;
if ( WaitForSingleObject ( hThread, dwTimeout ) == WAIT_TIMEOUT )
{
CString str;
str.Format(TEXT("Can not connect to http server in %d
milliseconds"),dwTimeout);
AfxMessageBox(str);
if ( hInetSession )
InternetCloseHandle ( hInetSession );
// Wait until the worker thread exits
WaitForSingleObject ( hThread, INFINITE );
return FALSE;
}
DWORD dwExitCode = 0;
if ( !GetExitCodeThread( hThread, &dwExitCode ) )
{
return FALSE;
}
CloseHandle (hThread);
if ( dwExitCode )
// Worker function failed
return FALSE;
if (hRequest)
{
InternetCloseHandle(hRequest);
}
if (hHttpConnection)
{
InternetCloseHandle(hHttpConnection);
}
if (hInetSession)
{
InternetCloseHandle(hInetSession);
}
return TRUE;
}
DWORD CVirtualEarth::WorkFunction(LPVOID lpParam)
{
BOOL b = HttpSendRequest(g_hRequest, NULL, 0, NULL, 0);
if (!b)
{
return 1;
}
return 0;
}
--
jake
jake