I need a sample code to retrieve the source code of a page .

Ex : I have www.blahblahblah.tld/file.ext?id=blah

I want to retrieve in a char the source code of that site.

Thanks

Recommended Answers

All 39 Replies

Learn socket programming. How to do that will depend on the operating system you are using. If *nix you need POSIX sockets. MS-Windows use WinSocket.

Using what OS?
On windows, you can use C++/CLI and open a WebClient^ to a page.

It can be also easily done with C# and VB. But since this is c++ forum ...

First thank you all for your reply.
And i am using Windows ...

I know in C# is more easy but i need C++.

Ill try the method with sockets and ... Thines, can you give me an example of WebClient^ ?
Thanks.

EDIT : My project is a Windows Form type.

This can be done more easly with the webBrowser1 from Tools ?

>>EDIT : My project is a Windows Form type.

That is not c++ -- its CLI/C++ which is slightly different language

>>This can be done more easly with the webBrowser1 from Tools ?

Probably.

First thank you all for your reply.
EDIT : My project is a Windows Form type./QUOTE]

What are you going to DO with the contents of the web page when you get it?

In final i found how to take the contents of the page like that :

private: System::Void webBrowser1_Navigated(System::Object^  sender, System::Windows::Forms::WebBrowserNavigatedEventArgs^  e) {
             if( this->webBrowser1->DocumentText->ToString() != "err" )
             {

                 // here i need something like that :
                                // this->webBrowser1->DocumentText->ToString() is
                                 // blahtext|blahtext2|blahtext3
                                 // i want to break that string into 3 another like :

                                 this->webBrowser1->DocumentText->ToString() 
                                     to
                                 blahtext
                                 blahtext2
                                 blahtext3

                                 this->label6->Text = Variablethatcontains blahtext
 this->label7->Text = Variablethatcontains blahtext2
this->label8->Text = Variablethatcontains blahtext3

             }
         }

I'm still confused.
Can you just use this->webBrowser1->DocumentText->ToString()->Split( /*parameters go here*/) ?
to convert that text into an array?

I still need to ask, are you just attempting to get some text from a page or do you actually need to start a browser object?

Anyway i made it but ....
I need to retrieve data from a server and that solution with read web page isn't good ...
I need MYSQL.
How to implement mysql with C++ ?
I don't think is an default include ...

If you have not already downloaded the connector (for .net), you will need to get it:
http://www.mysql.com/downloads/connector/net/

Once you get that installed, you can add a reference to the MySql.Data DLL entry and add "using namespace MySql:: Data::MySqlClient;" to the code that will use it.

commented: Very very helpfull +1

And if i build my application, and i publish it .. i need to put the mysql dll near it ?

Ex APP.exe
mydll.exe

If yes, is possible to embed dll in app ? I want One file for my app.

Later Reply : How to use that Mysql Connector ? Exists any tutorial or don't know im very 'newb' at C++ and MYSQL .............

Have you tried this?

commented: Excellent Suggestion! +12
commented: Thank you, Ancient Dragon. You've changed my life, and my forum posting career, forever. +4

Yes i tried but when i try to compile im getting a nasty error :

Cannot find : boost/system.hpe
Don't know exactly the error ( i closed visual studio ).

LE : LotOfLaugh : When i reopened visual studio and added again the connector the error dissapeared.

As i seen on Google ....
The examples are like that :

/* MySQL Connector/C++ specific headers */
#include <driver.h>
#include <connection.h>
#include <statement.h>
#include <prepared_statement.h>
#include <resultset.h>
#include <metadata.h>
#include <resultset_metadata.h>
#include <exception.h>
#include <warning.h>

But i don't have that... I have.. using namespace MySql:: Data::MySqlClient;

Don't include those headers.


There is documentation on the site.

...but here is a code snippet assuming you have a database table named "SOME_TABLE" that has a column called "NAME":

#include "stdafx.h"

using namespace System;
using namespace System::Collections::Generic;
using namespace System::IO;//assuming you still need this for the other part
using namespace System::Net; //assuming you still need this for the other part
using namespace MySql::Data::MySqlClient;

bool GetListFromDb(List<String^>^ lst_strNames, String^% strError)
{
   bool blnRetVal = true;

   try
   {
      MySqlConnectionStringBuilder^ csb = gcnew MySqlConnectionStringBuilder();
      csb->Server = "{server address goes here}";
      csb->Database = "{database name goes here}";
      csb->UserID = "{user id goes here}";
      csb->Password = "{password goes here}";

      MySqlConnection^ conn = gcnew MySqlConnection(csb->ToString());
      String^ strSQL = "SELECT NAME from SOME_TABLE";
      MySqlDataReader^ rdr = (gcnew MySqlCommand(strSQL, conn))->ExecuteReader();
      while(rdr->Read())
      {
         lst_strNames->Add(rdr["NAME"]->ToString()->Trim());
      }
      rdr->Close();
      conn->Close();
   }
   catch(Exception^ exc)
   {
      blnRetVal = false;
      strError = exc->Message;
   }

   return blnRetVal;
}

...and it can be called like this:

String^ strError = "";
    List<String^>^ lst_strNames = gcnew List<String^>();

    if(!GetListFromDb(lst_strNames, strError))
    {
       System::Diagnostics::Debug::WriteLine("Could not load list: " + strError);
       return -1;
    }

Tried

