Im currently trying to write a program that will connect and login to site with http. When logged in, I want to access xml-data at the site.
I've decided to use WinHttp, but I not quite sure what to do to make it work.
The site I want to access is www.hattrick.org. Login procedure:
then then you can access for example:
hSession = WinHttpOpen( L"A WinHTTP Example Program/1.0",
WINHTTP_NO_PROXY_BYPASS, 0 );
hConnect = WinHttpConnect( hSession,
hRequest = WinHttpOpenRequest( hConnect,
I dont know what to do next. I want to access the 'other' sites to gater xmldata, but I dont know how to do it.
I am even not sure if Im logged in, I have used GetLastError() but it returns no error code.
>> Any suggestions?
Why don't you start by reading the RFC's on the protocol?
>> I want to access the 'other' sites to gater xmldata...
What 'other' sites are you referring to?
>> I am even not sure if Im logged in...
GetLastError() doesn't do anything for you. Try testing your 'hConnect' handle.
MSDN provides complete sample code for downloading a resource here.
The sample code is for downloading a resource using ssl(https), so remember to change the flags in WinHttpConnect and WinHttpOpenRequest if you do not want ssl.
Following is the sample code with highlighted changes to download the resource you specify.
DWORD dwSize = 0;
DWORD dwDownloaded = 0;
BOOL bResults = FALSE;
HINTERNET hSession = NULL,
hConnect = NULL,
hRequest = NULL;
// Use WinHttpOpen to obtain a session handle.
hSession = WinHttpOpen( L"WinHTTP Example/1.0",
WINHTTP_NO_PROXY_BYPASS, 0 );
// Specify an HTTP server.
if( hSession )
hConnect = WinHttpConnect( hSession, L"126.96.36.199",
INTERNET_DEFAULT_HTTP_PORT, 0 );
// Create an HTTP request handle.
if( hConnect )
hRequest = WinHttpOpenRequest( hConnect, L"GET", L"/Common/default.asp?action=login&loginName=USERNAME&password=PASSWORD",
// Send a request.
if( hRequest )
bResults = WinHttpSendRequest( hRequest,
0, 0 );
// End the request.
if( bResults )
bResults = WinHttpReceiveResponse( hRequest, NULL );
// Keep checking for data until there is nothing left.
if( bResults )
// Check for available data.
dwSize = 0;
if( !WinHttpQueryDataAvailable( hRequest, &dwSize ) )
printf( "Error %u in WinHttpQueryDataAvailable.\n",
GetLastError( ) );
// Allocate space for the buffer.
pszOutBuffer = new char[dwSize+1];
if( !pszOutBuffer )
printf( "Out of memory\n" );
// Read the data.
ZeroMemory( pszOutBuffer, dwSize+1 );
if( !WinHttpReadData( hRequest, (LPVOID)pszOutBuffer,
dwSize, &dwDownloaded ) )
printf( "Error %u in WinHttpReadData.\n", GetLastError( ) );
printf( "%s", pszOutBuffer );
// Free the memory allocated to the buffer.
delete  pszOutBuffer;
} while( dwSize > 0 );
// Report any errors.
if( !bResults )
printf( "Error %d has occurred.\n", GetLastError( ) );
// Close any open handles.
if( hRequest ) WinHttpCloseHandle( hRequest );
if( hConnect ) WinHttpCloseHandle( hConnect );
if( hSession ) WinHttpCloseHandle( hSession );
>> want to access the 'other' sites to gater xmldata, but I dont know how to do it.<<
Once you have "logged in" with the above page, your credentials will be stored in a cookie and you will be able to download other pages with similar code.
I have a few additional questions about the code:
* Is the login procedure all the pasted code, or just the WinHttpOpenRequest() and WinHttpSendRequest()?
>>Once you have "logged in" with the above page, your >>credentials will be stored in a cookie and you will be able to >>download other pages with similar code.
* If I've understand this correct, you can for example use WinHttpOpenRequest() with "/Common/leagueDetails.asp?outputType=XML&actionType=view&l eagueLevelUnitID=1" to access other 'sites'?
And afterwards, you use WinHttpSendRequest, WinHttpReceiveResponse, WinHttpQueryDataAvailable and WinHttpReadData to get the cookie data?
PS: Sry for the newbie questions, but I've spent hour after hour reading the msdn without doing much of progress in my programming :/
>>Is the login procedure all the pasted code, or just the WinHttpOpenRequest() and WinHttpSendRequest()?<<
Remember, the login is only another web page. The pasted code is what is required to download a web page and print it to output.
You may be able to get rid of the code after "// Keep checking for data". This would be the equivalent of pressing the stop button in your browser before the page was loaded.
If you want to load a different page pass its location to WinHttpOpenRequest instead of
>>And afterwards, you use WinHttpSendRequest, WinHttpReceiveResponse, WinHttpQueryDataAvailable and WinHttpReadData to get the cookie data?<<
WinHttp manages the cookies for you. You do not have to do anything.
When you load the login page the site will send back a cookie with your identity. WinHttp saves this cookie for later.
When you visit another page at the same site(188.8.131.52) WinHttp automatically sends the cookie back to the server. This allows the site to verify your identity that it set on the login page.
anonytmouse: Thank you!
I works just fine now!
And whats even more important, I think I understand it! =D
Hi there, is a similar method available under linux? Since the winhttp.h is available for Windows??? :confused:
A so beginneth another lesson on reading the rules before deciding to bump threads.