in reply to trouble talking to html form using LWP

perrinwas right

You missed cookie which you recieve on first request to this page

Set-Cookie: ASP.NET_SessionId=tc0xi055f1dny0555tbamc55; path=/

Replies are listed 'Best First'.
Re^2: trouble talking to html form using LWP
by mhearse (Chaplain) on Sep 05, 2007 at 03:10 UTC
    You all are correct, but this code still doesn't get the results:
    #!/usr/bin/perl use strict; use HTTP::Request::Common; use LWP::UserAgent; use HTTP::Cookies; use URI::Escape; my $url = q{http://cgmix.uscg.mil/PSIX/VesselSearch.aspx}; my $ua = LWP::UserAgent->new(); $ua->cookie_jar( HTTP::Cookies->new( 'file' => '/home/matt/cookies.txt', )); my $safe_key = uri_escape($key); my $req = POST $url, Content_Type => 'form_data', Content => [ VesselName => 'FLINTHORN', VesselNumber => '', VesselCallSign => '', VesselHull => '', VesselYear => '', CheckBoxOutService => '', __VIEWSTATE => $safe_key, ]; my $res = $ua->request($req);print $res->content();
    It is getting the correct cookie:
    $VAR1 = bless( { '_content' => 'VesselName=FLINTHORN&VesselNumber=&Ves +selCallSign=&VesselHull=&VesselYear=&CheckBoxOutService=&__VIEWSTATE= +SHORTKEYTOSAVESPACE', '_uri' => bless( do{\(my $o = 'http://cgmix.uscg.mil/ +PSIX/VesselSearch.aspx')}, 'URI::http' ), '_headers' => bless( { 'user-agent' => 'libwww-perl/5 +.65', 'content-type' => 'form_data', 'cookie' => '$Version=2; ASP.N +ET_SessionId=xmtfq5nrirypi245hpi20e45; $Port="80"', 'content-length' => 126 }, 'HTTP::Headers' ), '_method' => 'POST' }, 'HTTP::Request' );

      This code works for me:

      #!/usr/bin/perl use strict; use HTTP::Request::Common; use LWP::UserAgent; my $ua = LWP::UserAgent->new(); my $url = q{http://cgmix.uscg.mil/PSIX/VesselSearch.aspx}; my $req = GET $url; my $res = $ua->request($req); my ($key) = $res->content() =~ m{name="__VIEWSTATE" value="([^"]+)"}; $req = POST $url, Content_Type => 'application/x-www-form-urlencoded', Content => [ VesselName => 'FLINTHORN', VesselNumber => '', VesselCallSign => '', VesselHull => '', DropDownListFlag => 'ALL', DropDownListService => 'ALL', VesselYear => '', CheckBoxOutService => '', Submit1 => 'Search', __VIEWSTATE => $key, ]; $res = $ua->request($req); print $res->content(); __END__

      As you can see Content_Type => 'form_data' is one of mistakes

        Thanks!!

      Also look to WWW::Mechanize for such things.