On Sat, 17 Nov 2007 07:38:54 -0800, lcerneka wrote: >> >> > I get an html page with a 400 error code (Bad Request) >> >> > When asking for this page directly from a browser (Firefox or IE) it >> >> > works fine... >> >> >> This happens often enough that it is covered in the FAQ for >> >> WWW::Mechanize: >> [snip] >> > Thanks guys for tries and good link... but I still cannot figure it >> > out... I tried to debug with use LWP::Debug qw(+) , and even tried to >> > extract the frame as a link with my @frame_links = $mech- >> >>find_link( tag => "frame" ) as suggested in the Mechanize FAQ... but >> > they don't work... the result is always the same: 400 Bad request... >> > I'm a bit frustrated 'cause I often write code to data retrieval and >> > this is the first time such an error occurs... The 400 error code is >> > about malformed URL's syntax or request header... Any other idea? > > #!/usr/bin/perl -w > use strict; > use warnings; > use WWW::Mechanize; > > my $mech = WWW::Mechanize->new( agent => 'Mozilla 2.0.0.9' ); > > $mech->get('http://www.aeroporto.fvg.it/tab/fmarrb.php'); > my $arrivi = $mech->content; > > print "Content-type: text/html\n\n"; > print $arrivi; > > exit;
Okay, I reproduced this and solved it. First, I verified that I got a proper response from Safari. Then I ran tcpdump to compare the request sent by Safari with the one sent by Mech. (Regrettably, setting LWP::Debug +conns does not show traffic unless you take the obscure step of setting the environment variable PERL_LWP_USE_HTTP_10 to revert to HTTP 1.0, so I use tcpdump instead.) Then I adjusted headers until I found one that worked. Add $mech->add_header( Accept => '*/*' ); before the fetch and the server will respond properly. Don't know why it's behaving that way. If you look at the source of the page in a browser there's no frame so looking for one would be pointless. A couple of other comments: -w is redundant when you have the superior use warnings. exit is redundant as the last statement. That's what it's going to do anyway. -- Peter Scott http://www.perlmedic.com/ http://www.perldebugged.com/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/