Mo
2007-06-21 00:35:35 UTC
Hello all
I am creating a utility that will check for network connection
availability (to run on WinCE device),
the idea here is to try to access a well-known URL(i.e. www.msn.com)
and download a file (index.html)
I am able to read the file via InternetOpenURL, the problem is I also
want to display
The HTTP status code as well
So I do the following in my code
InternetOpen()
InternetConnect()
HttpOpenRequest()
All these calls succeed up to this point, all handles are valid,
however HttpSendRequest()
Fails with GLE = 12007 // ERROR_INTERNET_NAME_NOT_RESOLVED
However if I call InternetOpenURL() using the same parameters (URL in
this case) I am able to read index.html successfully
another questions is what is the right way to call
nternetGetLastResponseInfo() when an WinInet function fails?
i am doing this below, however "size_needed" is set to a huge value
sometimes (something like 42544547)
dwErrorCode = GetLastError();
DWORD size_needed = 0;
InternetGetLastResponseInfo(&dwErrorCode,NULL,&size_needed);
TCHAR *message = new TCHAR [(size_needed + 1)];
InternetGetLastResponseInfo(&dwErrorCode,message,&size_needed);
Below is my code, any help is greatly appreciated
TCHAR szURL[MAX_PATH]=TEXT("http://www.msn.com/");
TCHAR szFileURL[MAX_PATH]=TEXT("index.html");
hOpenHandle =
InternetOpen(TEXT("ConnectURL.exe"),INTERNET_OPEN_TYPE_DIRECT, NULL,
NULL, NULL);
if(NULL == hOpenHandle)
{
dwErrorCode = GetLastError();
DEBUGMSG(1,(TEXT("ConnectURL: InternetOpen Failed dwErrorCode=%x
\r\n"),dwErrorCode));
}
hConnection = InternetConnect(hOpenHandle, szURL, 80,
NULL, NULL,
INTERNET_SERVICE_HTTP, 0, NULL );
hRequest = HttpOpenRequest(hConnection, TEXT("GET"),
TEXT("index.html"),
TEXT("HTTP/1.0" ), NULL,
NULL,
INTERNET_FLAG_NO_AUTO_REDIRECT |
INTERNET_FLAG_PRAGMA_NOCACHE
|
INTERNET_FLAG_NO_CACHE_WRITE, NULL );
if ( TRUE == HttpSendRequest( hRequest, NULL, 0, NULL, 0 ) == TRUE)
{
DWORD dwIndex = 0;
DWORD dwErr = 0;
TCHAR szBuff[MAX_PATH] = {0};
DWORD dwLeng = sizeof(szBuff);
if (HttpQueryInfo(hRequest, HTTP_QUERY_STATUS_CODE, &szBuff,
&dwLeng,
&dwIndex) == TRUE)
{ /// I get here 301 in szBuff !
/// do processing...
MessageBox (NULL, szBuff, szBuff, MB_OK);
}
}
else
{
dwErrorCode = GetLastError();
DEBUGMSG(1,(TEXT("ConnectURL: HttpSendRequest Failed
dwErrorCode=%x\r\n"),dwErrorCode));
}
_tcscat(szURL,szFileURL);
hResourceHandle = InternetOpenUrl(hOpenHandle ,szURL, NULL, 0,
INTERNET_FLAG_RAW_DATA , 0);
if (NULL == hResourceHandle)
{
dwErrorCode=GetLastError();
DEBUGMSG(1,(TEXT("ConnectURL: InternetOpenUrl Failed dwErrorCode=
%x\r\n"),dwErrorCode));
InternetCloseHandle(hOpenHandle);
return;
}
//read the file
BYTE buffer[1024];
while(InternetReadFile( hResourceHandle, buffer, 255, &dwRead ))
{
if ( dwRead == 0 )
{
fDone=TRUE;
break;
}
buffer[dwRead] = 0;
}
//end reading the file, close the handle
InternetCloseHandle(hResourceHandle);
InternetCloseHandle(hOpenHandle);
I am creating a utility that will check for network connection
availability (to run on WinCE device),
the idea here is to try to access a well-known URL(i.e. www.msn.com)
and download a file (index.html)
I am able to read the file via InternetOpenURL, the problem is I also
want to display
The HTTP status code as well
So I do the following in my code
InternetOpen()
InternetConnect()
HttpOpenRequest()
All these calls succeed up to this point, all handles are valid,
however HttpSendRequest()
Fails with GLE = 12007 // ERROR_INTERNET_NAME_NOT_RESOLVED
However if I call InternetOpenURL() using the same parameters (URL in
this case) I am able to read index.html successfully
another questions is what is the right way to call
nternetGetLastResponseInfo() when an WinInet function fails?
i am doing this below, however "size_needed" is set to a huge value
sometimes (something like 42544547)
dwErrorCode = GetLastError();
DWORD size_needed = 0;
InternetGetLastResponseInfo(&dwErrorCode,NULL,&size_needed);
TCHAR *message = new TCHAR [(size_needed + 1)];
InternetGetLastResponseInfo(&dwErrorCode,message,&size_needed);
Below is my code, any help is greatly appreciated
TCHAR szURL[MAX_PATH]=TEXT("http://www.msn.com/");
TCHAR szFileURL[MAX_PATH]=TEXT("index.html");
hOpenHandle =
InternetOpen(TEXT("ConnectURL.exe"),INTERNET_OPEN_TYPE_DIRECT, NULL,
NULL, NULL);
if(NULL == hOpenHandle)
{
dwErrorCode = GetLastError();
DEBUGMSG(1,(TEXT("ConnectURL: InternetOpen Failed dwErrorCode=%x
\r\n"),dwErrorCode));
}
hConnection = InternetConnect(hOpenHandle, szURL, 80,
NULL, NULL,
INTERNET_SERVICE_HTTP, 0, NULL );
hRequest = HttpOpenRequest(hConnection, TEXT("GET"),
TEXT("index.html"),
TEXT("HTTP/1.0" ), NULL,
NULL,
INTERNET_FLAG_NO_AUTO_REDIRECT |
INTERNET_FLAG_PRAGMA_NOCACHE
|
INTERNET_FLAG_NO_CACHE_WRITE, NULL );
if ( TRUE == HttpSendRequest( hRequest, NULL, 0, NULL, 0 ) == TRUE)
{
DWORD dwIndex = 0;
DWORD dwErr = 0;
TCHAR szBuff[MAX_PATH] = {0};
DWORD dwLeng = sizeof(szBuff);
if (HttpQueryInfo(hRequest, HTTP_QUERY_STATUS_CODE, &szBuff,
&dwLeng,
&dwIndex) == TRUE)
{ /// I get here 301 in szBuff !
/// do processing...
MessageBox (NULL, szBuff, szBuff, MB_OK);
}
}
else
{
dwErrorCode = GetLastError();
DEBUGMSG(1,(TEXT("ConnectURL: HttpSendRequest Failed
dwErrorCode=%x\r\n"),dwErrorCode));
}
_tcscat(szURL,szFileURL);
hResourceHandle = InternetOpenUrl(hOpenHandle ,szURL, NULL, 0,
INTERNET_FLAG_RAW_DATA , 0);
if (NULL == hResourceHandle)
{
dwErrorCode=GetLastError();
DEBUGMSG(1,(TEXT("ConnectURL: InternetOpenUrl Failed dwErrorCode=
%x\r\n"),dwErrorCode));
InternetCloseHandle(hOpenHandle);
return;
}
//read the file
BYTE buffer[1024];
while(InternetReadFile( hResourceHandle, buffer, 255, &dwRead ))
{
if ( dwRead == 0 )
{
fDone=TRUE;
break;
}
buffer[dwRead] = 0;
}
//end reading the file, close the handle
InternetCloseHandle(hResourceHandle);
InternetCloseHandle(hOpenHandle);