When I run the mechanize script from the shell with the /https?_proxy / set to the proxy server, it works through proxy, but when I run the script without that environment variable set in the shell and try to set the proxy internally in the program (script), it doesn't work.
I tried setting:
$ENV{'https_proxy'} = 'http://127.0.0.1:8118';
or:
$ENV{'HTTPS_PROXY'} = 'http://127.0.0.1:8118';
prior to calling:
$mech->get($url);
Also, I tried the same, as above, but adding the following prior to ->get command:
$mech->env_proxy;
Also, instead of setting the $ENV{var} with ->env_proxy command, the following, prior to ->get:
$mech->proxy(['https'], 'http://127.0.0.1:8118');
or:
$mech->proxy(['http'], 'http://127.0.0.1:8118');
Also, tried to disable https, as was suggested on some forums:
$mech->proxy(['http'], 'http://127.0.0.1:8118');
$mech->proxy(['https'], undef);
but nothing worked. My goal is to be able to dynamically switch proxy for the same url, i.e. different proxies are set to work through different providers and there is also an internet on direct connection. I want to cycle between direct connection and different proxies, on fail, until getting success. My current code is:
#!/usr/bin/env perl
use WWW::Mechanize;
my @https_proxy = (
'http://127.0.0.1:8118'
);
my $url = shift;
#print "LWP::Protocol::https::VERSION: ($LWP::Protocol::https::VERSION
+)\n";
$ENV{'https_proxy'} = $https_proxy[0];
my $mech = WWW::Mechanize->new(autocheck=>1);
$mech->proxy(['http'], $https_proxy[0]);
#$mech->proxy(['https'], undef);
#$mech->env_proxy;
$mech->get($url);
print $mech->content;
UPDATE
So, since I got no help here, I got help from people on perl telegram channel:
https://t.me/modernperl/217572
$ENV{'HTTPS_PROXY'} = 'http://127.0.0.1:8118';
my $mech = WWW::Mechanize->new(
autocheck=>1,
noproxy=>0
);
$mech->env_proxy;
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.