KOD cpp: UKRYJ
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "wininet.h"
#include <memory>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
#pragma link "wininet.lib"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
AnsiString ReadFileURL(AnsiString Url)
{
AnsiString result = "";
HINTERNET hSession = NULL;
hSession = InternetOpen("ReadFileURL", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if(hSession)
{
HINTERNET hService = NULL;
hService = InternetOpenUrl(hSession, Url.c_str(), NULL, 0, 0, 0 );
if(hService)
{
while(1)
{
char lpBuffer[6024 + 1];
DWORD dwBytesRead;
InternetReadFile(hService, lpBuffer, 6024, &dwBytesRead);
if(dwBytesRead == 0) break;
lpBuffer[dwBytesRead] = 0;
result += lpBuffer;
}
}
InternetCloseHandle(hService);
}
InternetCloseHandle(hSession);
return result;
}
//---------------------------------------------------------------------------
std::auto_ptr<TStringList> GetMailTo(AnsiString sMail)
{
Screen->Cursor = crHourGlass;
String Value = ReadFileURL(sMail);
std::auto_ptr< TStringList> Lista(new TStringList);
int p = Value.Pos("\"mailto:");
do
{
Value = Value.Delete(1, p + 7);
int x = Value.Pos("\">") - 1;
Lista->Add( Value.SubString(1, x) );
p = Value.Pos("\"mailto:");
}while(p > 0);
Screen->Cursor = crDefault;
return Lista;
}
//---------------------------------------------------------------------------
std::auto_ptr<TStringList> GetExtUrl(AnsiString sMail)
{
Screen->Cursor = crHourGlass;
String Value = ReadFileURL(sMail);
std::auto_ptr< TStringList> Lista(new TStringList);
int p = Value.Pos("href=\"");
do
{
Value = Value.Delete(1, p + 5);
int x = Value.Pos("\"") - 1;
if( Value.SubString(1, 7) == "http://" ) Lista->Add( Value.SubString(1, x) );
p = Value.Pos("href=\"");
}while(p > 0);
Screen->Cursor = crDefault;
return Lista;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button6Click(TObject *Sender)
{
Memo1->Lines->Assign( GetExtUrl("http://www.komputronik.pl/kontakty/").get() );
}
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "wininet.h"
#include <memory>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
#pragma link "wininet.lib"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
AnsiString ReadFileURL(AnsiString Url)
{
AnsiString result = "";
HINTERNET hSession = NULL;
hSession = InternetOpen("ReadFileURL", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if(hSession)
{
HINTERNET hService = NULL;
hService = InternetOpenUrl(hSession, Url.c_str(), NULL, 0, 0, 0 );
if(hService)
{
while(1)
{
char lpBuffer[6024 + 1];
DWORD dwBytesRead;
InternetReadFile(hService, lpBuffer, 6024, &dwBytesRead);
if(dwBytesRead == 0) break;
lpBuffer[dwBytesRead] = 0;
result += lpBuffer;
}
}
InternetCloseHandle(hService);
}
InternetCloseHandle(hSession);
return result;
}
//---------------------------------------------------------------------------
std::auto_ptr<TStringList> GetMailTo(AnsiString sMail)
{
Screen->Cursor = crHourGlass;
String Value = ReadFileURL(sMail);
std::auto_ptr< TStringList> Lista(new TStringList);
int p = Value.Pos("\"mailto:");
do
{
Value = Value.Delete(1, p + 7);
int x = Value.Pos("\">") - 1;
Lista->Add( Value.SubString(1, x) );
p = Value.Pos("\"mailto:");
}while(p > 0);
Screen->Cursor = crDefault;
return Lista;
}
//---------------------------------------------------------------------------
std::auto_ptr<TStringList> GetExtUrl(AnsiString sMail)
{
Screen->Cursor = crHourGlass;
String Value = ReadFileURL(sMail);
std::auto_ptr< TStringList> Lista(new TStringList);
int p = Value.Pos("href=\"");
do
{
Value = Value.Delete(1, p + 5);
int x = Value.Pos("\"") - 1;
if( Value.SubString(1, 7) == "http://" ) Lista->Add( Value.SubString(1, x) );
p = Value.Pos("href=\"");
}while(p > 0);
Screen->Cursor = crDefault;
return Lista;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button6Click(TObject *Sender)
{
Memo1->Lines->Assign( GetExtUrl("http://www.komputronik.pl/kontakty/").get() );
}
Zwróć uwagę, że zamiast <a href=" przeszukuję href=" ponieważ pomiędzy a i href mogą wystąpić inne tagi html.