22.7. Win32::Internet
The Win32::Internet extension
implements the Win32 Internet APIs (found in
WININET.DLL), providing support for HTTP, FTP,
and Gopher connections.
All types of connections start as a basic Internet connection that
must be opened with the following command:
use Win32::Internet;
$Connection = Win32::Internet->new( );
This creates an Internet object in Perl on which you use the
functions provided in this module to create more specific connection
objects. The objects and functions that create them are:
-
Internet connections (the main object, with new)
-
URLs (with OpenURL)
-
FTP sessions (with FTP)
-
HTTP sessions (with HTTP)
-
HTTP requests (with OpenRequest)
This module provides different levels of implementation of the Win32
Internet functions. Some routines use several Win32 API functions to
perform a complex task in a single call; they are simpler to use, but
less powerful. Other functions implement nothing more and nothing
less than the corresponding API function, so you can use all of their
power, but with some additional programming steps.
For example, the function FetchURL fetches the
contents of any HTTP, FTP, or Gopher URL with a simple command:
$inet = new Win32::Internet( );
$file = $inet->FetchURL("http://www.yahoo.com");
You can achieve the same result with this series of commands, which
is what FetchURL actually does:
$inet = new Win32::Internet( );
$url = $inet->OpenURL("http://www.yahoo.com");
$file = $url->ReadFile( );
$url->Close( );
22.7.1. General Internet Methods
The methods described in this section are used on Internet
connection objects created with
new:
$inet = Win32::Internet->new( );
You can supply new with an optional list of
arguments (or a reference to a hash containing them) that looks like
this:
Win32::Internet->new [useragent, opentype, proxy, proxybypass, flags]
Win32::Internet->new [$hashref]
The parameters and their values are:
- useragent
-
The user agent string passed to HTTP requests. Default is
Perl-Win32Internet/version.
- opentype
-
How to access the Internet (e.g., directly or using a proxy). Default
is INTERNET_OPEN_TYPE_DIRECT.
- proxy
-
Name of the proxy server (or servers) to use. Default is none.
- proxybypass
-
Optional list of hostnames or IP addresses that are known locally.
Default is none.
- flags
-
Additional flags affecting the behavior of the function. Default is
none.
If you pass a hash reference to the function, the following values
are taken from the hash:
%hash=(
"useragent" => "useragent",
"opentype" => "opentype",
"proxy" => "proxy",
"proxybypass" => "proxybypass",
"flags" => flags,
);
The following methods can be used on Internet connection objects.
$inet->CrackURL(URL, [flags])
Splits a URL into its
component parts and returns them in an array. Returns
undef on error. The array will contain the
following values: (scheme,
host, port,
username,
password, path,
extrainfo). For example, the URL
http://www.divinf.it/index.html#top can be
split into:
http, www.divinf.it, 80, anonymous, dada@divinf.it, /index.html, #top
If you don't specify a flags parameter,
ICU_ESCAPE will be used by default; for the
possible values of flags, refer to the
Microsoft Win32 Internet Functions documentation.
$inet->CreateURL(scheme, hostname, port, username, password, path, extrainfo, [flags])
$inet->CreateURL($hashref, [flags])
| |
Creates a URL from its
component parts. Returns undef on error and the
created URL if successful. If you pass a hash reference, the
following values are taken from the array:
%hash=(
"scheme" => "scheme",
"hostname" => "hostname",
"port" => port,
"username" => "username",
"password" => "password",
"path" => "path",
"extrainfo" => "extrainfo",
);
If you don't specify a flags parameter,
ICU_ESCAPE will be used by default; for the other
possible values of flags, refer to the
Microsoft Win32 Internet Functions documentation.
$inet->FTP($ftpobject, server, username, pwd, [port, pasv, context])
$inet->FTP($ftpobject, $hashref)
| |
Opens an FTP connection to
server, logging in with the given username
and password. The new connection object is saved to
ftpobject. The parameters and their values
are:
- server
-
The server to connect to.
- username
-
The username used to log in to the server. Default is
anonymous.
- pwd
-
The password used to log in to the server. Default is none.
- port
-
The port of the FTP service on the server. Default is
21.
- pasv
-
If it is a value other than 0, use passive
transfer mode. Otherwise, it is taken from the parent Internet
connection object; you can set this value with the
Pasv method.
- context
-
A number to identify this operation if it is asynchronous. See
SetStatusCallback and
GetStatusCallback for more info on asynchronous
operations.
If you pass a hash reference, the following values are taken from the
hash:
%hash=(
"server" => "server",
"username" => "username",
"password" => "password",
"port" => port,
"pasv" => pasv,
"context" => context,
);
The FTP method returns undef if
the connection failed, a number otherwise. You can then call any of
the FTP functions as methods of the newly created FTP object.
$inet->HTTP(httpobject, server, username, password, [port, flags, context])
$inet->HTTP($httpobject, $hashref)
| |
Opens an HTTP connection to
server, logging in with the given username
and password. The new connection object is saved as
httpobject. The parameters and their
values are:
- server
-
The server to connect to.
- username
-
The username used to log in to the server. Default is
anonymous.
- password
-
The password used to log in to the server. Default is none.
- port
-
The port of the HTTP service on the server. Default is
80.
- flags
-
Additional flags affecting the behavior of the function.
- context
-
A number to identify this operation if it is asynchronous.
If you pass a hash reference, the following values are taken from the
hash:
%hash=(
"server" => "server",
"username" => "username",
"password" => "password",
"port" => port,
"flags" => flags,
"context" => context,
);
The HTTP method returns undef
if the connection failed, a number otherwise. You can then call any
of the HTTP functions as methods of the newly created
httpobject.
$inet->SetStatusCallback( )
Initializes the callback
routine used to return data about the progress of an asynchronous
operation. This is one of the steps required to perform asynchronous
operations; the complete procedure is:
# Use the INTERNET_FLAG_ASYNC when initializing
$params{'flags'}=INTERNET_FLAG_ASYNC;
$inet = new Win32::Internet(params);
# Initialize the callback routine
$inet->SetStatusCallback( );
# Specify the context parameter (the last 1 in this case)
$inet->HTTP($http, "www.yahoo.com", "anonymous", "dada\@divinf.it",
80, 0, 1);
At this point, control returns immediately to Perl, and
$inet->Error( ) will return
997, which means an asynchronous I/O operation is
pending. Now, you can call:
$http->GetStatusCallback(1);
in a loop to verify what's happening; see also
GetStatusCallback.
22.7.2. FTP Functions
The methods described in this section are
used to control FTP sessions. They apply to FTP session objects
created by the FTP method on an Internet
connection object. FTP creates an open FTP session
and assigns it to an object ($FTP):
use Win32::Internet;
$inet = new Win32::Internet( );
$inet->FTP($FTP, "hostname", "username", "password");
The following methods are used on FTP session objects.
$FTP->List([pattern, listmode])
$FTP->Ls([pattern, listmode])
$FTP->Dir([pattern, listmode])
| |
Returns a list containing the files
found in the current directory, matching the given
pattern, if specified. The content of the
returned list depends on the listmodeparameter, which can have the following values:
- 1 (default)
-
The list contains the names of the files found.
- 2
-
The list contains seven values for each file:
- 3
-
The list contains a reference to a hash for each found file. Each
hash contains the following key/value pairs:
name => filename
altname => DOS short filename, a.k.a. 8.3
size => size
attr => attributes
ctime => creation time
atime => last access time
mtime => last modified time
All times are reported as strings of the following format: second,
hour, minute, day, month, year. For example:
$file->{'mtime'} == "0,10,58,9,12,1996"
# Stands for 09 Dec 1996 at 10:58:00
22.7.3. HTTP Functions
The methods described in this section are
used to create and control an HTTP session. Open an HTTP session
using the HTTP method on an Internet connection
object:
use Win32::Internet;
$inet = new Win32::Internet( );
$inet->HTTP($http, "hostname", "username", "password");
This opens the session and creates the HTTP session object
$http. The following methods can be used on HTTP
session objects.
$http->OpenRequest(requestobject, [path, method, version,
referer, accept, flags, context])
$http->OpenRequest($requestobject, hashref)
| |
Opens an HTTP request and
saves it as
$requestobject. Returns
undef on error, or a number if the connection was
successful. You can then use one of the AddHeader,
SendRequest, QueryInfo,
QueryDataAvailable, and
ReadFile methods on the newly created
requestobject. The optional parameters and
their values are:
- path
-
The object to request. This is generally a filename, an executable
module, etc. The default is /.
- method
-
The method to use, which can be GET, POST, HEAD, or PUT. Default is
GET.
- version
-
The HTTP version. Default is HTTP/1.0.
- referer
-
The URL of the document from which the URL in the request was
obtained.
- accept
-
The content types accepted. They must be separated by a
\0 (ASCII zero). Default types are text/*
image/gif image/jpeg.
- flags
-
Additional flags affecting the behavior of the function.
- context
-
A number to identify this operation if it is asynchronous. See
SetStatusCallback and
GetStatusCallback for more information on
asynchronous operations.
A reference to a hash containing the previous list of parameters can
also be supplied to this method:
%hash=(
"path" => "path",
"method" => "method",
"version" => "version",
"referer" => "referer",
"accept" => "accept",
"flags" => flags,
"context" => context,
);
$http->Request([path, method, version, referer, accept, flags])
$http->Request(hashref)
| |
Performs an HTTP request and
returns an array containing the status code, the headers, and the
content of the file. It is a one-step procedure that executes
OpenRequest, SendRequest,
QueryInfo, ReadFile, and
finally, Close. For a description of the
parameters, see OpenRequest.
 |  |  | 22.6. Win32::FileSecurity |  | 22.8. Win32::IPC |
Copyright © 2002 O'Reilly & Associates. All rights reserved.
|