<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: lwp code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 17 May 2008 19:01:13 GMT</pubDate>
    <description>DZone Snippets: lwp code</description>
    <item>
      <title>Browser automation using perl LWP</title>
      <link>http://snippets.dzone.com/posts/show/4587</link>
      <description>// This is a sample code used to measure reports response times on a OAS application.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/perl&lt;br /&gt;#&lt;br /&gt;# LWP connection to the Datamart Portal&lt;br /&gt;# Timing of the main reports&lt;br /&gt;#&lt;br /&gt;&lt;br /&gt;use strict;&lt;br /&gt;require LWP::UserAgent;&lt;br /&gt;&lt;br /&gt;my $ua = LWP::UserAgent-&gt;new;&lt;br /&gt;&lt;br /&gt;sub isodate() {&lt;br /&gt;        my ($day, $mon, $year, $hour, $min, $sec) = (localtime)[3, 4, 5, 2, 1, 0];&lt;br /&gt;        $mon++; # 0-based index&lt;br /&gt;        $year = $year + 1900;&lt;br /&gt;        my $date = sprintf ("%04i-%02i-%02i %02i\:%02i\:%02i", $year, $mon, $day, $hour, $min, $sec);&lt;br /&gt;        return $date;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;sub datamart_login {&lt;br /&gt;        my ( $user, $pass ) = @_;&lt;br /&gt;        my $time_begin=time();&lt;br /&gt;        my $url='http://daprd:7782/portal/page?_pageid=37,134413,37_134422&amp;_dad=portal&amp;_schema=PORTAL';&lt;br /&gt;        my $req = HTTP::Request-&gt;new( GET =&gt; $url );&lt;br /&gt;        my $resp = $ua-&gt;request($req);&lt;br /&gt;        my $loginform = $resp-&gt;content ;&lt;br /&gt;        if ( $loginform !~ /Entrez votre nom utilisateur/ ) {&lt;br /&gt;                die isodate()." Failed to get the logon page of the Web Site\n";&lt;br /&gt;        } else {&lt;br /&gt;                my $locale="";&lt;br /&gt;                my ($v) = $loginform =~ /NAME=\"v\" value=\"(.+)\"/;&lt;br /&gt;                my ($site2pstoretoken) = $loginform =~ /NAME=\"site2pstoretoken\" value=\"(.+)\"/;&lt;br /&gt;                my ($submiturl) = $loginform =~ /form method=\"POST\" action=\"(.*?)\"/;&lt;br /&gt;                $resp = $ua-&gt;post( $submiturl,&lt;br /&gt;                   [&lt;br /&gt;                     ssousername =&gt; $user,&lt;br /&gt;                     password =&gt; $pass,&lt;br /&gt;                     v =&gt; $v,&lt;br /&gt;                     locale =&gt; $locale,&lt;br /&gt;                     site2pstoretoken =&gt; $site2pstoretoken&lt;br /&gt;                   ],&lt;br /&gt;                );&lt;br /&gt;                $resp = $ua-&gt;get($url);&lt;br /&gt;                $resp = $ua-&gt;get($url);&lt;br /&gt;                if ( $resp-&gt;content !~ /Crit..res de recherche/ ) {&lt;br /&gt;                        die isodate()." Failed to get the main page of Portal\n";&lt;br /&gt;                }&lt;br /&gt;        }&lt;br /&gt;        print join(";",isodate(),"Time to log on the Portal",time()-$time_begin,$url)."\n";&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;sub datamart_testurl {&lt;br /&gt;        my ($label,$url,$expected)=@_;&lt;br /&gt;        my $time_begin=time();&lt;br /&gt;        my $resp;&lt;br /&gt;        $resp = $ua-&gt;get($url);&lt;br /&gt;        $resp = $ua-&gt;get($url) if $resp-&gt;content !~ /$expected/;        # We try two times !&lt;br /&gt;        if ( $resp-&gt;content !~ /$expected/ ) {&lt;br /&gt;                print STDERR isodate()." Test Failed on $label. $expected not found in response.\n";&lt;br /&gt;                print STDERR $resp-&gt;as_string;&lt;br /&gt;        } else {&lt;br /&gt;                print join(";",isodate(),$label,time()-$time_begin,$url)."\n";&lt;br /&gt;        }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;$ua-&gt;timeout(1200);&lt;br /&gt;$ua-&gt;cookie_jar({});&lt;br /&gt;$ua-&gt;agent( 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)' );&lt;br /&gt;#push @{ $ua-&gt;requests_redirectable }, 'POST';&lt;br /&gt;&lt;br /&gt;# &gt;&gt;&gt;&gt; Main code here&lt;br /&gt;&lt;br /&gt;datamart_login("xxxx","xxxx");&lt;br /&gt;&lt;br /&gt;open FH,"/exploit/scripts/appli/check_datamart.ini" or die "Unable to open check_datamart.ini";&lt;br /&gt;while (&lt;FH&gt;) {&lt;br /&gt;        chomp();&lt;br /&gt;        my ($report,$expected,$url) = split /;/;&lt;br /&gt;        datamart_testurl($report,$url,$expected);&lt;br /&gt;}&lt;br /&gt;close FH;&lt;br /&gt;&lt;br /&gt;# &lt;&lt;&lt;&lt; Main code here&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 28 Sep 2007 11:48:47 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4587</guid>
      <author>bouffon69 (Sylvain Le Courtois)</author>
    </item>
    <item>
      <title>Perl - send form POST</title>
      <link>http://snippets.dzone.com/posts/show/3163</link>
      <description>// Manda le informazioni per il login ad un determinato sito&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;use HTTP::Request::Common qw(POST);&lt;br /&gt;use LWP::UserAgent;&lt;br /&gt;&lt;br /&gt;my $user = "user";&lt;br /&gt;my $pass = "pass";&lt;br /&gt;&lt;br /&gt;my $browser = LWP::UserAgent-&gt;new();&lt;br /&gt;&lt;br /&gt;my $responde = HTTP::Request-&gt;new(POST =&gt; "http://www.sito.com/index.php");&lt;br /&gt;$responde-&gt;content_type("application/x-www-form-urlencoded");&lt;br /&gt;$responde-&gt;content("user=" . $user . "&amp;pass=" . $pass);&lt;br /&gt;&lt;br /&gt;$browser-&gt;request($responde)-&gt;as_string&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 18 Dec 2006 23:57:55 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3163</guid>
      <author>whitetiger ()</author>
    </item>
    <item>
      <title>Getting Documents with LWP</title>
      <link>http://snippets.dzone.com/posts/show/1009</link>
      <description>&lt;code&gt;&lt;br /&gt;my $url ='http://freshair.npr.org/dayFA.cfm?todayDate=current';&lt;br /&gt;    # Just an example: the URL for the most recent /Fresh Air/ show&lt;br /&gt;&lt;br /&gt;  use LWP::Simple;&lt;br /&gt;  my $content = get $url;&lt;br /&gt;  die "Couldn't get $url" unless defined $content;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 23 Dec 2005 09:02:55 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1009</guid>
      <author>koogunmo ()</author>
    </item>
  </channel>
</rss>
