Ir al contenido


Foto

[RESUELTO] Como leer un texto en internet?


  • Por favor identifícate para responder
2 respuestas en este tema

#1 c0lo

c0lo

    Advanced Member

  • Miembro Platino
  • PipPipPip
  • 241 mensajes
  • LocationLima-Peru

Escrito 07 septiembre 2009 - 06:59

Al decir, como leer un texto en internet me refiero al conetenido de una pagina web. Mas espefico, digamos que tengo www.algo.com/algo.txt, quiero leer el contenido de algo.txt y guardarlo en un arrelo para luego comparar.

Gracias
  • 0

#2 escafandra

escafandra

    Advanced Member

  • Administrador
  • 4.107 mensajes
  • LocationMadrid - España

Escrito 08 septiembre 2009 - 12:12

Pues te dejo unas funciones que diseñé para leer una página web. Usan sólo la API.



cpp
  1. #include <vector>
  2.  
  3. bool InternetReadWeb(char* URL, std::vector<BYTE> *Vector)
  4. {
  5.   HINTERNET hNet;
  6.   HINTERNET hUrl;
  7.   BYTE  Buffer;
  8.   DWORD BytesRead = 0;
  9.   bool Result = false;
  10.  
  11.   if(InternetAttemptConnect(0) != ERROR_SUCCESS) return Result;
  12.  
  13.   hNet = InternetOpen("App", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
  14.   if (hNet){
  15.     hUrl = InternetOpenUrl(hNet, URL, NULL, 0, INTERNET_FLAG_RELOAD, 0);
  16.     if (hUrl){
  17.       // Lee la pagina web...
  18.       for(int ind=0; ; ind+=BytesRead){
  19.         Result = InternetReadFile(hUrl, &Buffer, sizeof(BYTE), &BytesRead);
  20.         if(Result && BytesRead==0) break;
  21.         Vector->push_back(Buffer);
  22.       }
  23.       InternetCloseHandle(hUrl);
  24.     }
  25.     InternetCloseHandle(hNet);
  26.   }
  27.  
  28.   return Result;
  29. }



Esta función puede usarse de la siguiente manera:


cpp
  1. void __fastcall TForm1::Button1Click(TObject *Sender)
  2. {
  3.     std::vector<BYTE> Memory;
  4.     InternetReadWeb("http://www.delphiaccess.com/", &Memory);
  5.     RichEdit1->Text = (char*)&Memory[0];
  6. }



Si prefieres simplificarlo usando algo de la VCL en la función, puedes hacerlo así­:



cpp
  1. String InternetReadWeb(char* URL)
  2. {
  3.   HINTERNET hNet;
  4.   HINTERNET hUrl;
  5.   char  Buffer;
  6.   DWORD BytesRead = 0;
  7.   String Text = "";
  8.   bool NoError;
  9.  
  10.   if(InternetAttemptConnect(0) != ERROR_SUCCESS) return Text;
  11.  
  12.   hNet = InternetOpen("agent", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
  13.   if (hNet){
  14.     hUrl = InternetOpenUrl(hNet, URL, NULL, 0, INTERNET_FLAG_RELOAD, 0);
  15.     if (hUrl){
  16.       // Lee la pagina web...
  17.       for(int ind=0; ; ind+=BytesRead){
  18.         NoError = InternetReadFile(hUrl, &Buffer, sizeof(BYTE), &BytesRead);
  19.         if(NoError && BytesRead==0) break;
  20.         Text = Text + Buffer;
  21.       }
  22.       InternetCloseHandle(hUrl);
  23.     }
  24.     InternetCloseHandle(hNet);
  25.   }
  26.  
  27.   return Text;
  28. }



Y como ejemplo de uso:


cpp
  1.   RichEdit1->Text = InternetReadWeb("http://www.delphiaccess.com/");



Seguro que le sacas partido.  ;)

Saludos.
  • 0

#3 agag4

agag4

    Advanced Member

  • Miembros
  • PipPipPip
  • 298 mensajes
  • LocationMéxico

Escrito 28 septiembre 2009 - 07:01

Excelente respuesta....
  • 0




IP.Board spam blocked by CleanTalk.