1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
#include <windows.h>
#include <tchar.h>
#include <wininet.h>
#include <iostream>
#include <conio.h>
#include <fstream>
#include<string>
#pragma comment (lib, "wininet.lib")
using namespace std;
void Error (TCHAR *msg)
{
::MessageBox (0, msg, NULL, MB_OK | MB_ICONERROR);
exit (EXIT_FAILURE);
}
int main (int argc, char *argv[])
{
fstream fs_obj;
fs_obj.open ("temp.txt", ios::out | ios::app);
if (!fs_obj)
{
Error (_T ("Failed to open stream"));
}
HINTERNET hInternet = InternetOpenA ("InetURL/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (hInternet == NULL)
Error (_T ("Error opening internet."));
HINTERNET hConnection = InternetConnectA (hInternet, "www.cplusplus.com", 80, " ", " ", INTERNET_SERVICE_HTTP, 0, 0); //enter url here
if (hConnection == NULL)
Error (_T ("Error opening internet connection"));
HINTERNET hData = HttpOpenRequestA (hConnection, "GET", "/forum/windows/", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
if (hData == NULL)
Error (_T ("Error opening internet request"));
char buf[2048];
BOOL success = HttpSendRequestA (hData, NULL, 0, NULL, 0);
if (!success)
Error (_T ("Error sending request"));
string total;
DWORD bytesRead = 0;
DWORD totalBytesRead = 0;
while (InternetReadFile (hData, buf, 2000, &bytesRead) && bytesRead != 0)
{
buf[bytesRead] = 0; // insert the null terminator.
total = total + buf;
printf ("%d bytes read\n", bytesRead);
totalBytesRead += bytesRead;
}
fs_obj << total << "\n--------------------end---------------------\n";
fs_obj.close ();
printf ("\n\n END -- %d bytes read\n", bytesRead);
printf ("\n\n END -- %d TOTAL bytes read\n", totalBytesRead);
cout << endl << total << endl; //it will save source code to (temp.txt) file
InternetCloseHandle (hData);
InternetCloseHandle (hConnection);
InternetCloseHandle (hInternet);
cin.get ();
}
|