http://www.perlmonks.org?node_id=966800

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/