WinInet basics

Discussion in 'Programming General' started by Govind, Oct 4, 2010.

WinInet basics
  1. Unread #1 - Oct 4, 2010 at 7:39 PM
  2. Govind
    Joined:
    Apr 22, 2005
    Posts:
    7,825
    Referrals:
    13
    Sythe Gold:
    23
    Prove it! Trole Tier 1 Prizebox Tortoise Penis Le Monkey UWotM8? Wait, do you not have an Archer rank? Potamus

    Govind The One Musketeer
    Mudkips Highly Respected Retired Administrator

    WinInet basics

    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.
     
  3. Unread #2 - Oct 9, 2010 at 6:37 PM
  4. blindkilla
    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord

    blindkilla Guru
    $25 USD Donor New

    WinInet basics

    Thanks for sharing SMR!
     
< Links allways open in default browser:/ | First legit language [ Read ] >

Users viewing this thread
1 guest


 
 
Adblock breaks this site