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

HI im so happy 'cause im part of perlmonks :D!!! guys, im doin some kinda of post request to a grandstream gateway, so im using something like this:
use Data::Dumper;\n use LWP::UserAgent; use Apache2::Connection (); my $ua = LWP::UserAgent->new; $ua->agent("Mozilla 12.50 "); $ua->timeout("3"); $a=$ua->post("http://192.168.0.164/dologin.htm","P2=>pass1"); print Dumper($a);
but my response is like :
500 read timeout
if i use wget it happen exactlly the same behavior....so i try with telnet and it works!! But i dont know whats going on with perl, i did some sniff arround packet and there are some diferences between packets from perl and packtes from a mozilla brwowser, for example: Connnection parameter with perl is :
Connection: keep-alive, TE, close\r\n
connection parameter with any browser is:
Connection: keep-alive\r\n
So any clue of what could be? im kinda stuck here :(

Replies are listed 'Best First'.
Re: Request POST with LWP
by kcott (Archbishop) on Nov 03, 2010 at 23:40 UTC

    Your post is very difficult to read - take a look at Writeup Formatting Tips. Formatting added - thankyou

    You've set your timeout to 3 seconds - the documentation for LWP::UserAgent shows the default as 3 minutes (180 seconds).

    Try commenting out $ua->timeout("3"); (or setting it to a much higher value) and see if that helps.

    -- Ken

Re: Request POST with LWP
by iza (Monk) on Nov 04, 2010 at 10:27 UTC

    i had the exaxt same problem and the solution was to initialize the proxy before sending the request

    perldoc lwpcook gives some hints on this :

    PROXIES Some sites use proxies to go through fire wall machines, or just a +s cache in order to improve performance. Proxies can also be used for + accessing resources through protocols not supported directly (or sup +ported badly :-) by the libwww-perl library. You should initialize your proxy setting before you start sending +requests: use LWP::UserAgent; $ua = LWP::UserAgent->new; $ua->env_proxy; # initialize from environment variables # or $ua->proxy(ftp => 'http://proxy.myorg.com'); $ua->proxy(wais => 'http://proxy.myorg.com'); $ua->no_proxy(qw(no se fi)); my $req = HTTP::Request->new(GET => 'wais://xxx.com/'); print $ua->request($req)->as_string; The LWP::Simple interface will call env_proxy() for you automatica +lly. Applications that use the $ua->env_proxy() method will normally no +t use the $ua->proxy() and $ua->no_proxy() methods. Some proxies also require that you send it a username/password in +order to let requests through. You should be able to add the required + header, with something like this: use LWP::UserAgent; $ua = LWP::UserAgent->new; $ua->proxy(['http', 'ftp'] => 'http://username:password@proxy.myo +rg.com'); $req = HTTP::Request->new('GET',"http://www.perl.com"); $res = $ua->request($req); print $res->decoded_content if $res->is_success;
Re: Request POST with LWP
by aquarium (Curate) on Nov 03, 2010 at 23:38 UTC
    please put code in code tags.
    if you're sure that it's a header..you can set the headers using LWP.
    however, try not setting timeout and see what happens
    also check that there's no automatic gateway or proxy set on your system when you use browser or telnet clients, as you would have to set these manually for LWP.
    the hardest line to type correctly is: stty erase ^H
Re: Request POST with LWP
by suhijo (Novice) on Nov 04, 2010 at 18:05 UTC
    Hey Thanks, you were fast to answer me, i did all you guys told me and still not working. What i really dont undestand is why do i have to set up proxy, when there is no such of thing like that in my lan?. :s Also ive been catching some http packtes and ive observs that versions use by my gateway is http 1.0 and perl uses http 1.1? maybe is wrong ? and how do i change set this?

      take a look at LWP::Debug

      also, have you tried kcott's solution (3 seconds _is_ short !) ?