Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WWW::Mechanize , how to re-route browser

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
Hi,

Can someone explain please how you get to submit a form and physically route the browser to the next screen after the form is submitted?

I've used the following...
Code:
$mech->submit_form(
        form_number => 1,
        fields      => { %vals },
    );
    die unless ($mech->success);

and all I get is
CGI Error
The specified CGI application misbehaved by not returning a complete set of HTTP headers.

I'm basically trying to log someone into a 3rd party ancillary website, i've got the form, i've filled them in, now I need to make the CGI work like the user has submitted the login form and route the browser to the 3rd party site.

I've even tried print $mech->response; but that just prints a text version of the form still with the URL as my PERL program, when it should be the 3rd party website after being logged in.

Is this possible, I can only seem to get Mechanize to do what appears to be screen scraping, not act like you were really on the 3rd party site.

All help appreciated.

1DMF.



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
I dunno I've scoured the net for help and examples everything I do produces the same result.

Submit doesn't submit the browser and display the result , no matter which way I try to do it.

$mech->submit();
$mech->click();

etc.. etc...

I've even used
Code:
foreach my $key (@fields){
            if($key->type eq "text" || $key->type eq "password"){
                $cnt++;
                $mech->field( $key->name, $user[$cnt] );
                $name = $key->name;
            }        
}

my $html = $mech->content;
$html =~ s[login.aspx][[URL unfurl="true"]http://myurl/login.aspx[/URL]]isg;
$html =~ s[<body>][<body onload=\"document.getElementById('aspnetForm').submit();\">]isg;
$mech->update_html( $html );
print $html;

Which just displays the form but all the fields are blank, so $mech->field() doesn't seem to work, allthough if I use
Code:
die "name = $name , value = " . $mech->value( $name );
after the for loop it shows the correct value for the field.

So why does $mech->update_html( $html ); not update the form with the values set in $mech->field();

And at the end of the day if i print anything returned for ANY operation to the screen the URL is still and not the location of the 3rd party website after having logged in via the submitted form.

I'm obviously missing something, but I cannot find a single working example on the net, every thing I try produces the same result, with my perl program still being the URL in the address bar?

Has anyone managed to get to login to a web form?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
:) I've cracked it!!!!!!

1stly I had to decide what ever I thought $mech->submit() and its derivatives do, they don't and so needed to come at it from a different angle!

So this is what I got that works...
Code:
# Load webpage
my $URL = "[URL unfurl="true"]http://domain_of_3rd_party_site.co.uk/login.aspx?url=%2fdefault.aspx";[/URL]

my $mech = [URL unfurl="true"]WWW::Mechanize->new();[/URL]

$mech->agent_alias( 'Windows IE 6' );

$mech->get( $URL ); 

my $html = $mech->content;
$html =~ s[login.aspx][[URL unfurl="true"]http://domain_of_3rd_party_site.co.uk/login.aspx[/URL]]i;
$html =~ s[<body>][<body onload=\"__doPostBack('ctl00\$ContentPlaceHolder\$Login1\$Submit','');\">]i;
$html =~ s[User\"][User\" value=\"$user\"]i;                
$html =~ s[MyCode\"][MyCode\" value=\"$rs[0]{'My_Code'}\"]i;
$html =~ s[Password\"][Password\" value=\"$rs[0]{'Password'}\"]i;

print $html;

Some of the problem was to do with this pesky dopostback JS rubish, also I was having a mare with the form field name place holders having dollar signs in them, so I did a non-global replace using the tail end var names so the id= part didn't get affected.

Also I have to insert the FQ URI as the form was using a relative path for the form action.

rebuild the html and print to browser, which when loads, runs the dopostback script and login is achieved.

not great but serves my purpose!



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top