Discussion:
InternetCrackUrl error 87 (yes, again...)
(too old to reply)
c***@gmail.com
2005-09-21 06:10:03 UTC
Permalink
Ok folks, there are a lot of people out there who have the same problem
I am currently facing. I don't know if they have solved their problem,
but all solutions I found have not proven effective for me, so now I'm
asking the pros directly. Please take a look and let me know what you
think (even if it's more than just InternetCrackUrl). The biggest
problem with this code is that I get an error 87 (Incorrect parameter)
from InternetCrackUrl and nothing I do fixes that.


The idea of the program is to read URLs from a file and display the
HTML that corresponds with the URL. That's *all* so far and I still
can't get it to work. Please let me know what you think about my
approach to the problem as well as any advice to move forward *very*
quickly.

Your opinions are very much appreciated. Thank you for the assistance.

-Jed Mitten

// some standard headers
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <cassert>
using namespace std;
// windows headers
#include <windows.h>
#include <wininet.h>

#define DEBUG

void ErrorExit( LPTSTR );

int main()
{
// Check for existing internet connection
DWORD dwConPresent = InternetAttemptConnect( 0 );

LPCTSTR sUrl = "http://www.ncfta.net";
if( !InternetCheckConnection( sUrl, FLAG_ICC_FORCE_CONNECTION,
dwConPresent ) )
ErrorExit( "InternetCheckConnection" );

#ifdef DEBUG
cout << "Ping Success! Successfully pinged " << sUrl << endl;
#endif
// Get the file name
string fName;
cout << "Please enter the path of the list file\n: ";
cin >> fName;
// open the list of sites
ifstream listFile( fName.c_str() );
if( listFile == NULL )
{
cerr << "File does not exist: " << fName << endl;
exit( 1 );
}

while( !listFile.eof() )
{
// read 1 line at a time to get the url
char url[256];
TCHAR fixedUrl[256];
DWORD val = 256;
LPDWORD pval = &val;
listFile.getline( url, 255 );
#ifdef DEBUG
cout << "Read: " << url << endl;
#endif
InternetCanonicalizeUrl( url, fixedUrl, pval, NULL );
string sUrl = fixedUrl;
URL_COMPONENTS urlComponents;
LPCTSTR szUrl = sUrl.c_str();
memset(&urlComponents, 0, sizeof(URL_COMPONENTS));
urlComponents.dwStructSize = sizeof( urlComponents );
urlComponents.dwSchemeLength = 1;
urlComponents.dwHostNameLength = 1;
urlComponents.dwUrlPathLength = 1;
if( !InternetCrackUrl( szUrl, sUrl.length(), NULL, &urlComponents ) )
ErrorExit( "InternetCrackUrl" );
;
//try the site, read the text and try to match keywords
// Read the page
LPCSTR pEntity = "None";
// test internet connection
HINTERNET hOpen = InternetOpen( pEntity, INTERNET_OPEN_TYPE_DIRECT,
NULL, NULL, 0 );

if( hOpen == NULL )
{
ErrorExit( "InternetOpen" );
}

// Initialize a session with the domain
HINTERNET hConnect = InternetConnect( hOpen,
urlComponents.lpszHostName,
INTERNET_SERVICE_HTTP, urlComponents.lpszUserName,
urlComponents.lpszPassword,
urlComponents.nPort, NULL, NULL );

if( hConnect == NULL )
{
ErrorExit( "InternetConnect" );
}

// Make the GET request
HINTERNET hGet = HttpOpenRequest( hConnect, NULL,
urlComponents.lpszUrlPath, NULL,
NULL, NULL, NULL, 0 );
if( hGet == NULL )
ErrorExit( "HttpOpenRequest" );

// Success!! Now download the page
if( !HttpSendRequest( hGet, NULL, 0, NULL, NULL ) )
ErrorExit( "HttpSendRequest" );

TCHAR pageBuf[1024];
DWORD dwBytesRead;
if( !InternetReadFile( hGet, pageBuf, 1022, &dwBytesRead ) )
ErrorExit( "InternetReadFile" );

#ifdef DEBUG
cout << pageBuf << endl;
#endif
// Wrap up the Handle
if( !InternetCloseHandle( hOpen ) )
{
ErrorExit( "InternetCloseHandle( hOpen )" );
}
if( !InternetCloseHandle( hConnect ) )
{
ErrorExit( "InternetCloseHandle( hConnect )" );
}

}

return 0;
}

void ErrorExit(LPTSTR lpszFunction)
{
TCHAR szBuf[80];
LPVOID lpMsgBuf;
DWORD dw = GetLastError();

FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );

wsprintf(szBuf,
"%s failed with error %d: %s",
lpszFunction, dw, lpMsgBuf);

cerr << szBuf << endl;
//MessageBox(NULL, szBuf, "Error", MB_OK);

//LocalFree(lpMsgBuf);
ExitProcess(dw);
}
gp
2005-11-08 06:27:19 UTC
Permalink
hi jed,
i don have the solution though, but waht i see is ther is a mix of Unicode
and Ansi here.. you are using LPTSTR some here and using char in some
places. And i don know ur project settings..which char set u r using.. any
way there is a remote possibility of char set not matching

