getting website source code with c++

hi
assume we have link like this
http://www.cplusplus.com/

i want to get source code of this link using c++ without opening any browser
or how to search string in this page(without opening any browser).

how to do that.
any help.
thank you.
Last edited on
You can use libcurl to accomplish this task quite easily.
now i have this code and obtain any www.example.com source code
but i want to get the source code of page like that :
http://www.cplusplus.com/forum/windows/17264/

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
#include <windows.h>
#include <wininet.h>
#include <iostream>
#include <conio.h>
#include <fstream>
#include<string>
#pragma comment (lib, "wininet.lib")

using namespace std;

int main(int argc, char *argv[])
{
	fstream fs_obj;
  fs_obj.open("temp.txt",ios::out | ios::app);  
  HINTERNET hInternet = InternetOpenA("InetURL/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 );

  HINTERNET hConnection = InternetConnectA( hInternet, "http://www.cplusplus.com/forum/windows/17264/", 80, " "," ", INTERNET_SERVICE_HTTP, 0, 0 ); //enter url here

  HINTERNET hData = HttpOpenRequestA( hConnection, "GET", "/", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0 );

  char buf[ 2048 ] ;

  HttpSendRequestA( hData, NULL, 0, NULL, 0 ) ;
  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();
}
Last edited on
You need to check the return values.
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, "http://www.cplusplus.com/forum/windows/17264/", 80, " ", " ", INTERNET_SERVICE_HTTP, 0, 0); //enter url here
  if (hConnection == NULL)
    Error (_T ("Error opening internet connection"));
  HINTERNET hData = HttpOpenRequestA (hConnection, "GET", "/", 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 ();
}
thank you.
i got it by replacing line 34
to
HINTERNET hData = HttpOpenRequestA (hConnection, "GET", "/forum/windows/", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);

but i can not get http://www.cplusplus.com/user/ source code with the new informaion after login

http://www.cplusplus.com/user/ source code before login==http://www.cplusplus.com/user/ source code afer login

any help
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 ();
}
Last edited on
What exactly does not work ?

When I run it it works, though it takes sometimes quite a bit.
ok
i log in the website.
and request http://www.cplusplus.com/user/pm.cgi source code
but it shows to me in the source code
 
<title>User sign in: This feature is only available for registered users</title>


c++ program or the server does not understand that i am logged in the website

how to get correct source code.
You can try so send the username and password with InternetConnect.
does not work
any another idea.
c++ program or the server does not understand that i am logged in the website


Since it works with a browser I guess that they use cookies to remember the login status. I will try in the afternoon with the WebBrowser control in C++/CLI to see if it works there.

EDIT

I did a little app in C++/CLI and the WebBrowser control and it works fine. I can send you the project (VS 2013) if you are interested.

Last edited on
I did a little app in C++/CLI and the WebBrowser control and it works fine. I can send you the project (VS 2013) if you are interested.

nice.
send the project to me.
You gat a PM.
Danke
please look at your pm.
Topic archived. No new replies allowed.