tomgracey has asked for the wisdom of the Perl Monks concerning the following question:
Hello all. Consider the following code:
#!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; my $ip = 'xxx.xxx.xxx.xxx'; # but with actual real numbers obviously my $port = '82'; # ie not port 80 my $domain_name = 'www.example.com'; my $ua = LWP::UserAgent->new; my $target = "http://$ip:$port/"; $ua->default_header('Host' => $domain_name); my $response = $ua->get($target); print "Response: ".$response->decoded_content."\n";
The 'Host' header is there so the destination server knows which website I'm after on that ip and port. Surely this header should not have any effect on where LWP sends the request? However, I'm getting this:
Response: 500 Can't connect to www.example.com:82It seems to be taking the Host header, adding the port and going to look for that, instead of looking for what is passed to it in $target. Very odd!
If I remove the line that adds the default_header, then:
Response: 200 OKStumped! Can anyone shed any light?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Adding a 'Host' request header leads to unexpected LWP::UserAgent behaviour
by BrowserUk (Patriarch) on Jun 16, 2016 at 16:41 UTC | |
|
Re: Adding a 'Host' request header leads to unexpected LWP::UserAgent behaviour
by tomgracey (Scribe) on Jun 19, 2016 at 07:39 UTC | |
|
Re: Adding a 'Host' request header leads to unexpected LWP::UserAgent behaviour (proof example.com)
by Anonymous Monk on Jun 16, 2016 at 23:57 UTC |