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

Can't get Mechanize get to work?? Should be easy, right? I am using Windows XP, ActivePerl 5.10.1, and Mechanize 1.6 (that came with my install accoring to PPM). I cant get a single URL to work. All I get is "Bad hostname". I copy the URLs from my browser to my code and still no luck (browser works fine). Here is the code:


-----------------
#!/usr/bin/perl # Include the WWW::Mechanize module use WWW::Mechanize; # What URL shall we retrieve? $url = "http://www.google.com"; # Create a new instance of WWW::Mechanize # enabling autoheck checks each request to ensure it was successful, # producing an error if not. my $mechanize = WWW::Mechanize->new(autocheck => 1); # Retrieve the page $mechanize->get($url); # Assign the page content to $page my $page = $mechanize->content; # Output the page print $page;

----------------- Here is the output I get at the commmand line: -----------------

C:\Data\Perl> mechperl.pl Error GETing http://www.google.com: Can't connect to www.google.com:80 (Bad hostname 'www.google.com') at C:\Data\Perl\mechperl.pl line 15

-----------------

Any help is appreciated. thanks

Replies are listed 'Best First'.
Re: Can't get WWW::Mechanize get to work??
by cavac (Prior) on Apr 26, 2012 at 12:48 UTC

    Does your network require a proxy setting?

    If your default browser is internet explorer, take a look at (rough translation from german language): Extras, Internet Options, Connections. If you have any VPN- or Dial-out connections, check them for Proxy settings. Also check in "LAN Settings" if you have any "automatic proxy settings".

    You can also check if you can connect to google via your telnet client (manual HTTP connection). Open cmd.exe and connect to google:

    telnet www.google.com 80
    Then, type
    GET /
    (you might not see anything) and press Enter twice. Depending on your physical location, you should see something similar to
    HTTP/1.0 302 Found Location: http://www.google.at/ Cache-Control: private Content-Type: text/html; charset=UTF-8 Set-Cookie: PREF=ID=47392c3f5731cea0:FF=0:TM=1335443806:LM=1335443806: +S=cuyFu3S3rkcsl_Do; expires=Sat, 26-Apr-2014 12:36:46 GMT; path=/; do +main=.google.com Set-Cookie: NID=59=UfWRVat2ENDZmUrEV_XafYqLpl5-5DpsARihwnhqliVswI9X2DV +ABcF8Cmcx2q6TCQp7KQIPIqVx9iZPH3XRm3g-jtRz6fFry__EOSQKY2XNd7QtoZPopMjT +OR5HmTBU; expires=Fri, 26-Oct-2012 12:36:46 GMT; path=/; domain=.goog +le.com; HttpOnly P3P: CP="This is not a P3P policy! See http://www.google.com/support/a +ccounts/bin/answer.py?hl=en&answer=151657 for more info." Date: Thu, 26 Apr 2012 12:36:46 GMT Server: gws Content-Length: 218 X-XSS-Protection: 1; mode=block X-Frame-Options: SAMEORIGIN <HTML><HEAD><meta http-equiv="content-type" content="text/html;charset +=utf-8"> <TITLE>302 Moved</TITLE></HEAD><BODY> <H1>302 Moved</H1> The document has moved <A HREF="http://www.google.at/">here</A>. </BODY></HTML>

    If you see something completly different that looks like a message from your local proxy, you will have to add the correct proxy settings to your script. I never had to do this, but from a quick glance into the documentation, WWW::Mechanize seems to use/be based on LWP::UserAgent, so these settings should do the trick.

    "You have reached the Monastery. All our helpdesk monks are busy at the moment. Please press "1" to instantly donate 10 currency units for a good cause or press "2" to hang up. Or you can dial "12" to get connected directly to second level support."
Re: Can't get WWW::Mechanize get to work??
by Anonymous Monk on Apr 26, 2012 at 10:34 UTC
    Sounds like an application firewall preventing Perl to connect to a DNS server in order to resolve the hostnames.

    Do you run such a software? Check this first.