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

WinInet basics

Govind

The One Musketeer

Highly Respected
Mudkips
Retired Administrator
Joined
Apr 22, 2005
Sythe Gold
23
Messages
7,825
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:
#define WIN32_LEAN_AND_MEAN // exclude rarely-used stuff from windows headers
#include <windows.h> //for winmain/messagebox
#include <wininet.h> //wininet api
#ifdef _MSC_VER //is this a microsoft compiler?
#pragma comment(lib, "wininet.lib") // may not be necessary on non-microsoft compilers; wininet.lib is not included when linking by default on MSVC at least
#endif // _MSC_VER
#include <string> //for retVal in GetPage
using namespace std;

string GetPage(LPCSTR server, LPCSTR path) // LPCSTR is a Win32 type that's equivalent to const char*
//note that you can put this in a static library or a DLL allowing use of it in other projects. the latter option allows it to be used even in non-C++ languages
// --- if you can get around the return type... lol
{
	DWORD dwRead;
	HINTERNET hClient = InternetOpen("GetPage/1.0",INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
	hClient = InternetConnect(hClient,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(!hClient)
	{
		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. */
	hClient = HttpOpenRequest(hClient,"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"). */
/* This is not always the verb you will use; POST is sometimes necessary as well */
	HttpSendRequest(hClient,NULL,0,NULL,0);
// so that hData can be used as a handle to InternetReadFile
	CHAR  pageContents[2048]; //amount read each loop; somewhat arbitrary
	string retVal;
	while(InternetReadFile(hClient,pageContents,255,&dwRead))
	{
		if(dwRead==0) //EOF?
		{
			break; //Quit
		}
		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(hClient);
// free up memory used by the handle you initialized.  after using this function your handle may no longer be used for wininet apis
	return retVal;
//return the string containing the page contents.
}
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	string myIP = GetPage("www.whatismyip.org","/"); //get the index of whatismyip.org
	// note that the actual remote filename is unnecessary for reading the index, just / will do.
	const char *buffer = myIP.c_str(); //convert myIP to a type compatible with MessageBox
	MessageBox(0,buffer,"Your IP is:",MB_OK|MB_ICONINFORMATION); // display IP to user
	return 0;
}

Connects to the simplest plain-text HTTP page I know of and retrieves your IP address for you. I have commented the code extensively and believe it is pretty self-explanatory.
 
Thanks for sharing SMR!
 
Runescape Gamblingrunescape gambling
Back
Top