Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

WWW::Mechanize - Could not connect to a server

by jesuashok (Curate)
on Mar 20, 2007 at 03:00 UTC ( #605624=perlquestion: print w/ replies, xml ) Need Help??
jesuashok has asked for the wisdom of the Perl Monks concerning the following question:


This is the first time I am trying some sample programs using WWW::Mechanize. I got this sample code from here

#!/usr/bin/perl -w use WWW::Mechanize; use Data::Dumper; use Storable; $url = ' +7.html'; $m = WWW::Mechanize->new(); $m->get($url); $c = $m->content; print Dumper [ $c ];
I got the below Error :-
$VAR1 = [ '500 Can\'t connect to (connect: Unknown e +rror) ' ];
But I could connect to the URL via web browser. Could anyone help me to resolve this issue ?

hmmm ....let me think what did I said

Comment on WWW::Mechanize - Could not connect to a server
Select or Download Code
Re: WWW::Mechanize - Could not connect to a server
by chargrill (Parson) on Mar 20, 2007 at 03:20 UTC

    Are you behind a proxy?

    If you are, WWW::Mechanize respects your $ENV{HTTP_PROXY} environment variable - you could try setting that in your program to whatever your browser is set to.

    Update: Just checked the Mech FAQ, How to use mech through a proxy might be of interest, too

    s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)
      I Don't know how to check whether my system uses Proxy, through Perl. because when I tried to
      print $ENV{HTTP_PROXY} , "\n";
      Prints nothing.
      But I could see a IP address set in Internet Explorer. Can I use that Ip as follows in my perl code ?
      $ENV{HTTP_PROXY} = "xx.xx.xx.xx:<port_number>";
      I am Sorry, if this questions annoy you.

      hmmm ....let me think what did I said

        It's been a while since I've tried to mech from inside a proxy. (Yes, I've just verbized the short name for WWW::Mechanize).

        Give that a shot, or try following the other suggestions frm the FAQ, for which you've already got the link above.

        If that fails (and I don't see how, since now you've confirmed you're behind a proxy) there are some other troubleshooting steps you can try near the end of the Mech FAQ, including checking $mech->status after every attempt to get something.

        s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)
Re: WWW::Mechanize - Could not connect to a server
by Preceptor (Chaplain) on Mar 20, 2007 at 11:44 UTC
    You've mentioned you're using a proxy.

    You need to set up WWW:Mechanize to use your proxy. Ideally, you'll use the in-built OS options, but as a shortcut (to check if this actually is your problem) then just use:

    my $mech = WWW::Mechanize -> new(); $mech -> http_proxy ( "Your_proxy_address:proxy_port" )
    I've also found enabling LWP debugging to be most useful when troubleshooting Mechanized scripts.
    use LWP::Debug qw(+);

      #!/usr/bin/perl -w use WWW::Mechanize; use strict; use Data::Dumper; use Storable; my $url = ' +0027.html'; my $m = WWW::Mechanize->new(); $m->http_proxy ( "proxy_address:proxy_port" );
      I tried the above code and got the error as follows :-
      Can't locate object method "http_proxy" via package "WWW::Mechanize" a +t lin e 11.
      Is there something wrong in the above Code. If so, could you add your valuable points.

      hmmm ....let me think what did I said
        There is no method http_proxy. If you RTFM you can discover the correct method.
        If you had read the Documentation (which was pointed out to you by chargrill in the first reply to this thread) you would see that you have $m->http_proxy rather than $m->proxy. Again, if you are not sure about something why not read the documentation, it does not take that long does it?

        As a side note you can also use can to find out if a Method exists within an invocant:
        #!/usr/bin/perl use strict; use warnings; use WWW::Mechanize; my $mech = WWW::Mechanize->new(); if ($mech->can("http_proxy")){ print "Mech can http_proxy\n"; }else{ print "Mech can't http_proxy\n"; } if ($mech->can("proxy")){ print "Mech can proxy\n"; }else{ print "Mech can't proxy\n"; }

        Hope this helps


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://605624]
Approved by chargrill
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2015-07-04 01:41 GMT
Find Nodes?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...

    Results (57 votes), past polls