C++ and Web Leeching...

Discussion in 'Programming General' started by Esuom, Jan 18, 2009.

C++ and Web Leeching...
  1. Unread #1 - Jan 18, 2009 at 12:41 PM
  2. Esuom
    Joined:
    May 3, 2007
    Posts:
    1,731
    Referrals:
    2
    Sythe Gold:
    0

    Esuom Guru
    Banned

    C++ and Web Leeching...

    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.
     
  3. Unread #2 - Jan 18, 2009 at 7:28 PM
  4. 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

    C++ and Web Leeching...

    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/
     
  5. Unread #3 - Jan 18, 2009 at 8:39 PM
  6. night
    Joined:
    Apr 21, 2005
    Posts:
    60
    Referrals:
    1
    Sythe Gold:
    5

    night Member
    Banned

    C++ and Web Leeching...

    yeah or use regular expressions to get specific substring of a specified string.
     
  7. Unread #4 - Jan 18, 2009 at 8:43 PM
  8. Esuom
    Joined:
    May 3, 2007
    Posts:
    1,731
    Referrals:
    2
    Sythe Gold:
    0

    Esuom Guru
    Banned

    C++ and Web Leeching...

    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.
     
  9. Unread #5 - Jan 18, 2009 at 10:26 PM
  10. 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

    C++ and Web Leeching...

    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.
    }
     
  11. Unread #6 - Jan 18, 2009 at 10:53 PM
  12. Esuom
    Joined:
    May 3, 2007
    Posts:
    1,731
    Referrals:
    2
    Sythe Gold:
    0

    Esuom Guru
    Banned

    C++ and Web Leeching...

    Thanks. That is a lot of help. I may actually be able to write this :p
     
< vb6 on vista? Not working. | Opinions Please. :D >

Users viewing this thread
1 guest


 
 
Adblock breaks this site