use Win32::Internet;Then you have to open an Internet connection with this command:
$Connection = new Win32::Internet();This is required to use any of the function of this module. It will create an Internet object in Perl on which you can act upon with the Internet Functions explained later.
The objects available are:
There are then other functions that 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.
To make an example, there is a function called FetchURL that you can use to fetch the content of any HTTP, FTP or GOPHER URL with this simple commands:
$INET = new Win32::Internet(); $file = $INET->FetchURL("http://www.yahoo.com");You can have the same result (and this is actually what is done by FetchURL) this way:
$INET = new Win32::Internet(); $URL = $INET->OpenURL("http://www.yahoo.com"); $file = $URL->ReadFile(); $URL->Close();Or, you can open a complete HTTP session:
$INET = new Win32::Internet(); $HTTP = $INET->HTTP("www.yahoo.com", "anonymous", "dada@divinf.it"); ($statuscode, $headers, $file) = $HTTP->Request("/"); $HTTP->Close();Finally, you can choose to manage even the HTTP request:
$INET = new Win32::Internet(); $HTTP = $INET->HTTP("www.yahoo.com", "anonymous", "dada@divinf.it"); $HTTP->OpenRequest($REQ, "/"); $REQ->AddHeader("If-Modified-Since: Saturday, 16-Nov-96 15:58:50 GMT"); $REQ->SendRequest(); $statuscode = $REQ->QueryInfo("",HTTP_QUERY_STATUS_CODE); $lastmodified = $REQ->QueryInfo("Last-Modified"); $file = $REQ->ReadEntireFile(); $REQ->Close(); $HTTP->Close();To open and control a complete FTP session, type:
$Connection->FTP($Session, "ftp://ftp.activeware.com", "anonymous", "dada\@divinf.it");This will create an FTP object in Perl to which you can apply the FTP functions provided by the package:
$Session->Cd("/ntperl/perl5.001m/CurrentBuild"); $Session->Ascii(); $Session->Get("110-i86.zip"); $Session->Close();For a more complete example, see the TEST.PL file that comes with the package.
use Win32::Internet;somewhere before the method calls, and that you have an Internet object called $INET which was created using this call:
$INET = new Win32::Internet();See new for more information.
$cURL = $INET->CanonicalizeURL($URL); $URL = $INET->CanonicalizeURL($cURL, ICU_DECODE);
$INET->Close(); $FTP->Close(); $INET->Close($FTP); # same as above...
$URL = $INET->CombineURL("http://www.divinf.it/dada/perl/internet", "..");
$HTTP->ConnectBackoff(2000); $backoff = $HTTP->ConnectBackoff();
$HTTP->ConnectRetries(20); $retries = $HTTP->ConnectRetries();
$HTTP->ConnectTimeout(10000); $timeout = $HTTP->ConnectTimeout();
$HTTP->ControlReceiveTimeout(10000); $timeout = $HTTP->ControlReceiveTimeout();
$HTTP->ControlSendTimeout(10000); $timeout = $HTTP->ControlSendTimeout();
scheme, host, port, username, password, path, extrainfo
For example, the URL "http://www.divinf.it/index.html#top" can be splitted in:
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. See also CreateURL.@parts=$INET->CrackURL("http://www.activeware.com"); ($scheme, $host, $port, $user, $pass, $path, $extra) = $INET->CrackURL("http://www.divinf.it:80/perl-win32/index.sht#feedback");
%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. See also CrackURL.$URL=$I->CreateURL("http", "www.divinf.it", 80, "", "", "/perl-win32/index.sht", "#feedback"); $URL=$I->CreateURL(\%params);
$HTTP->DataReceiveTimeout(10000); $timeout = $HTTP->DataReceiveTimeout();
$HTTP->DataSendTimeout(10000); $timeout = $HTTP->DataSendTimeout();
Error number | Meaning |
-1 | A "trivial" error has occurred in the package. For example, you tried to use a method on the wrong type of object. |
1 .. 11999 | A generic error has occurred and the Win32::GetLastError error message is returned. |
12000 and higher | An Internet error has occurred; the extended Win32 Internet API error message is returned. |
See also GetResponse.
Example:
die $INET->Error(), qq(\n); ($ErrNum, $ErrText) = $INET->Error();
$file = $INET->FetchURL("http://www.yahoo.com/"); $file = $INET->FetchURL("ftp://www.activeware.com/contrib/internet.zip");
Parameter | Meaning | Default |
server | The server to connect to. | none |
username | The username used to login to the server. | anonymous |
password | The password used to login to the server. | none |
port | The port of the FTP service on the server. | 21 |
pasv | If it is a value other than 0, use passive transfer mode. | 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. | none |
If you pass hashref (a reference to an hash array), the following values are taken from the array:
%hash=( "server" => "server", "username" => "username", "password" => "password", "port" => port, "pasv" => pasv, "context" => context, );
This 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 ftpobject.
Example:
$result = $INET->FTP($FTP, "ftp.activeware.com", "anonymous", "dada\@divinf.it"); # and then for example... $FTP->Cd("/ntperl/perl5.001m/CurrentBuild"); $params{"server"} = "ftp.activeware.com"; $params{"password"} = "dada\@divinf.it"; $params{"pasv"} = 0; $result = $INET->FTP($FTP,\%params);
print $INET->GetResponse(); $INET->FTP($FTP, "ftp.activeware.com", "anonymous", "dada\@divinf.it"); print $FTP->GetResponse();
($status, $info) = $INET->GetStatusCallback(1);
Parameter | Meaning | Default |
server | The server to connect to. | none |
username | The username used to login to the server. | anonymous |
password | The password used to login to the server. | none |
port | The port of the HTTP service on the server. | 80 |
flags | Additional flags affecting the behavior of the function. | none |
context | A number to identify this operation if it is asynchronous. See SetStatusCallback and GetStatusCallback for more info on asynchronous operations. | none |
Refer to the Microsoft Win32 Internet Functions documentation for more details on those parameters.
If you pass hashref (a reference to an hash array), the following values are taken from the array:
%hash=( "server" => "server", "username" => "username", "password" => "password", "port" => port, "flags" => flags, "context" => context, );This 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.
$result = $INET->HTTP($HTTP,"www.activeware.com","anonymous","dada\@divinf.it"); # and then for example... ($statuscode, $headers, $file) = $HTTP->Request("/gifs/camel.gif"); $params{"server"} = "www.activeware.com"; $params{"password"} = "dada\@divinf.it"; $params{"flags"} = INTERNET_FLAG_RELOAD; $result = $INET->HTTP($HTTP,\%params);
Parameter | Meaning | Default |
useragent | The user agent passed to HTTP requests. See OpenRequest. | Perl-Win32::Internet/version |
opentype | How to access to the Internet (eg. directly or using a proxy). | INTERNET_OPEN_TYPE_DIRECT |
proxy | Name of the proxy server (or servers) to use. | none |
proxybypass | Optional list of host names or IP addresses, or both, that are known locally. | none |
flags | Additional flags affecting the behavior of the function. | none |
Refer to the Microsoft Win32 Internet Functions documentation for more details on those parameters. If you pass hashref (a reference to an hash array), the following values are taken from the array:
%hash=( "useragent" => "useragent", "opentype" => "opentype", "proxy" => "proxy", "proxybypass" => "proxybypass", "flags" => flags, );Example:
$INET = new Win32::Internet(); die qq(Cannot connect to Internet...\n) if ! $INET; $INET = new Win32::Internet("Mozilla/3.0", INTERNET_OPEN_TYPE_PROXY, "www.microsoft.com", ""); $params{"flags"} = INTERNET_FLAG_ASYNC; $INET = new Win32::Internet(\%params);
$INET->OpenURL($URL, "http://www.yahoo.com/"); $bytes = $URL->QueryDataAvailable(); $file = $URL->ReadEntireFile(); $URL->Close();
$HTTP->Password("splurfgnagbxam"); $password = $HTTP->Password();
$INET->OpenURL($URL, "http://www.yahoo.com/"); $bytes = $URL->QueryDataAvailable();
$value = $INET->QueryOption(INTERNET_OPTION_CONNECT_TIMEOUT); $value = $HTTP->QueryOption(INTERNET_OPTION_USERNAME);
$INET->OpenURL($URL, "http://www.yahoo.com/"); $file = $URL->ReadEntireFile();
NOTE: be careful to keep bytes to an acceptable value (eg. don't tell him to swallow megabytes at once...). ReadEntireFile in fact uses QueryDataAvailable and ReadFile in a loop to read no more than 16k at a time.
Example:
$INET->OpenURL($URL, "http://www.yahoo.com/"); $chunk = $URL->ReadFile(16000);
$INET->SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,10000); $HTTP->SetOption(INTERNET_OPTION_USERNAME,"dada");
$INET->SetStatusCallback();This is one of the step 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.
seconds, minute, hours, day, month, year, day_of_week
The second form does the opposite (or at least it should, because actually seems
to be malfunctioning): it takes the values and returns an HTTP date/time string,
in the RFC format specified by the RFC parameter (OK, I didn't find yet any accepted value
in the range 0..2000, let me know if you have more luck with it).
Example:
($sec, $min, $hour, $day, $mday, $year, $wday) = $INET->TimeConvert("Sun, 26 Jan 1997 20:01:52 GMT"); # the opposite DOESN'T WORK! which value should $RFC have??? $time = $INET->TimeConvert(52, 1, 20, 26, 1, 1997, 0, $RFC);
$INET->UserAgent("Mozilla/3.0"); $useragent = $INET->UserAgent();
$HTTP->Username("dada"); $username = $HTTP->Username();
$version = $INET->Version(); # should return "0.06/4.70.1215" @version = $INET->Version(); # should return ("0.06", "4.70.1215")
use Win32::Internet; $INET = new Win32::Internet(); $INET->FTP($FTP, "hostname", "username", "password");somewhere before the method calls; in other words, we assume that you have an Internet object called $INET and an open FTP session called $FTP.
See new and FTP for more information.
$FTP->Ascii();
$FTP->Binary();
$FTP->Cd("/pub");
$FTP->Delete("110-i86.zip");
Parameter | Meaning | Default |
remote | The name of the remote file on the FTP server. | none |
local | The name of the local file to create. | remote |
overwrite | If 0, overwrites local if it exists; with any other value, the function fails if the local file already exists. | 0 |
flags | Additional flags affecting the behavior of the function. | none |
context | A number to identify this operation if it is asynchronous. See SetStatusCallback and GetStatusCallback for more info on asynchronous operations. | none |
Refer to the Microsoft Win32 Internet Functions documentation for more details
on those parameters.
Example:
$FTP->Get("110-i86.zip"); $FTP->Get("/pub/perl/languages/CPAN/00index.html", "CPAN_index.html");
@files = $FTP->List(); @textfiles = $FTP->List("*.txt"); foreach $file (@textfiles) { print "Name: ", $file, "\n"; }
@files = $FTP->List("*.*", 2); for($i=0; $i<=$#files; $i+=7) { print "Name: ", @files[$i], "\n"; print "Size: ", @files[$i+2], "\n"; print "Attr: ", @files[$i+3], "\n"; }
@files = $FTP->List("*.*", 3); foreach $file (@files) { print $file->{'name'}, " ", $file->{'size'}, " ", $file->{'attr'}, "\n"; }Note: all times are reported as strings of the following format:
$file->{'mtime'} == "0,10,58,9,12,1996" stands for 09 Dec 1996 at 10:58:00
$FTP->Mkdir("NextBuild");
print "Current mode is: ", $FTP->Mode(); $FTP->Mode("asc"); # ... same as $FTP->Ascii();
print "Pasv is: ", $FTP->Pasv(); $INET->Pasv(1); $INET->FTP($FTP,"ftp.activeware.com", "anonymous", "dada\@divinf.it"); $FTP->Pasv(0); # this will be ignored...
$FTP->Put("internet.zip"); $FTP->Put("d:/users/dada/temp.zip", "/temp/dada.zip");
$path = $FTP->Pwd();
$FTP->Rename("110-i86.zip", "68i-011.zip");
$FTP->Rmdir("CurrentBuild");
use Win32::Internet; $INET = new Win32::Internet(); $INET->HTTP($HTTP, "hostname", "username", "password");somewhere before the method calls; in other words, we assume that you have an Internet object called $INET and an open HTTP session called $HTTP.
See new and HTTP for more information.
$HTTP->OpenRequest($REQUEST,"/index.html"); $REQUEST->AddHeader("If-Modified-Since: Sunday, 17-Nov-96 11:40:03 GMT"); $REQUEST->AddHeader("Accept: text/html\r\n", HTTP_ADDREQ_FLAG_REPLACE);
Parameter | Meaning | Default |
path | The object to request. This is generally a file name, an executable module, etc. | / |
method | The method to use; can actually be GET, POST, HEAD or PUT. | GET |
version | The HTTP version. | HTTP/1.0 |
referer | The URL of the document from which the URL in the request was obtained. | none |
accept | The content types accepted. They must be separated by a "\0" (ASCII zero). | text/* image/gif image/jpeg |
flags | Additional flags affecting the behavior of the function. | none |
context | A number to identify this operation if it is asynchronous. See SetStatusCallback and GetStatusCallback for more info on asynchronous operations. | none |
Refer to the Microsoft Win32 Internet Functions documentation for more details on those parameters. If you pass hashref (a reference to an hash array), the following values are taken from the array:
%hash=( "path" => "path", "method" => "method", "version" => "version", "referer" => "referer", "accept" => "accept", "flags" => flags, "context" => context, );See also Request.
$HTTP->OpenRequest($REQUEST, "/index.html"); $HTTP->OpenRequest($REQUEST, "/index.html", "GET", "HTTP/0.9"); $params{"path"} = "/index.html"; $params{"flags"} = " $HTTP->OpenRequest($REQUEST, \%params);
$HTTP->OpenRequest($REQUEST,"/index.html"); $statuscode = $REQUEST->QueryInfo("", HTTP_QUERY_STATUS_CODE); $headers = $REQUEST->QueryInfo("", HTTP_QUERY_RAW_HEADERS_CRLF); # will get all the headers $length = $REQUEST->QueryInfo("Content-length");
($statuscode, $headers, $file) = $HTTP->Request("/index.html"); ($s, $h, $f) = $HTTP->Request("/index.html", "GET", "HTTP/1.0");
$HTTP->OpenRequest($REQUEST, "/index.html"); $REQUEST->SendRequest(); # A POST request... $HTTP->OpenRequest($REQUEST, "/cgi-bin/somescript.pl", "POST"); #This line is a must -> (thanks Philip :) $REQUEST->AddHeader("Content-Type: application/x-www-form-urlencoded"); $REQUEST->SendRequest("key1=value1&key2=value2&key3=value3");