• Log in or sign up for an account here: https://www.sythe.org/login

C++ and Web Leeching...

Esuom

Guru

Banned
Joined
May 3, 2007
Sythe Gold
0
Messages
1,731
Location
EST Time Zone.
Ok, I am quite new to C++ and I have a program that I want to make/learn while doing...Basically, it gets a number from a web site and displays it. It sounds simple, but I am having some trouble...

#1. I don't know how to get C++ to access the internet...at all. I'm not looking for a full explanation (although one would be nice :D), but more for direction to a simple tutorial somewhere online...


#2. After I have the source to the page, how would I go about leeching the data? It is between


Code:
Data 1:</td><td>
and...
Code:
</td></tr><tr><td>Data 2:
For example.
Code:
Data 1:</td><td>553</td></tr><tr><td>Data 2:
So I am thinking there may be someway to find what is in between those two.




I will probably have more questions after this, but for now, thanks.
 
Le Monkey
PHP Shit Show 2k26
Potamus
Prove it!
Sythe Awards 2011 Winner
Tier 1 Prizebox
Tortoise Penis
Trole
UWotM8?
Wait, do you not have an Archer rank?
C++ GetPage (by me):
Code:
#include <windows.h>
#include <wininet.h>
#pragma comment(lib,"wininet.lib")
#include <string>
using namespace std;

string GetPage(LPCSTR server, LPCSTR path)
{
	DWORD dwRead;
	HINTERNET hSession = InternetOpen("GetPage/1.0",INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
	HINTERNET hConnection = InternetConnect(hSession,server,INTERNET_DEFAULT_HTTP_PORT,"","",INTERNET_SERVICE_HTTP,0,0);
	if(!hConnection)
	{
		MessageBox(NULL,"An error occured while trying to connect to the host specified.",NULL,MB_OK);
		SetLastError(ERROR_CONNECTION_INVALID);
		return "Error opening site";
	}
	HINTERNET hData = HttpOpenRequest(hConnection,"GET",path,NULL,NULL,NULL,INTERNET_FLAG_KEEP_CONNECTION,0);
	HttpSendRequest(hData,NULL,0,NULL,0);
	CHAR pageContents[2048];
	string retVal;
	while(InternetReadFile(hData,pageContents,255,&dwRead))
	{
		if(dwRead==0)
		{
			break;
		}
		pageContents[dwRead]=0;
		retVal+=pageContents;
	}
	InternetCloseHandle(hConnection);
	InternetCloseHandle(hSession);
	return retVal;
}

Use std::string searching and substring methods to parse out the number:
http://www.cplusplus.com/reference/string/string/
 
yeah or use regular expressions to get specific substring of a specified string.
 
SMR, that was helpful, but if you have time, could you throw in comments? I don't understand 100% of it and this is a learning thing for me.
 
Le Monkey
PHP Shit Show 2k26
Potamus
Prove it!
Sythe Awards 2011 Winner
Tier 1 Prizebox
Tortoise Penis
Trole
UWotM8?
Wait, do you not have an Archer rank?
Code:
#include <windows.h>
#include <wininet.h>
#pragma comment(lib,"wininet.lib")
#include <string>
/* These are, as you probably know, the includes for the program.  The wininet.lib is linked to so that the WinInet APIs are available. */
using namespace std;

string GetPage(LPCSTR server, LPCSTR path) // LPCSTR is a Win32 type that's equivalent to const char*
{
	DWORD dwRead;
	HINTERNET hSession = InternetOpen("GetPage/1.0",INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
	HINTERNET hConnection = InternetConnect(hSession,server,INTERNET_DEFAULT_HTTP_PORT,"","",INTERNET_SERVICE_HTTP,0,0);
/* The above two lines are WinInet API calls.  The first creates the HTTP client with the user-agent GetPage/1.0.  The second connects to the HTTP address specified in the second parameter (server). */
	if(!hConnection)
	{
		MessageBox(NULL,"An error occured while trying to connect to the host specified.",NULL,MB_OK);
		SetLastError(ERROR_CONNECTION_INVALID);
		return "Error opening site";
	}
/* If the Internet connection failed or the server doesn't exist, then tell the user so and set the last system error to invalid connection.  Useful for debugging. */
	HINTERNET hData = HttpOpenRequest(hConnection,"GET",path,NULL,NULL,NULL,INTERNET_FLAG_KEEP_CONNECTION,0);
/* Uses the HTTP GET command to retrieve the page specified by the parameter following it (replace path with the remote file path, eg "/index.php"). */
	HttpSendRequest(hData,NULL,0,NULL,0);
// so that hData can be used as a handle to InternetReadFile
	CHAR pageContents[2048];
	string retVal;
	while(InternetReadFile(hData,pageContents,255,&dwRead))
	{
		if(dwRead==0)
		{
			break;
		}
		pageContents[dwRead]=0;
		retVal+=pageContents;
// puts the contents into the retVal string and then when the end of the remote file is reached, break and stop.
	}
	InternetCloseHandle(hConnection);
	InternetCloseHandle(hSession);
// free up memory used by the handles you initialized.
	return retVal;
//return the string containing the page contents.
}
 
Runescape Gamblingrunescape gambling
Back
Top