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

Hello monks!

I am looking for a solution how to do a HTTP get request to a IP (let's say 192.168.0.100), port 80 by specifying virtual host that does not exist (non existing domain)! I am connecting to servers that have multiple vhosts defined on single IP but these vhosts are internal (e.g. server1, server2 etc.). If I enter IP and host into hosts file it works just by getting http://server1/ etc. but I need to run my script also on servers where I can not modify hosts file.

Somebody already suggested to me that I should try to specify server's IP address as proxy in LWP::UserAgent request like:

my $ua = LWP::UserAgent->new; $ua->proxy('http','http://192.168.0.100:80/'); my $response = $ua->get('http://server1/');

but this doesn't work (it returns 500 Can't connect to 192.168.0.100:80 (connect: No route to host))

Can someone suggest how this could be done without the need to modify /etc/hosts file? Thanx!

Replies are listed 'Best First'.
Re: Get data via HTTP on IP and specify virtual host
by tobyink (Canon) on Feb 28, 2012 at 21:25 UTC
    my $ua = LWP::UserAgent->new; my $req = HTTP::Request->new( GET => 'http://192.168.0.100:80/', [Host => 'server1'], ); my $res = $ua->request($req);
Re: Get data via HTTP on IP and specify virtual host
by Anonymous Monk on Feb 28, 2012 at 18:51 UTC
    $ lwp-request -t1 -USEd -H "Host: Server1" http://127.0.0.1/ GET http://127.0.0.1/ Host: Server1 User-Agent: lwp-request/6.03 libwww-perl/6.03 500 Can't connect to 127.0.0.1:80 (timeout) Content-Type: text/plain Client-Date: Tue, 28 Feb 2012 18:56:44 GMT Client-Warning: Internal response

      That is quite interesting, Anonymous.   Would you, or someone else, please elaborate on this?

        Look in RFC 2616, for the Host: header. It's what makes multiple websites live on the same IP address/port combination.