Make postback request python or perl

434 Views Asked by At

I'm trying to do a postback request to a site built in ASP.NET, my first attemp was to do it in Perl, using Mechanize, then I tried Python and no results... I was reading that might be a damaged SSL or the postback itself.

I'm doing these because there's a file that I want to download daily and downloading manually it will cost a lot of time, but I haven't been able to automatize it.

I'm open to any language or suggestions. The following is my perl code (Note: i I had to desactive SSL Certification because when it was active I got no response from the server):

use warnings;
use strict;
use WWW::Mechanize;
use Data::Dumper;
use Time::Piece;
use Time::Seconds;
use Try::Tiny;
use Log::Logger;
use File::Basename;
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;

sub getLoggingTime {

    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
    my $nice_timestamp = sprintf ( "%04d%02d%02d %02d:%02d:%02d",
                                   $year+1900,$mon+1,$mday,$hour,$min,$sec);
    return $nice_timestamp;
}
my $dirname = dirname(__FILE__);
my $log = new Log::Logger;
$log->open_append($dirname . "/pmlcenace.log");
$log->log(getLoggingTime() . " INFO: Inicio del Script pmlcenace.pl");

# =============================
# Seteo de Variables generales
# =============================

my $maxtry = 6;
my $dest = qq(C:/Users/MyUser/Desktop/Precios/);
my $url = 'https://www.cenace.gob.mx/SIM/VISTA/REPORTES/H_CapacidadTransfer.aspx?N=263&site=&tipoArch=C&tipoUni=SIN&tipo=Diarios';
my $params = [   
        __ASYNCPOST => 'true',
        __EVENTARGUMENT => '{"commandName":"Check","index":"0:0"}',
        __EVENTTARGET => 'ctl00$ContentPlaceHolder1$treePrincipal',
        ctl00_ContentPlaceHolder1_treePrincipal_ClientState => '{"expandedNodes":[],"collapsedNodes":[],"logEntries":[],"selectedNodes":[],"checkedNodes":["0","0:0"],"scrollPosition":0}',
        __VIEWSTATE => '/w edited for space and viewing reasons',
        __VIEWSTATEGENERATOR => '955A55B8',
        __EVENTVALIDATION => '/wEdAAOCpQshmYscjMDA9x+69HkswfFVx5pEsE3np13JV2opXVEvSNmVO1vU+umjph0DtwdLoqQBBqXirK2Np+DpA6TO2lTaZh4NXJjUyfeW6oTM9g==',
        'ctl00_ContentPlaceHolder1_ListViewNodos_ClientState' => '',
        'ctl00$ContentPlaceHolder1$NotifAvisos$hiddenState' => '',
        'ctl00_ContentPlaceHolder1_NotifAvisos_XmlPanel_ClientState' => '',
        'ctl00_ContentPlaceHolder1_NotifAvisos_TitleMenu_ClientState' => '',
        'ctl00_ContentPlaceHolder1_NotifAvisos_ClientState' => '',
        'ctl00$ContentPlaceHolder1$btnCerrarPanel' => '',
        'ctl00$ContentPlaceHolder1$toolkit' => '{"expandedNodes":[],"collapsedNodes":[],"logEntries":[],"selectedNodes":[],"checkedNodes":["0","0:0"],"scrollPosition":0}'
        ,''=>''];

# ===========================================
# Configuración e inizialización de Mechanize
# ===========================================

my $mech = WWW::Mechanize->new();
$mech->show_progress(1);
$mech->max_redirect(0);
$mech->agent_alias('Windows Mozilla');
#$mech->add_header(
#    User_Agent => 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
#    );

# ==========================================
# POST Request a la página del CENACE
# Nota: Intenta hasta 5 veces, si no exit: 1
# ==========================================


while ($maxtry > 0) {
    $log->log(getLoggingTime() . " INFO: POST Request: " . $url);
    $mech->post($url,$params);  

    if ($mech->status == '200') {
        last;
    } else {
        $maxtry--;
        $log->log(getLoggingTime() . " WARN: POST Request fallido. [" . $maxtry . "] Intentos restantes");
        if ($maxtry == 1) {
            $log->fail(getLoggingTime() . " ERROR: Numero máximo de intentos alcanzados");
            exit 1;
        }
        sleep(1.5);
    }
}




print "\n\n\n\n\n Validating: AFTER DOING THE POST:\n";
print $mech->content(  base_href => [my $base_href|undef] );
if ($mech->is_html() == 1) 
    {
        print "\n";
        print "It DOES have HTML";
        print "\n";
    }
    else 
    {
        print "\n";
        print "Do NOT have HTML";
        print "\n";
    }

The response I got is RedirectError, like this:

1|#||4|87|pageRedirect||%2fContacto.aspx%3faspxerrorpath%3d%2fSIM%2fVISTA%2fREPORTES%2fH_CapacidadTransfer.aspx|

But that request have to return a HTML code that has the links I need for the files.

Maybe I'm missing something I don't know. I really would appreciate any kind of help or solution.

Thanks

0

There are 0 best solutions below