Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

How to Detect HTTP proxy settings automatically in Windows OS?

by sam_bakki (Pilgrim)
on Apr 24, 2012 at 11:30 UTC ( #966800=perlquestion: print w/replies, xml ) Need Help??
sam_bakki has asked for the wisdom of the Perl Monks concerning the following question:

Dear Perl Gurus,

I am writing a perl script to download files from internet using perl LWP , WWW::Mechanize modules. Currently I rely on HTTP_PROXY environment variable to understand the proxy. But it is pain to instruct the Windows users to set this variable and use my script.

I am thinking to find the HTTP proxy automatically and i was looking into MSDN and I found following windows APIs,

1. WinHttpGetProxyForUrl ,

2. InternetGetProxyInfo ,

I tried to use InternetGetProxyInfo API by using following code but looks like API itself is broken and suggestion is to use WinHttpGetProxyForUrl .

use strict; use warnings; use Win32::Internet; use Win32::API; #$Win32::API::DEBUG = 1; my $InternetGetProxyInfo = Win32::API->new('jsproxy.dll', 'BOOL Intern +etGetProxyInfo(LPCSTR lpszUrl, DWORD dwUrlLength, LPSTR lpszUrlHostNa +me, DWORD dwUrlHostNameLength, LPSTR lplpszProxyHostName, LPDWORD lpd +wProxyHostNameLength)') or warn "\n ERROR: Can not import API:i2cGetD +eviceAddress , $^E ,"; my $lpszUrl = ''; my $dwUrlLength = length($lpszUrl); my $lpszUrlHostName = ''; my $dwUrlHostNameLength = length($lpszUrlHostName); my $lplpszProxyHostName = " " x 100; my $lpdwProxyHostNameLength = 0; print $InternetGetProxyInfo->Call($lpszUrl,$dwUrlLength,$lpszUrlHostNa +me,$dwUrlHostNameLength,$lplpszProxyHostName,$lpdwProxyHostNameLength +) or print "\n ERR: ",$^E; print "\n ERR:", Win32::FormatMessage(Win32::GetLastError()); print "\n 1: $lplpszProxyHostName \n 2: $lpdwProxyHostNameLength \n";

Question: Any one tried this API in perl already? If so can you please point me some code reference? Or is there any better way to detect HTTP proxy automatically?

Thanks & Regards,

Perl Gtk2 technology demo project -

Replies are listed 'Best First'.
Re: How to Detect HTTP proxy settings automatically in Windows OS?
by Anonymous Monk on Apr 24, 2012 at 11:40 UTC

    See / try Scripting Proxy Settings in IE and Firefox,

    sub get_ie_proxy { return "" unless $^O eq 'MSWin32'; my %RegHash; eval 'use Win32::TieRegistry(Delimiter=>"/", TiedHash=>\%RegHash); +'; return get_ie_proxy_with_registry() if $@; my $iekey = $RegHash{"CUser/Software/Microsoft/Windows/CurrentVers +ion/Internet Settings/"} or return ""; my $ie_proxy_enable = $iekey->{"/ProxyEnable"} or return ""; my $ie_proxy_server = $iekey->{"/ProxyServer"} or return ""; my $ie_proxy_no = $iekey->{"/ProxyOverride"}; $_[1]=$ie_proxy_no if defined($ie_proxy_no); return $ie_proxy_enable=~/1$/ ? $ie_proxy_server : ""; }

      Hi There

      Thanks for the quick reply. Your solution may suite perfectly if Windows user explicitly sets proxy server (and port) in IE.

      But in our organization (and most others too) uses auto configuration proxys (proxy.pac).
      In my Windows 7 machine, Under HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings, I have

      AutoConfigURL =

      I thought of parsing this file and get the real proxy but to me Windows APIs looks simple approach (ofcourse if it works :) ) than parsing .pac file.

      Of course, Your suggestion is also needed for me because few windows user set direct proxy server and port in IE settings but most others have auto proxy config scripts.

      So , What is a best way to get proxy information (Like many installers do , Ex: chrome installer) in windows OS?

      Thanks & Regards,
      Bakkiaraj M
      My Perl Gtk2 technology demo project - , contributions are welcome.

        I thought of parsing this file and get the real proxy but to me Windows APIs looks simple approach (ofcourse if it works :) ) than parsing .pac file.

        Well, if you put it that way :)

        HTTP::ProxyAutoConfig - use a .pac or wpad.dat file to get proxy information

        HTTP::ProxyPAC - use a PAC (Proxy Auto Config) file to get proxy info

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://966800]
Front-paged by Corion
[Corion]: hippo: If you have a Unicode-wise Perl then likely some zero-width characters in $x would work. Maybe $x = "\x{200b}" works.
[Corion]: Hmm - no, that outputs 1 for me on 5.14 - perl -wle "my $x = qq(\x{200b}); warn $x; warn length $x"
[hippo]: Smart - I'll give that a go. Thanks.
[hippo]: Ah
[Corion]: But maybe there is some other Unicode string that will be true but have a zero width
[hippo]: For explanation, I've seen this construct in someone else's code (no names, no pack drill) and couldn't think of a situation to trigger it.
[Corion]: You'll have to look somewhere esoteric for that. Maybe some tied variable or special dualvar can also trigger that. But it's certainly not a common occurrence
[Corion]: And on 5.20, the following also outputs no find:perl -wle 'for my $x ("\x{2000}".."\ x{1fffff}") { if( $x && ! length $x ) { warn qq(<$x>); warn length $x; die } }'

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2017-07-27 13:28 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (413 votes). Check out past polls.