ExtendedNC/ExtNC.cpp
2021-09-01 17:38:08 +02:00

215 lines
6.4 KiB
C++

//---------------------------------------------------------------------------
#include <time.h>
#include <vcl.h>
#pragma hdrstop
#include "ExtNC.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "SHDocVw_OCX"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
ComboBox3->ItemIndex=0;
Panel1->Height = 44;
ToSend->ColWidths[0]=150;
ToSend->ColWidths[1]=450;
ToSend->Cells[0][0] = "Host";
ToSend->Cells[1][0] = "";
ToSend->Cells[0][1] = "Referer";
ToSend->Cells[1][1] = "";
ToSend->Cells[0][2] = "Cookie";
ToSend->Cells[1][2] = "";
ToSend->Cells[0][3] = "Accept";
ToSend->Cells[1][3] = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*";
ToSend->Cells[0][4] = "Accept-Language";
ToSend->Cells[1][4] = "es,es;q=0.5";
ToSend->Cells[0][5] = "Accept-Charset";
ToSend->Cells[1][5] = "iso-8859-1,*,utf-8";
ToSend->Cells[0][6] = "Content-Type";
ToSend->Cells[1][6] = "application/x-www-form-urlencoded";
ToSend->Cells[0][7] = "Accept-Encoding";
ToSend->Cells[1][7] = "gzip, deflate";
ToSend->Cells[0][8] = "User-Agent";
ToSend->Cells[1][8] = "Mozilla/4.0 (compatible; MSIE 6.0; Windows 98; .NET CLR 1.0.3705)";
ToSend->Cells[0][9] = "Proxy-Connection";
ToSend->Cells[1][9] = "Keep-Alive";
ToSend->Cells[0][10] = "Pragma";
ToSend->Cells[1][10] = "no-cache";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::GenerarCabeceraClick(TObject *Sender)
{
AnsiString Method[] = { "GET","HEAD","OPTIONS","TRACE","PUT","POST","DELETE" };
// Obtenemos el Host de la URL
AnsiString Host;
Host = Url->Text;
if ( Host.Pos("http://") > 0 )
Host = Host.SubString( 8, Host.Length()-7 );
if ( Host.Pos( "/" ) > 0 )
Host = Host.SubString( 1, Host.Pos( "/" ) );
// Componemos la cabecera que va a ser enviada...
Memo3->Clear();
Memo3->Lines->Add(Method[ComboBox3->ItemIndex]+" "+Url->Text+" "+ProtocolVersion->Text);
// Si esta vacia, ponemos el HOST
if ( ToSend->Cells[1][0].IsEmpty() )
ToSend->Cells[1][0] = Host;
// Componemos la cabecera que debemos enviar.
for ( int i=0; i<ToSend->RowCount; i++ )
if ( !ToSend->Cells[1][i].IsEmpty() )
Memo3->Lines->Add( ToSend->Cells[0][i] + ": " + ToSend->Cells[1][i] );
// Metodo POST
if ( ComboBox3->ItemIndex==5 )
{
Memo3->Lines->Add( "Content-Length: " +AnsiString( Post->Text.Length() ) );
Memo3->Lines->Add( "" );
Memo3->Lines->Add( Post->Text );
}
Memo3->Lines->Add( "" );
Memo3->Lines->Add( "" );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ComboBox3Change(TObject *Sender)
{
if ( ComboBox3->ItemIndex==5 )
Panel1->Height = 80;
else
Panel1->Height = 44;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
GenerarCabeceraClick(0);
EnviarCabecera();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::BitBtn3Click(TObject *Sender)
{
EnviarCabecera();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::EnviarCabecera(void)
{
AnsiString buf;
ClientSocket = new TClientSocket( this );
ClientSocket->OnConnect = SocketConnect;
// ClientSocket->OnDisconnect = SocketDisconnect;
// ClientSocket->OnError = SocketError;
ClientSocket->ClientType = ctBlocking;
ClientSocket->Host = ToSend->Cells[1][0];
ClientSocket->Port = 80;
ClientSocket->Active = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SocketConnect(TObject *Sender,
TCustomWinSocket *Socket)
{
time_t first, second;
TDateTime start;
TWinSocketStream *pStream;
bool eond;
int num_bytes_read;
char buff[500];
// create a TWinSocketStream for reading and writing
pStream = new TWinSocketStream( Socket, 20000 );
try
{
// fetch and process commands until the connection or thread is terminated
if ( ClientSocket->Active )
{
try
{
// Enviamos todos los datos de cabecera
for ( int i=0; i<Memo3->Lines->Count; i++ )
{
strncpy( buff, Memo3->Lines->Strings[i].c_str(), 499 );
pStream->Write( buff, strlen(buff) );
pStream->Write( "\n", 1 );
}
Memo2->Clear();
if ( pStream->WaitForData(10000) )
{
eond = false;
first = time(NULL); /* Gets system
time */
while( !eond && ClientSocket->Active && difftime(time(NULL),first) < 20 )
{
try {
buff[0]='\0';
num_bytes_read = 0;
num_bytes_read = pStream->Read( buff, 499 );
eond = (num_bytes_read==0);
} catch(...) {
eond = true;
}
buff[num_bytes_read]='\0';
Memo2->Lines->Text = Memo2->Lines->Text+AnsiString(buff);
}
}
}
catch (Exception &E)
{
if (!E.ClassNameIs("EAbort"))
{
// nothing
}
}
}
}
__finally
{
delete pStream;
Socket->Close();
}
int numCRLF=1;
Memo1->Clear();
for (int i=0; numCRLF > 0 && i<Memo2->Lines->Count; i++ )
{
if ( Memo2->Lines->Strings[0].IsEmpty() ) numCRLF--;
Memo1->Lines->Add(Memo2->Lines->Strings[0]);
Memo2->Lines->Delete(0);
}
Memo2->Lines->SaveToFile(ExtractFilePath(Application->ExeName)+"output.htm");
wchar_t *wbuff;
int ancho_buff = AnsiString(ExtractFilePath(Application->ExeName)+"output.htm").WideCharBufSize();
wbuff = new wchar_t[ancho_buff];
AnsiString(ExtractFilePath(Application->ExeName)+"output.htm").WideChar(wbuff,ancho_buff);
CppWebBrowser1->Navigate(wbuff, NULL, NULL, NULL, NULL );
delete buff;
}