Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

authenticate to a proxy server [Again!]

by abhishes (Friar)
on Feb 22, 2003 at 18:25 UTC ( [id://237781]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Perl monks,

This is a repeat question... so please don't get annoyed for asking it again. I didn't understand the replies in the first thread ...so I am brining up the question again.

I connect to the internet via a proxy server. the proxy server is NOT NTLM ... I am sure because I am able to browse the web thru mozilla. if it was NTLM then only IE will work. My proxy server is also not SOCKS.

I suppose it is just a plain proxy server which needs loginid and password before it forwards the request out of our network.

I have searched many times google and this site ... but I cannot find how can I do this in perl.

In my search I was able to find a C# program which did the very same thing which I want. Please let me know how can I do the same thing in perl

using System; using System.IO; using System.Net; public class TestNet { public static void Main(string[] args) { string url = "http://www.perlmonks.com"; HttpWebRequest req = (HttpWebRequest) WebRequest.Create(url); + WebProxy proxy = new WebProxy("proxy.server.intranet:8080"); proxy.Credentials = new NetworkCredential("user", "password"); req.Proxy = proxy; HttpWebResponse resp = (HttpWebResponse) req.GetResponse(); + StreamReader reader = new StreamReader(resp.GetResponseStream()); + while(reader.Peek() > -1) { Console.WriteLine(reader.ReadLine()); } reader.Close(); resp.Close(); } }
thanks.
Abhishek.

Replies are listed 'Best First'.
Re: authenticate to a proxy server [Again!]
by integral (Hermit) on Feb 22, 2003 at 18:40 UTC
    If you want to use the LWP::UserAgent module which is a popular way of performing HTTP requests in perl, then the answer can be found in its lwpcook pod. You either use the HTTP_PROXY environment variable or the proxy method on the object. In both you need to use the http://username:password@host:port/ url format to encode your proxy username and password, the port number is optional.

    --
    integral, resident of freenode's #perl
    
Re: authenticate to a proxy server [Again!]
by toma (Vicar) on Feb 23, 2003 at 08:36 UTC
    When I first started using LWP I had trouble getting the correct syntax for this line:
    $ua->proxy(['http'], 'http://myproxy.mycorp.com/');
    My errors led me to believe that the proxy server wanted some sort of password or something, but I was mistaken.

    You could also try the LWP::Debug module.

    If you need more ideas you can check my LWP Quick Reference Guide.

    It should work perfectly the first time! - toma

      Thank you everyone for your replies and patience. But my program doesn't work. Here is my code

      use strict; use warnings; use LWP::UserAgent; my $content; my $ua = new LWP::UserAgent; $ua->proxy(['http'], 'http://my_username:my_pwd@proxy.mycompany.intran +et/autoproxy:8080/'); $ua->agent("Mozilla/6.0"); my $req = new HTTP::Request(GET => 'http://www.perlmonks.com/'); my $res = $ua->request($req); print $res->content if ($res->is_success);

      I also tried another variation. I set 3 environment variables HTTP_PROXY=http://proxy.mycompany.intranet/autoproxy:8080/
      HTTP_proxy_pass=my_username
      HTTP_proxy_user=my_pwd

      Now I changed the code to
      use strict; use warnings; use LWP::UserAgent; my $content; my $ua = new LWP::UserAgent; $ua->env_proxy(); $ua->agent("Mozilla/6.0"); my $req = new HTTP::Request(GET => 'http://www.perlmonks.com/'); my $res = $ua->request($req); print $res->content if ($res->is_success);

      It still didn't work!

      When I run this program it does not print the contents of the perlmonks page. It prints a script file on my console. This is exactly the same output which I used to get in my C# program till I had configured the proxy authentication properly.

      Currently the output of running my perl code is

      function FindProxyForURL (url, host) { if ( shExpMatch (host, "127.0.0.1") || isPlainHostName(host) || dnsDomainIs(host,"mycompany.intranet") || shExpMatch(host,"xy.xyz.*") || ) return "DIRECT"; return "PROXY proxy.mycompany.intranet:8080"; }

      Regarding starting a new thread and not posting it to the old one, I did so because I was afraid, that if i post on the old thread no one will read it unless they go down to that date. If i start a new one then it will appear in the newest node where there is more probability of monks reading my question.

      regards,
      Abhishek.

        Perhaps your proxy is using WebDoubler or something similar. The script you are getting is a chunk of JavaScript that is sent to the browser for automatic proxy detection.

        The identity of your real proxy server is in the return value of the function:

        proxy.mycompany.intranet:8080
        So the proper configuration is:
        $ua->proxy(['http'],'http://proxy.mycompany.intranet:8080/');
        I found this with a Google search on FindProxyForURL. A good trick for figuring things out is to type your error message into Google.

        It should work perfectly the first time! - toma

Re: authenticate to a proxy server [Again!]
by Anonymous Monk on Feb 23, 2003 at 01:10 UTC
    This is a repeat question... so please don't get annoyed for asking it again. I didn't understand the replies in the first thread ...so I am brining up the question again
    Don't get annoyed? If you didn't understand the replies, you should 've responded to them saying something like "Excuse me, i don't understand ....". Starting a new thread is annoying.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://237781]
Approved by data64
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-04-23 21:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found