A
Angela
Hi,
I am trying to develop a cgi routine that requests an image on
behalf of a user and returns it to their mobile phone without caching the
image on my server. (Caching would break the conditions of the remote
image site's use).
I am able to return HTML content to a user but gif images have so far
defeated me.
My current code iteration looks like this:-
#!/www/perl/bin/perl.exe
use LWP::UserAgent;
use CGI;
use HTTP::Cookies;
use CGI::Carp qw/fatalsToBrowser/;
my $query = CGI-> new();
my $username=$query-> param('username');
my $password=$query-> param('password');
my $ua = LWP::UserAgent-> new;
my $req = HTTP::Request-> new(GET =
"http://secure.metoffice.com/logon.do?
username=$username&password=$password&submitted=&submit=logon");
$req- referer("http://www.activitae.com");
my $res = $ua- request($req);
my $cookie_jar = HTTP::Cookies-> new(
File = "$ENV{HOME}/cookies",
AutoSave = 1,
);
$cookie_jar-> extract_cookies($res);
my $id = $res-> {'_headers'}-> {'set-cookie'}- [10];
if ($res-> is_success) {
# for testing only
#print $res-> as_string;
#print $id;
} else {
print "Failed: ", $res-> status_line, "\n";
}
# alternate lines for testing
# returns html
#my $secreq = HTTP::Request-> new(GET =
'http://secure.metoffice.com/aviation/reports.do?type=METAR&list=04');
# returns gif
my $secreq = HTTP::Request- new(GET =
'http://secure.metoffice.com/aviation/productcomponentserver.do?
productId=157&componentId=0&imageFile=F215_1824.gif');
$secreq-> referer(http://www.activitae.com);
$cookie_jar-> add_cookie_header($secreq);
my $secres=$ua->request($secreq);
if ($secres- is_success) {
print "HTTP/1.0 \r\n";
print "Content-Type: image/gif\r\n";
print "Connection: close\r\n\r\n";
binmode print $secres-> {'_content'};
exit 0;
} else {
print "Failed: ", $secres- status_line, "\n";
}
Firstly the code does a remote logon for a user and gets a session ID,
then a second request for the image of weather data is made. When run
via the Windows development environment of Komodo code headed by GIF87 is
returned but this appears not to be a good representation of a gif image.
When the alternate line for $secreq is used good html is returned.
However, when both alternatives are run as a server cgi routine, a server
error 'premature end
of script errors' is reported.
So my code has at least two errors!!
Any pointers to the correct way of doing things will be greatly
appreciated!
Angela
I am trying to develop a cgi routine that requests an image on
behalf of a user and returns it to their mobile phone without caching the
image on my server. (Caching would break the conditions of the remote
image site's use).
I am able to return HTML content to a user but gif images have so far
defeated me.
My current code iteration looks like this:-
#!/www/perl/bin/perl.exe
use LWP::UserAgent;
use CGI;
use HTTP::Cookies;
use CGI::Carp qw/fatalsToBrowser/;
my $query = CGI-> new();
my $username=$query-> param('username');
my $password=$query-> param('password');
my $ua = LWP::UserAgent-> new;
my $req = HTTP::Request-> new(GET =
"http://secure.metoffice.com/logon.do?
username=$username&password=$password&submitted=&submit=logon");
$req- referer("http://www.activitae.com");
my $res = $ua- request($req);
my $cookie_jar = HTTP::Cookies-> new(
File = "$ENV{HOME}/cookies",
AutoSave = 1,
);
$cookie_jar-> extract_cookies($res);
my $id = $res-> {'_headers'}-> {'set-cookie'}- [10];
if ($res-> is_success) {
# for testing only
#print $res-> as_string;
#print $id;
} else {
print "Failed: ", $res-> status_line, "\n";
}
# alternate lines for testing
# returns html
#my $secreq = HTTP::Request-> new(GET =
'http://secure.metoffice.com/aviation/reports.do?type=METAR&list=04');
# returns gif
my $secreq = HTTP::Request- new(GET =
'http://secure.metoffice.com/aviation/productcomponentserver.do?
productId=157&componentId=0&imageFile=F215_1824.gif');
$secreq-> referer(http://www.activitae.com);
$cookie_jar-> add_cookie_header($secreq);
my $secres=$ua->request($secreq);
if ($secres- is_success) {
print "HTTP/1.0 \r\n";
print "Content-Type: image/gif\r\n";
print "Connection: close\r\n\r\n";
binmode print $secres-> {'_content'};
exit 0;
} else {
print "Failed: ", $secres- status_line, "\n";
}
Firstly the code does a remote logon for a user and gets a session ID,
then a second request for the image of weather data is made. When run
via the Windows development environment of Komodo code headed by GIF87 is
returned but this appears not to be a good representation of a gif image.
When the alternate line for $secreq is used good html is returned.
However, when both alternatives are run as a server cgi routine, a server
error 'premature end
of script errors' is reported.
So my code has at least two errors!!
Any pointers to the correct way of doing things will be greatly
appreciated!
Angela