-gP
Post by c***@gmail.com
Ok folks, there are a lot of people out there who have the same problem
I am currently facing. I don't know if they have solved their problem,
but all solutions I found have not proven effective for me, so now I'm
asking the pros directly. Please take a look and let me know what you
think (even if it's more than just InternetCrackUrl). The biggest
problem with this code is that I get an error 87 (Incorrect parameter)
from InternetCrackUrl and nothing I do fixes that.
The idea of the program is to read URLs from a file and display the
HTML that corresponds with the URL. That's *all* so far and I still
can't get it to work. Please let me know what you think about my
approach to the problem as well as any advice to move forward *very*
quickly.
Your opinions are very much appreciated. Thank you for the assistance.
-Jed Mitten
// some standard headers
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <cassert>
using namespace std;
// windows headers
#include <windows.h>
#include <wininet.h>
#define DEBUG
void ErrorExit( LPTSTR );
int main()
{
// Check for existing internet connection
DWORD dwConPresent = InternetAttemptConnect( 0 );
LPCTSTR sUrl = "http://www.ncfta.net";
if( !InternetCheckConnection( sUrl, FLAG_ICC_FORCE_CONNECTION,
dwConPresent ) )
ErrorExit( "InternetCheckConnection" );
#ifdef DEBUG
cout << "Ping Success! Successfully pinged " << sUrl << endl;
#endif
// Get the file name
string fName;
cout << "Please enter the path of the list file\n: ";
cin >> fName;
// open the list of sites
ifstream listFile( fName.c_str() );
if( listFile == NULL )
{
cerr << "File does not exist: " << fName << endl;
exit( 1 );
}
while( !listFile.eof() )
{
// read 1 line at a time to get the url
char url[256];
TCHAR fixedUrl[256];
DWORD val = 256;
LPDWORD pval = &val;
listFile.getline( url, 255 );
#ifdef DEBUG
cout << "Read: " << url << endl;
#endif
InternetCanonicalizeUrl( url, fixedUrl, pval, NULL );
string sUrl = fixedUrl;
URL_COMPONENTS urlComponents;
LPCTSTR szUrl = sUrl.c_str();
memset(&urlComponents, 0, sizeof(URL_COMPONENTS));
urlComponents.dwStructSize = sizeof( urlComponents );
urlComponents.dwSchemeLength = 1;
urlComponents.dwHostNameLength = 1;
urlComponents.dwUrlPathLength = 1;
if( !InternetCrackUrl( szUrl, sUrl.length(), NULL, &urlComponents ) )
ErrorExit( "InternetCrackUrl" );
;
//try the site, read the text and try to match keywords
// Read the page
LPCSTR pEntity = "None";
// test internet connection
HINTERNET hOpen = InternetOpen( pEntity, INTERNET_OPEN_TYPE_DIRECT,
NULL, NULL, 0 );
if( hOpen == NULL )
{
ErrorExit( "InternetOpen" );
}
// Initialize a session with the domain
HINTERNET hConnect = InternetConnect( hOpen,
urlComponents.lpszHostName,
INTERNET_SERVICE_HTTP, urlComponents.lpszUserName,
urlComponents.lpszPassword,
urlComponents.nPort, NULL, NULL );
if( hConnect == NULL )
{
ErrorExit( "InternetConnect" );
}
// Make the GET request
HINTERNET hGet = HttpOpenRequest( hConnect, NULL,
urlComponents.lpszUrlPath, NULL,
NULL, NULL, NULL, 0 );
if( hGet == NULL )
ErrorExit( "HttpOpenRequest" );
// Success!! Now download the page
if( !HttpSendRequest( hGet, NULL, 0, NULL, NULL ) )
ErrorExit( "HttpSendRequest" );
TCHAR pageBuf[1024];
DWORD dwBytesRead;
if( !InternetReadFile( hGet, pageBuf, 1022, &dwBytesRead ) )
ErrorExit( "InternetReadFile" );
#ifdef DEBUG
cout << pageBuf << endl;
#endif
// Wrap up the Handle
if( !InternetCloseHandle( hOpen ) )
{
ErrorExit( "InternetCloseHandle( hOpen )" );
}
if( !InternetCloseHandle( hConnect ) )
{
ErrorExit( "InternetCloseHandle( hConnect )" );
}
}
return 0;
}
void ErrorExit(LPTSTR lpszFunction)
{
TCHAR szBuf[80];
LPVOID lpMsgBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
wsprintf(szBuf,
"%s failed with error %d: %s",
lpszFunction, dw, lpMsgBuf);
cerr << szBuf << endl;
//MessageBox(NULL, szBuf, "Error", MB_OK);
//LocalFree(lpMsgBuf);
ExitProcess(dw);
}
Loading...