Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   C++ Builder (https://www.clubdelphi.com/foros/forumdisplay.php?f=13)
-   -   Ver código fuente de una página web (https://www.clubdelphi.com/foros/showthread.php?t=64367)

chinchan 29-03-2009 04:26:56

Ver código fuente de una página web
 
Hola. ¿Cómo podría conectarme con una página web y ver su código fuente? (en un RichEdit por ejemlo). Muchas Gracias

dec 29-03-2009 10:36:21

Hola,

Código:

RichEdit->Text = IdHttp->Get("http://www.clubdelphi.com/");
Donde "IdHttp" es un componente del paquete de componentes Indy, incluidos en Delphi, y, también en C++ Builder, creo.

chinchan 29-03-2009 14:30:44

Perfecto, no hay como preguntar al que sabe, muchas gracias.

escafandra 30-03-2009 13:40:18

Yo no soy muy amigo de componentes, aunque la misma VCL es un conjunto de ellos...

Bueno, me gustaría exponer una solución usando sólo API sin componentes de terceros ni de la VCL en la función de lectura de la Web:

Código:

#include <vector>

bool InternetReadWeb(char* URL, std::vector<BYTE> *Vector)
{
  HINTERNET hNet;
  HINTERNET hUrl;
  BYTE  Buffer;
  DWORD BytesRead = 0;
  bool Result = false;

  if(InternetAttemptConnect(0) != ERROR_SUCCESS) return Result;

  hNet = InternetOpen("App", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
  if (hNet){
    hUrl = InternetOpenUrl(hNet, URL, NULL, 0, INTERNET_FLAG_RELOAD, 0);
    if (hUrl){
      // Lee la pagina web...
      for(int ind=0; ; ind+=BytesRead){
        Result = InternetReadFile(hUrl, &Buffer, sizeof(BYTE), &BytesRead);
        if(Result && BytesRead==0) break;
        Vector->push_back(Buffer);
      }
      InternetCloseHandle(hUrl);
    }
    InternetCloseHandle(hNet);
  }

  return Result;
}

Y el uso de esta función para el caso particular de esta pregunta:
Código:

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

Tenemos algo mas de código, pero nos hacemos independientes de las Indy.;)

Saludos.

escafandra 30-03-2009 14:18:04

... Y si no te importa usar algo de la VCL en tu función InternetReadWeb, entonces la puedes diseñar así:

Código:

String InternetReadWeb(char* URL)
{
  HINTERNET hNet;
  HINTERNET hUrl;
  char  Buffer;
  DWORD BytesRead = 0;
  String Text = "";
  bool NoError;

  if(InternetAttemptConnect(0) != ERROR_SUCCESS) return Text;

  hNet = InternetOpen("agent", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
  if (hNet){
    hUrl = InternetOpenUrl(hNet, URL, NULL, 0, INTERNET_FLAG_RELOAD, 0);
    if (hUrl){
      // Lee la pagina web...
      for(int ind=0; ; ind+=BytesRead){
        NoError = InternetReadFile(hUrl, &Buffer, sizeof(BYTE), &BytesRead);
        if(NoError && BytesRead==0) break;
        Text = Text + Buffer;
      }
      InternetCloseHandle(hUrl);
    }
    InternetCloseHandle(hNet);
  }

  return Text;
}

Para usarla de esta manera:
Código:

  RichEdit1->Text = InternetReadWeb("http://www.clubdelphi.com/");
Con menos código y quizás mas cómoda...;)

Se me olvidaba, en cualquiera de las opciones del uso de estas API, debes incluir en tu proyecto la librería inet.lib.

Saludos.


La franja horaria es GMT +2. Ahora son las 19:42:12.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi