Forum Moderators: coopster & phranque

Message Too Old, No Replies

Quest on LWP::Simple::get

No content returned

         

flashfan

7:05 pm on Jun 28, 2004 (gmt 0)

10+ Year Member




sub fetch{
my $url = '..'; # a long url with length of 800
my $cont = LWP::Simple::get($url);
die $cont; #return $cont;
}

My cgi calls the routine fetch. But nothing shows in the error_log; When using LWP::Simple::getprint($url), 200 is shown in the error_log; and content outputs to browser. Don't know why LWP::Simple::get($url) doesn't work. BTW, the server access_log tells a 200 status returned, and right content has been sent back.

I tried to run 'perl **.cgi' at command line, it doesn't return the right content.

Any help will be highly appreciated.

VectorJ

7:40 pm on Jun 28, 2004 (gmt 0)

10+ Year Member



The syntax is correct, so I'm thinking it must be something in the URL. Can you post the URL (or at least the part after [example.com...]

flashfan

8:24 pm on Jun 28, 2004 (gmt 0)

10+ Year Member



The url is:

$url = 'http://www.blah.com/search?uid=xx&pwd=xx&fields=a,b,c,d!,e!&format=1';

$url = "http://www.example.com/fetch.cgi?uri=" . uri_escape($url);

VectorJ

8:50 pm on Jun 28, 2004 (gmt 0)

10+ Year Member



From what I can tell, the LWP::Simple module just isn't capable of handling a URL like that. LWP::UserAgent seems to work though:


use LWP::UserAgent;
my $ua = LWP::UserAgent->new();
$res = $ua->get("http://example.com/search?uid=xx&pwd=xx&fields=a,b,c,d!,e!&format=1");
print $res->content();

flashfan

9:06 pm on Jun 28, 2004 (gmt 0)

10+ Year Member



Unfortunately, LWP::UserAgent doesn't work as well. I used code (below); same result.


my $ua = LWP::UserAgent->new;
my $request = HTTP::Request('GET' => $url);
my $response = $ua->request($request);

if ($response->is_success){
print $response->content;
}else{
die "Bad luck";
}

VectorJ

11:05 pm on Jun 28, 2004 (gmt 0)

10+ Year Member



I assume that you used the $url variable twice in your previous post for the purpose of clarity, but if not that would of course be a problem.

Would it be possible to post more of the code so that I could try to do some debugging?

flashfan

12:27 pm on Jun 29, 2004 (gmt 0)

10+ Year Member



If fetching xml file, it will fail. But if fetching plain text file (i.e. html), it works.


#######################
# fetch.pm
########################
sub ParseXML
{
my $params = shift;
my ($uid, $pwd) = Access();
my $url = 'http://www.blah.com/junk.cgi';
$url = ConstructURL($url, $params);
my $xml = LWP::Simple:get($url);
return $xml;
}

sub ConstructURL
{
my ($url, $params) = @_;
my @tmp = ();
while ( my ($k, $v) = each (%$params) ){
push(@tmp, "$k=$v");

push(@tmp, "$k=$v");
}
return $url . "?" . join('&', @tmp);
}

##############################################
# blah.cgi
###########################################
use fetch;
use CGI ':standard';

my $params = { 'a' => 1, 'b' => 9, .., 'filename' => 'xx'};
my $xml = fetch($params);
print header, start_html('Test'), p($xml), end_html;
___END___

##########################################
# junk.cgi (on server B)
########################################

use CGI ':standard';

my $par = param('filename');
my $fn = "$ENV{WEB_ROOT}/xml/$par.xml";
my $xml = '';
open(FH, "<$fn") ¦¦ dienice();
$xml .= $_ while(<FH>);
close(FH);
print header, $xml;

__END__

VectorJ

4:26 am on Jul 1, 2004 (gmt 0)

10+ Year Member



Sorry, I'm at a loss. Anyone else?

If you've found the solution, please post it!