Search code examples
perlhttplwplwp-useragent

Send a plain string request with LWP


To get a response from a certain website, I have to give one exact request string, HTTP/1.1. I tried that one with telnet, it gives me the response I want (a redirect, but I need it).

But when I try to give the same request string to HTTP::Request->parse(), I merely get the message 400 URL must be absolute.

I am not sure if it's the website or LWP giving me that, because as I said, the response worked with telnet.

This is the code:

my $req = "GET / HTTP/1.1\n".
  "Host: www.example-site.de\n".
  "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1\n".
  "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\n".
  "Accept-Language: en-us,en;q=0.5\n".
  "Accept-Encoding: gzip, deflate\n".
  "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\n".
  "Keep-Alive: 115\n".
  "Connection: keep-alive\n";

# Gives correct request string
print HTTP::Request->parse($req)->as_string;

my $ua = LWP::UserAgent->new( cookie_jar => {}, agent => '' );
my $response = $ua->request(HTTP::Request->parse($req));

# 400 error
print $response->as_string,"\n";

Anyone can help me here?


Solution

  • Ok, I did it using Sockets. After all, I had the HTTP request and wanted the plain response. Here the code for people who are interested:

    use IO::Sockets;
    
    my $sock = IO::Socket::INET->new(
        PeerAddr => 'www.example-site.de',
        PeerPort => 80, 
        Proto => 'Tcp',
    );
    die "Could not create socket: $!\n" unless $sock;
    
    print $sock, $req;
    
    while(<$sock>) {
        # Look for stuff I need
    }
    
    close $sock;
    

    It's just important to remember to leave the while, as the HTTP response won't end with an EOF.