[Cover] [Previous Section] [Next Section] [Index]
Current chapter: Perl HTTP Utilities
Section 65 out of 79 total sections
, Section 2 out of 5 sections in this chapter
HTTPConnect()
Description
HTTPConnect() connects a specified URL to a socket. It reads the HTTP status line but leaves the rest of the output. HTTPConnect() handles up to five redirects. If it doesn't load the specified URL after the fifth redirect, it fails, returning 0. As an option, you can specify the request method, GET or POST. If the method is POST, the last parameter is the content to send to the server. If you do not specify a method, GET is assumed.
Synopsis
require 'HTTPClient.pl';
...
&AppUtils::HTTPConnect($fh, $url, $method, $postArgs);
Arguments
|
Argument
|
Meaning
|
$fh
|
A file(socket) handle to create.
|
$url
|
The URL to use, of the form http://[host[:port]]/[path].
|
$method
|
The request method (optional).
|
$postArgs
|
Args for the POST method (optional).
|
Returns
|
Value
|
Meaning
|
0
|
Failure.
|
1
|
Success.
|
Example
The following code retrieves the current temperature for Elko, Nevada, and generates the HDML to display it as shown in Figure 12-1:
#!/usr/local/bin/perl5.001
#
require 'HDMLUtils.pl';
require 'HTTPClient.pl';
$ret = &AppUtils::HTTPConnect(\*HTTP,
"http://iwin.nws.noaa.gov/iwin/nv/hourly.html");
while (<HTTP>)
{
if (/elko/i)
{
$report = $_;
last;
}
}
&AppUtils::HTTPDisconnect(\*HTTP);
if($ret == 0 || $report eq undef)
{
$report = "Report not available";
}
$deck = "<HDML VERSION=3.0 TTL=0>".
"<DISPLAY>".
"$report".
"</DISPLAY>".
"</HDML>";
&AppUtils::OutputDeck($deck);
FIGURE 12-1.
Display card with the current temperatures for Stateline.
[Cover] [Previous Section] [Next Section] [Index]
Current chapter: Perl HTTP Utilities
Section 65 out of 79 total sections
, Section 2 out of 5 sections in this chapter
Copyright © 1999, Unwired Planet, Inc. All rights
reserved.