I figured out what was wrong, I was  giving useragent POST parameters that
had already been URL encoded ("Log+In" rather than "Log In" for example)

-----Original Message-----
From: Chris Greenhill [mailto:chr...@oxxfordinfo.com] 
Sent: Tuesday, July 12, 2011 12:29 PM
To: libwww@perl.org
Subject: Problems logging in

Hi all,

 

I'm having problems logging into a site with LWP and I think it might be
something to do with the header or an encoding thing.  

 

I have to parse out a couple session identifiers from the log in page

 

                $login_page =~ s/[\r\n]//g;

                $login_page =~ /__VIEWSTATE" value="/;

                $viewstate = $';

                $viewstate =~ s/".+$//;

                $eventvalidation = '';

                $login_page =~ /EVENTVALIDATION" value="/;

                $eventvalidation = $';

                $eventvalidation =~ s/".+$//;

 

And here's the code I've been trying

 

                $ua->cookie_jar($cookie_jar);

 

 
$ua->default_header("Accept"=>"text/html,application/xhtml+xml,application/x
ml;q=0.9,*/*;q=0.8",

                "Accept-Language"=>"en-us,en;q=0.5",

#             "Accept-Encoding"=>"gzip,deflate",

                "Accept-Charset"=>"ISO-8859-1,utf-8;q=0.7,*;q=0.7",

                "Keep-Alive"=>"300",

                "Connection"=>"keep-alive",

                "Referer"=>"http://****/Login.aspx";);

 

                $url = "http://****/Login.aspx";;

                $response= $ua->request(POST
$url,["__EVENTTARGET"=>"","__EVENTARGUMENT"=>"","__VIEWSTATE"=>$viewstate,"_
_EVENTVALIDATION"=>$eventvalidation,"Login1_UserName"=>"botuser","Login1_Pas
sword"=>"bu12086","Login1_LoginButton"=>"Log+In"]);

                $cookie_jar->extract_cookies($response);

                $content = $response->as_string;

 

 

According to Wireshark, here's what a successful POST header generated by
Firefox looks like:

 

    POST /Login.aspx HTTP/1.1\r\n

        [Expert Info (Chat/Sequence): POST /Login.aspx HTTP/1.1\r\n]

            [Message: POST /Login.aspx HTTP/1.1\r\n]

            [Severity level: Chat]

            [Group: Sequence]

        Request Method: POST

        Request URI: /Login.aspx

        Request Version: HTTP/1.1

    Host ****\r\n

    User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.9.1.8)
Gecko/20100202 Firefox/3.5.8 ( .NET CLR 3.5.30729)\r\n

    Accept:
text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n

    Accept-Language: en-us,en;q=0.5\r\n

    Accept-Encoding: gzip,deflate\r\n

    Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n

    Keep-Alive: 300\r\n

    Connection: keep-alive\r\n

    Referer: http://****/Login.aspx\r\n

    Cookie: ASP.NET_SessionId=hfsexrcoq4sdeo1pz4sl0iov\r\n

    Content-Type: application/x-www-form-urlencoded\r\n

    Content-Length: 439\r\n

        [Content length: 439]

    \r\n

    [Full request URI: http://oxx002.oxxfordinfo.com/Login.aspx]

Line-based text data: application/x-www-form-urlencoded

    [truncated] __EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEP(long
gibberish string)

 

And here's what the failed header looks like that was generated by my script

 

    POST /Login.aspx HTTP/1.1\r\n

        [Expert Info (Chat/Sequence): POST /Login.aspx HTTP/1.1\r\n]

            [Message: POST /Login.aspx HTTP/1.1\r\n]

            [Severity level: Chat]

            [Group: Sequence]

       Request Method: POST

        Request URI: /Login.aspx

        Request Version: HTTP/1.1

    TE: deflate,gzip;q=0.3\r\n

    Connection: keep-alive, TE, close\r\n

    Accept:
text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n

    Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n

    Accept-Language: en-us,en;q=0.5\r\n

    Host: ****\r\n

    Referer: http://****/Login.aspx\r\n

    User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.9.1.8)
Gecko/20100202 Firefox/3.5.8 ( .NET CLR 3.5.30729)\r\n

    Content-Length: 435\r\n

        [Content length: 435]

    Content-Type: application/x-www-form-urlencoded\r\n

    Cookie: ASP.NET_SessionId=4upqtr1oqenxgwil2tqlu42u\r\n

    Cookie2: $Version="1"\r\n

    Keep-Alive: 300\r\n

    \r\n

    [Full request URI: http://oxx002.oxxfordinfo.com/Login.aspx]

Line-based text data: application/x-www-form-urlencoded

    [truncated] __EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEP(long
gibberish string)

 

 

Main difference I notice is the different content lengths. A successful log
in returns a 302 redirect, but I've just been getting the original log in
page back. 

What am I doing wrong? Do I need to encode the session variables (I thought
LWP did that automatically)? 

 

Thanks for any help,

Chris

 


Reply via email to