Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Proxy Authentication

by rmb (Novice)
on Jun 10, 2004 at 14:59 UTC ( #363098=perlquestion: print w/replies, xml ) Need Help??

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

Hi, Help and advice required please. This short script works ok on internal web pages but how do I add proxy authentication? and should the username and password be in plain text or encoded in some way? The proxy server is windows based.
#!/usr/local/bin/perl use LWP::UserAgent; use HTTP::Request; use HTTP::Response; my $ua = new LWP::UserAgent; ##$ua->proxy('http', 'http://my_domain\my_username:my_passwd@10.10.10. +10:8080/'); $ua->proxy('http', ''); $ua->no_proxy(''); my $request = new HTTP::Request('GET', $ARGV[0]); ##$request->proxy_authorization_basic( 'my_domain\my_username', 'my_pa +sswd' ); my $response = $ua->request($request); if ($response->is_success) { print $response->content; } else { print $response->error_as_HTML; }
Thanks, rmb

Replies are listed 'Best First'.
Re: Proxy Authentication
by Corion (Patriarch) on Jun 10, 2004 at 15:03 UTC

    I am a lazy person and thus put my proxy authorization into the HTTP_PROXY environment variable, and let LWP::UserAgent do the rest:

    $ENV{HTTP_PROXY} = 'http://corion:secret@my.proxy.example:8080';

    But you don't say why you commented out the $ua->proxy parts in your script - why don't you use the methods that LWP::UserAgent provides?

      Corion, Thanks, I commented them out because I could not get them to work. I don't like the idea of passwords being in plain text either in the script or in the users environment. Do you know of anyway round that? Thanks, rmb

        Of course - ask the user for it:

        use ExtUtils::MakeMaker; my $user = prompt "Proxy user:"; my $password = prompt "Proxy password:"; # ... continue as before

        In short, if you want to use data in a program, somehow that data needs to get to that program. There is no way around it.

        Also, regarding your manual proxy settings, "I could not get them to work" is not really helpfull, as the weather impedes my ESP abilities somewhat. Maybe you can describe to us a bit more precise what fails, and how it fails, and what the log messages and error messages are...

        I don't like the idea of passwords being in plain text either in the script or in the users environment. Do you know of anyway round that?
        Well, you could just manually (or semi-manually) add the raw header
        Proxy-Authorization: Basic <string>
        where <string> is a base64 encoded string containing the username and password separated by a colon, generated like so:
        use MIME::Base64; print encode_base64( join( ':', $username, $password ) );
        and either manually print it in the right spot (after the GET or POST line)
        print "Proxy-Authorization: Basic SSdtIG5vdCB0aGF0IHN0dXBpZC4uLiA6KQ== +\n";
        or, in your case, use HTTP::Headers
        use HTTP::Headers; my $header = HTTP::Headers->new( Proxy_Authorization => 'Basic SSdtIG5 +vdCB0aGF0IHN0dXBpZC4uLiA6KQ==' );
        and add $header as the third parameter to your HTTP::Request constructor:
        my $request = new HTTP::Request('GET', $ARGV[0], $header);
        (insert usual disclaimers about passwords... blah blah... security through obscurity... blah blah...)

        - ><iper

        use japh; print;
Re: Proxy Authentication
by eclark (Scribe) on Jun 10, 2004 at 17:35 UTC

    I just tried your script using the first commented line ($ua->proxy) with the uri for our squid proxy at work. It worked perfect.

    Can you provide more detail about the error you're getting?

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2023-05-28 05:25 GMT
Find Nodes?
    Voting Booth?

    No recent polls found