coolsaurabh has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to execute one URL from the CGI script but failing to do.Any inputs are highly welcome.Please find below the two codes used for the same.

use CGI ':standard'; use HTTP::Request; use LWP::Simple qw/get/; $url = 'https://10.204.16.17:16316/ibm/console/login.do?action=secure' +; `wget $url`;
#!/usr/bin/perl use LWP::UserAgent ; use HTTP::Request ; my $URL = 'https://10.204.16.17:16316/ibm/console/login.do?action=secu +re'; my $agent = LWP::UserAgent->new(env_proxy => 1,keep_alive => 1, timeou +t => 30); my $header = HTTP::Request->new(GET => $URL); my $request = HTTP::Request->new('GET', $URL, $header); my $response = $agent->request($request); # Check the outcome of the response if ($response->is_success){ print "URL:$URL\nHeaders:\n"; print $response->headers_as_string; print "\nContent:\n"; print $response->as_string; }elsif ($response->is_error){ print "Error:$URL\n"; print $response->error_as_HTML; }

Replies are listed 'Best First'.
Re: Unable to execute URL from perl/cgi script
by Anonymous Monk on Feb 05, 2015 at 19:59 UTC

    When I replace the URL in each of the code examples, they both run for me. If the code is executing on your side too, then the problem may be on the network or on the server side. It would be good if you could elaborate on "failing to do".... what error messages are you getting? Can you access these URLs from the command line? Please see How do I post a question effectively? and Basic debugging checklist.

    Or, perhaps the code isn't doing what you want it to? The first code example does seem a little strange: You load LWP::Simple, but then call the external wget command. If you wrote my $data = get($url);, you could fetch the URL into Perl without going to the external command. If you could explain the bigger picture of what you are trying to do that would be good.

    BTW, since you're using LWP::Simple and CGI together, please see the section "Caveat" at the bottom of LWP::Simple.

      Thanks for the reply. Whenever I am running the script,new window is opening but now the correct URL which is mentioned in the code. I am not sure where the problem exists exactly but the code is running fine and no error is found in the new window. Basic nutshell is that I have to open the URL whenever the script is executed.

      #!/usr/bin/perl use CGI ':standard'; use HTTP::Request; use LWP::Simple qw/get/; #use LWP::UserAgent; print header(); print start_html(); $a=param('$selected_rows.Location'); $a =~ s/\D//g; $a =~ s/^[0]+//g; $url = "http://operationalintelligence/Site/sitevisit.php?site=$a&type +=a&days=90"; #$url = 'https://10.204.16.17:16316/ibm/console/login.do?action=secure +'; my $data=get($url); print end_html();

        Sorry but I'm still not quite clear on what you are trying to do. But I'm going to wager two guesses: 1. Do you want to fetch the page from the URL and display it to the user? In that case, you could drop the start_html() and end_html() calls and simply say print $data; - However, note your script currently does no error handling, and this would only reliably work for plain HTML pages; if you have images, CSS files, etc. those may or may not work correctly depending on how they are referenced in the HTML.

        Or, 2. Do you want to redirect the user to the new URL? In that case, look at the redirect function from CGI (section "Generating a redirection header"), and note that you should not output anything else (i.e. no header(), no start_html(), etc.)

        Note that you really, really should be doing use warnings; use strict; at the top of your script. See Use strict and warnings.

        If I am still misunderstanding what you want to do then what might help would be if you could give a step-by-step explanation of the expected behavior of the user and your script.