vishy_acts has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks , I am trying to connect to url https://www.irctc.co.in . I searched in earlier posts and tried 4-5 ways but still same problem persists . I am giving here 2 methods that I tried along with code and error . Please help . I am using strawberry perl and WinXP

Method 1 :

#!/usr/bin/perl use strict; use WWW::Mechanize; use Crypt::SSLeay; my $mech = WWW::Mechanize->new(autocheck => 1,noproxy => 1); $mech->cookie_jar(HTTP::Cookies->new); $ENV{HTTPS_PROXY} = 'my_proxy:port/'; $mech->agent('Mozilla/5.0'); $mech->proxy(['https', 'http', 'ftp'], 'my_proxy:port'); # http $mech->get("http://www.google.com"); my $test = $mech->forms(); # https $mech->get("https://www.irctc.co.in") ; die $mech->response->status_line unless $mech->success; my $test = $mech->forms();
<error> main::(pnr_2.perl:9): my $mech = WWW::Mechanize->new(autocheck => 1,noproxy => 1); DB<1> r Error GETing https://www.irctc.co.in: Bad Request at pnr_2.perl line 23 at C:/strawberry/perl/lib/WWW/Mechanize.pm line 2747 WWW::Mechanize::_die('Error ', 'GET', 'ing ', 'URI::https=SCALAR(0x19a0a04)', ': ', 'Bad Request') called at C:/strawberry /perl/lib/WWW/Mechanize.pm line 2734 WWW::Mechanize::die('WWW::Mechanize=HASH(0x15ddde4)', 'Error ', 'GET', 'ing ', 'URI::https=SCALAR(0x19a0a04)', ': ', 'Bad Request') called at C:/strawberry/perl/lib/WWW/Mechanize.pm line 2383 WWW::Mechanize::_update_page('WWW::Mechanize=HASH(0x15ddde4)', 'HTTP::Request=HASH(0x19d9e14)', 'HTTP::Response=HASH(0x19a 0f34)') called at C:/strawberry/perl/lib/WWW/Mechanize.pm line 2213 WWW::Mechanize::request('WWW::Mechanize=HASH(0x15ddde4)', 'HTTP::Request=HASH(0x19d9e14)') called at C:/strawberry/perl/ve ndor/lib/LWP/UserAgent.pm line 389 LWP::UserAgent::get('WWW::Mechanize=HASH(0x15ddde4)', 'https://www.irctc.co.in') called at C:/strawberry/perl/lib/WWW/Mech anize.pm line 407 WWW::Mechanize::get('WWW::Mechanize=HASH(0x15ddde4)', 'https://www.irctc.co.in') called at pnr_2.perl line 23 Debugged program terminated. Use q to quit or R to restart, use o inhibit_exit to avoid stopping after program termination, h q, h R or h o to get additional info. DB<1> </error>

Method 2:

#!perl use warnings; use diagnostics; #use LWP::UserAgent; use LWP::Simple; use Crypt::SSLeay; print "Content-type: text/html\n\n"; #<-- for viewing in browser # Create a user agent object my $ua = LWP::UserAgent->new; $ua->proxy(['http', 'ftp'], 'my_proxy:port'); #my $response = $ua->get('http://www.tradeindia.com/Seller/Agricultu +re/Cashews/'); my $response = $ua->get('https://www.irctc.co.in'); ##':content_file' => $out,); if ($response->is_success) { print "\n i am in finally"; } else { die $response->status_line; }
<error> main::(pnr_3.perl:7): print "Content-type: text/html\n\n"; #<-- for viewing in browser DB<1> r Content-type: text/html 500 Connect failed: connect: No connection could be made because the target machine actively refused it.; No connection could be made because the target machine actively refused it. at pnr_3.perl line 21. at pnr_3.perl line 21 Debugged program terminated. Use q to quit or R to restart, use o inhibit_exit to avoid stopping after program termination, h q, h R or h o to get additional info. DB<1> </error>

Thanks

Vishy

Replies are listed 'Best First'.
Re: HTTPS connection problems WWW::Mechanize , LWP
by Corion (Patriarch) on Apr 01, 2011 at 11:38 UTC

    You need to set up %ENV before Crypt::SSLeay is loaded. For example set it up before starting your script, or set it up in a BEGIN block:

    BEGIN { $ENV{HTTPS_PROXY} = 'my_proxy:port/'; }; use Crypt::SSLeay;

      Thanks Corion , but I have already put it there.

      Please see method 1

      $ENV{HTTPS_PROXY} = 'my_proxy:port/';

      -- Vishy

        No you don't. Corion said that you need to specify this within a BEGIN block, as per his example.

        but I have already put it there.

        Except , like Corion stated, its not in a BEGIN block, BEGIN block is key