String^ strError = "";
    List<String^>^ lst_strNames = gcnew List<String^>();

			 MySqlConnectionStringBuilder^ csb = gcnew MySqlConnectionStringBuilder();
      csb->Server = "localhost";
      csb->Database = "dumi";
      csb->UserID = "dumi";
      csb->Password = "dumi";
 
      MySqlConnection^ conn = gcnew MySqlConnection(csb->ToString());
      String^ strSQL = "SELECT * from servers";
      MySqlDataReader^ rdr = (gcnew MySqlCommand(strSQL, conn))->ExecuteReader();
      while(rdr->Read())
      {
         lst_strNames->Add(rdr["id"]->ToString()->Trim());
      }
      rdr->Close();
      conn->Close();

Getting

1>c:\users\adrian\desktop\dhlds2\dhlds2\Form1.h(668): error C2065: 'List' : undeclared identifier
1>c:\users\adrian\desktop\dhlds2\dhlds2\Form1.h(668): error C2275: 'System::String' : illegal use of this type as an expression
1>          c:\program files\reference assemblies\microsoft\framework\.netframework\v4.0\mscorlib.dll : see declaration of 'System::String'
1>c:\users\adrian\desktop\dhlds2\dhlds2\Form1.h(668): error C2059: syntax error : '>'
1>c:\users\adrian\desktop\dhlds2\dhlds2\Form1.h(681): error C2065: 'lst_strNames' : undeclared identifier
1>c:\users\adrian\desktop\dhlds2\dhlds2\Form1.h(681): error C2227: left of '->Add' must point to class/struct/union/generic type

Check my edit (as I added the namespaces):

Now is working thank you soo much !

Two last things :

String^ strError = "";
List<String^>^ lst_strNames = gcnew List<String^>();

if(!GetListFromDb(lst_strNames, strError))
{
this->textBox4->Text = lst_strNames; // how to do that ?
}

And the 2nd ... that connector needs an .dll or something present near the application ? If yes ... how to 'hide it' inside the application ?

What you're asking is how to convert a list of Strings to a single String.

I would do it like this:

using namespace System::Linq;

...

this->textBox4->Text = 
       String::Join("\n",
         Enumerable::ToArray<String^>(lst_strNames));

Something not working ...

I have the Mysql SERVER ONLINE ....
The code is :

bool GetListFromDb(List<String^>^ lst_strNames, String^% strError)
{
   bool blnRetVal = true;

   try
   {
      MySqlConnectionStringBuilder^ csb = gcnew MySqlConnectionStringBuilder();
      csb->Server = "localhost";
      csb->Database = "dumi";
      csb->UserID = "dumi";
      csb->Password = "dumi";

      MySqlConnection^ conn = gcnew MySqlConnection(csb->ToString());
      String^ strSQL = "SELECT * FROM `bla` WHERE (`ident` = '1' );";
      MySqlDataReader^ rdr = (gcnew MySqlCommand(strSQL, conn))->ExecuteReader();
      while(rdr->Read())
      {
         lst_strNames->Add(rdr["blah2"]->ToString()->Trim());
      }
      rdr->Close();
      conn->Close();
   }
   catch(Exception^ exc)
   {
      blnRetVal = false;
   }

   return blnRetVal;
}

2nd

String^ strError = "test";
    List<String^>^ lst_strNames = gcnew List<String^>();
 
    if(GetListFromDb(lst_strNames, strError))
    {
      this->textBox1->Text = String::Join("\n", Enumerable::ToArray<String^>(lst_strNames));
      // return -1;
    }
	else
	{
		this->textBox1->Text = strError;
	}

The mysql table is ok because with php it works ...

and when i run the code ...
the textbox is changeing into 'test'
What's wrong ?

You removed the exclamation point from the if statement:

if(!GetListFromDb(lst_strNames, strError))

Now, the textbox has no content ...
That can be from the mysql query syntax ? ( that 'blah2' is VARCHAR with 500 length )

You can remove those accent marks from you db elements.

When you step through this in the debugger, is the line of code executed that moves the data into the List?

while(rdr->Read())
      {
          this->textBox3->Text = "debuged"; //notexecuted
         lst_strNames->Add(rdr["host"]->ToString()-> Trim());
      }

...then the SELECT is not working.
There is no data matching the criteria.
- Could be the column you're pulling
- Could be the extra decoration inside your SQL


MY BAD

...forgot to call Open on the database connection'conn->Open(); //before you call Read()

MySqlConnectionStringBuilder^ csb = gcnew MySqlConnectionStringBuilder();
      csb->Server = "localhost";
      csb->Database = "dumi";
      csb->UserID = "dumi";
      csb->Password = "dumi";
		
      MySqlConnection^ conn = gcnew MySqlConnection(csb->ToString());
        	  conn->Open();
	  String^ strSQL = "SELECT * FROM `blah` WHERE (`ident` = '1' );";
      MySqlDataReader^ rdr = (gcnew MySqlCommand(strSQL, conn))->ExecuteReader();
	  
	  while(rdr->Read())
      {
		  this->textBox3->Text = "debuged";
         lst_strNames->Add(rdr["blah2"]->ToString()-> Trim());
      }
      rdr->Close();
      conn->Close();

Going into

String^ strError = "";
    List<String^>^ lst_strNames = gcnew List<String^>();
 
    if(!GetListFromDb(lst_strNames, strError))
    {
      this->textBox1->Text = String::Join("\n", Enumerable::ToArray<String^>(lst_strNames));
      // return -1;
    }
	else
	{
		[B]this->textBox1->Text = "ERR";[/B]
	}

The function reports its errors in strError. You can use that to see what actually went wrong.
...and that else you added is purely cosmetic.

if i debbug strError ... is Blank ... has no content.

Then there is no error.

...which should mean something is NOW in the List, right?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.