SuperABC
Gebruiker
- Lid geworden
- 16 jul 2009
- Berichten
- 327
Ik ben nu bezig te programmeren met winhttp. Het lukt zeer goed webpagina's op te vragen. Maar nu wil ik bijvoorbeeld dat er wordt ingelogd op een site. Dus moet ik op de een of andere manier een username en password meesturen, weet iemand hoe dit precies moet? Je hebt dus bijvoorbeeld dit inlog stukje code:
Dat is een stuk inlog code van www.travian.nl. Ik heb omdat het totaal niet lukte gekeken in de source van een java programma die daar wel kan inloggen. Dit is de code die ze daar gebruiken:
Als ik dus even de variabelen omzet krijg je zoiets:
username=USERNAME&password=PASSWORD&s1.x=X&s1.y=Y&s1=login&w=1280"%3A1024&login=XXXXXXX
X = RANDOM.NextInt(80)
Y = RANDOM.NextInt(20)
XXXXXXX = een code welke staat in de bron.
Bij welke http functie moet ik dit meesturen om ingelogd te komen? Kan ik hierna ook een cookie maken zodat bij latere reguest iets anders kan worden meegestuurd of hoe werkt dat precies? Hier is een heel overzicht van de functies
Deze code heb ik, en is iets aangepast van die van msdn:
[CPP]#include <windows.h>
#include <winhttp.h>
#include <iostream>
#pragma comment(lib, "winhttp.lib");
using namespace std;
int main()
{
DWORD dwSize = 0;
DWORD dwDownloaded = 0;
LPSTR pszOutBuffer;
BOOL bResults = FALSE;
HINTERNET hSession = NULL,
hConnect = NULL,
hRequest = NULL;
// Use WinHttpOpen to obtain a session handle.
hSession = WinHttpOpen( L"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)",
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS, 0 );
// Specify an HTTP server.
if( hSession )
hConnect = WinHttpConnect( hSession, L"URL", 80, 0 );
// Create an HTTP request handle.
if( hConnect )
hRequest = WinHttpOpenRequest( hConnect, L"GET", L"PAGINA",
NULL, WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
WINHTTP_FLAG_ESCAPE_DISABLE );
// Send a request.
if( hRequest )
bResults = WinHttpSendRequest( hRequest,
WINHTTP_NO_ADDITIONAL_HEADERS, 0,
WINHTTP_NO_REQUEST_DATA, 0,
0, 0 );
// End the request.
if( bResults )
bResults = WinHttpReceiveResponse( hRequest, NULL );
// Keep checking for data until there is nothing left.
if( bResults )
{
do
{
// 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" );
dwSize=0;
}
else
{
// Read the data.
ZeroMemory( pszOutBuffer, dwSize+1 );
if( !WinHttpReadData( hRequest, (LPVOID)pszOutBuffer,
dwSize, &dwDownloaded ) )
printf( "Error %u in WinHttpReadData.\n", GetLastError( ) );
else
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 );
cin.get();
return 0;
}[/CPP]
Code:
[COLOR=darkorange]<form method="post" name="snd" action="1.php">[/COLOR]
<table cellpadding="1" cellspacing="1" id="login_form">
<tbody>
<tr class="top">
<th>Naam : </th>
<td>
[COLOR=darkorange]<input class="text" type="text" name="name" value="" />[/COLOR] <span class="error"> </span>
</td>
</tr>
<tr class="btm">
<th>Wachtwoord</th>
<td>
[COLOR=darkorange]<input class="text" type="password" name="password" value=""
maxlength="20" />[/COLOR] <span class="error"> </span>
</td>
</tr>
</tbody>
</table>
<p class="btn">
[COLOR=darkorange]<input type="image" value="login" name="s1" onclick="xy();" id="btn_login" class="dynamic_img" src="img/x.gif" alt="Log-in knop" />[/COLOR]
<input type="hidden" name="w" value="" />
<input type="hidden" name="login" value="1278531883" />
</p>
[COLOR=darkorange]</form>[/COLOR]
Code:
post("/dorf1.php", userField + "=" + URLEncoder.encode(Settings.username, "UTF-8") + "&" + passwordField + "=" + URLEncoder.encode(Settings.password, "UTF-8") + "&s1.x=" + TravianData.s1X() + "&s1.y=" + TravianData.s1Y() + "&s1=login" + "&w=" + screenSize.width + "%3A" + screenSize.height + "&login=" + loginValue);
Als ik dus even de variabelen omzet krijg je zoiets:
username=USERNAME&password=PASSWORD&s1.x=X&s1.y=Y&s1=login&w=1280"%3A1024&login=XXXXXXX
X = RANDOM.NextInt(80)
Y = RANDOM.NextInt(20)
XXXXXXX = een code welke staat in de bron.
Bij welke http functie moet ik dit meesturen om ingelogd te komen? Kan ik hierna ook een cookie maken zodat bij latere reguest iets anders kan worden meegestuurd of hoe werkt dat precies? Hier is een heel overzicht van de functies
Deze code heb ik, en is iets aangepast van die van msdn:
[CPP]#include <windows.h>
#include <winhttp.h>
#include <iostream>
#pragma comment(lib, "winhttp.lib");
using namespace std;
int main()
{
DWORD dwSize = 0;
DWORD dwDownloaded = 0;
LPSTR pszOutBuffer;
BOOL bResults = FALSE;
HINTERNET hSession = NULL,
hConnect = NULL,
hRequest = NULL;
// Use WinHttpOpen to obtain a session handle.
hSession = WinHttpOpen( L"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)",
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS, 0 );
// Specify an HTTP server.
if( hSession )
hConnect = WinHttpConnect( hSession, L"URL", 80, 0 );
// Create an HTTP request handle.
if( hConnect )
hRequest = WinHttpOpenRequest( hConnect, L"GET", L"PAGINA",
NULL, WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
WINHTTP_FLAG_ESCAPE_DISABLE );
// Send a request.
if( hRequest )
bResults = WinHttpSendRequest( hRequest,
WINHTTP_NO_ADDITIONAL_HEADERS, 0,
WINHTTP_NO_REQUEST_DATA, 0,
0, 0 );
// End the request.
if( bResults )
bResults = WinHttpReceiveResponse( hRequest, NULL );
// Keep checking for data until there is nothing left.
if( bResults )
{
do
{
// 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" );
dwSize=0;
}
else
{
// Read the data.
ZeroMemory( pszOutBuffer, dwSize+1 );
if( !WinHttpReadData( hRequest, (LPVOID)pszOutBuffer,
dwSize, &dwDownloaded ) )
printf( "Error %u in WinHttpReadData.\n", GetLastError( ) );
else
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 );
cin.get();
return 0;
}[/CPP]
Laatst bewerkt: