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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PERL cgi Status Code 302 problem 1

Status
Not open for further replies.

griffgg8

Programmer
Dec 20, 2006
2
US
I have been trying for a few weeks to retrieve a html page programatically that requires a username/password. Normally, the username/password is submitted via the POST method, the response from the server is Status Code 302 (temporaryly moved). The redirection is completed using the GET method and the correct page pops up. When I do it programatically, I do NOT get the correct page (it defautlts to asking for the username and password again).

Any help will be greatly appreciated.
I have tried everything I can think of.

Thanks in advance,

griffgg8

Here's part of the code:
Code:
use LWP::UserAgent;
use HTTP::Headers;
use HTTP::Status;
use HTTP::Request::Common;
use HTTP::Response;
use URI::URL;
use URI;

$ua = LWP::UserAgent->new;
[COLOR=red]
$response = $ua->request(POST 	'[URL unfurl="true"]http://www.website.com/DT/login.asp',[/URL]
	[
	    username => 'xxxxxxxxxxxxxxxxx',
	    password => 'xxxxxxxxxxxxxxxxx'
	],
	[
		HTTP-EQUIV => '1.1',
		Host 	=> '[URL unfurl="true"]www.website.com',[/URL]
		User-Agent => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)',
		Accept 	=> 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*',
		Accept-Language	=> 'en-us',
		Accept-Encoding => 'gzip, deflate',
		Proxy-Connection => 'Keep-Alive',
		Pragma 		=> 'no-cache',
		Referer 				=> '[URL unfurl="true"]http://www.website.com/DT/login.asp',[/URL]
		Cookie 				=> 'ASPSESSIONIDAQDSTCTD=GKDOMDBDEKCPAJEIPOODFIAA; BTC=LogFlag=1&ContactID=C6UJ9A007HO8',
		Location	=> '/dt/default.asp',
		Previous				=> '[URL unfurl="true"]http://www.website.com/DT/login.asp',[/URL]
		Content-Type 	=> 'application/x-www-form-urlencoded'
    ]
);
[/color]
$original_response_code = $response->code;
[COLOR=blue]if ($original_response_code =~ /302/)
{[/color]
	$referral       = LWP::UserAgent->new;
	$referral_uri 	= $response->header('Location');
	local $URI::ABS_ALLOW_RELATIVE_SCHEME = 1;
	$base 		= $response->base;
	$referral_uri 	= $HTTP::URI_CLASS->new($referral_uri, $base)->abs($base);
[COLOR=red]	$response2 = $referral->request(GET $referral_uri);[/color]
	printf "   response2=%s\n", $response2;
[COLOR=blue]}[/color]
else
{
	$content = $response->content();
	printf  "   else contents=%s\n", $content;
};
exit (0);
 
Hi

I do not know the LWP module, so I can not help to correct it.

But the problem must be somewhere around the [tt]new[/tt] :
Code:
    $referral       = LWP::UserAgent->new;
    [gray]# ...[/gray]
    $response2 = $referral->request(GET $referral_uri);
So you create a completely new request without passing forward the cookie(s) containing the authentication's result, received from the previous request.

Feherke.
 
I tried "clone"ing the original $ua by replacing "$referral = LWP::UserAgent->new;" with "$referral = $ua->clone;" and got the same results. Got any other ideas?

Thanks,

griffgg8
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top