Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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 , http://msdn.microsoft.com/en-us/library/windows/desktop/aa384097(v=vs.85).aspx

2. InternetGetProxyInfo , http://msdn.microsoft.com/en-us/library/windows/desktop/aa384726(v=vs.85).aspx

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 = 'http://www.google.com'; my $dwUrlLength = length($lpszUrl); my $lpszUrlHostName = 'www.google.com'; 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,
Bakki

Perl Gtk2 technology demo project - http://code.google.com/p/saaral-soft-search-spider/

Comment on How to Detect HTTP proxy settings automatically in Windows OS?
Download Code
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, http://cpansearch.perl.org/src/QJZHOU/LWP-UserAgent-ProxyAny-1.02/ProxyAny.pm

    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 = http://apac.nics.nxp.com:8080/proxy.pac

      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 - http://code.google.com/p/saaral-soft-search-spider/ , 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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://966800]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2014-09-22 02:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (177 votes), past polls