Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

281 lines
8.2 KiB

  1. =head1 NAME
  2. lwpcook - libwww-perl cookbook
  3. =head1 DESCRIPTION
  4. This document contain some examples that show typical usage of the
  5. libwww-perl library. You should consult the documentation for the
  6. individual modules for more detail.
  7. All examples should be runnable programs. You can, in most cases, test
  8. the code sections by piping the program text directly to perl.
  9. =head1 GET
  10. It is very easy to use this library to just fetch documents from the
  11. net. The LWP::Simple module provides the get() function that return
  12. the document specified by its URL argument:
  13. use LWP::Simple;
  14. $doc = get 'http://www.sn.no/libwww-perl/';
  15. or, as a perl one-liner using the getprint() function:
  16. perl -MLWP::Simple -e 'getprint "http://www.sn.no/libwww-perl/"'
  17. or, how about fetching the latest perl by running this command:
  18. perl -MLWP::Simple -e '
  19. getstore "ftp://ftp.sunet.se/pub/lang/perl/CPAN/src/latest.tar.gz",
  20. "perl.tar.gz"'
  21. You will probably first want to find a CPAN site closer to you by
  22. running something like the following command:
  23. perl -MLWP::Simple -e 'getprint "http://www.perl.com/perl/CPAN/CPAN.html"'
  24. Enough of this simple stuff! The LWP object oriented interface gives
  25. you more control over the request sent to the server. Using this
  26. interface you have full control over headers sent and how you want to
  27. handle the response returned.
  28. use LWP::UserAgent;
  29. $ua = new LWP::UserAgent;
  30. $ua->agent("$0/0.1 " . $ua->agent);
  31. # $ua->agent("Mozilla/8.0") # pretend we are very capable browser
  32. $req = new HTTP::Request 'GET' => 'http://www.sn.no/libwww-perl';
  33. $req->header('Accept' => 'text/html');
  34. # send request
  35. $res = $ua->request($req);
  36. # check the outcome
  37. if ($res->is_success) {
  38. print $res->content;
  39. } else {
  40. print "Error: " . $res->status_line . "\n";
  41. }
  42. The lwp-request program (alias GET) that is distributed with the
  43. library can also be used to fetch documents from WWW servers.
  44. =head1 HEAD
  45. If you just want to check if a document is present (i.e. the URL is
  46. valid) try to run code that looks like this:
  47. use LWP::Simple;
  48. if (head($url)) {
  49. # ok document exists
  50. }
  51. The head() function really returns a list of meta-information about
  52. the document. The first three values of the list returned are the
  53. document type, the size of the document, and the age of the document.
  54. More control over the request or access to all header values returned
  55. require that you use the object oriented interface described for GET
  56. above. Just s/GET/HEAD/g.
  57. =head1 POST
  58. There is no simple procedural interface for posting data to a WWW server. You
  59. must use the object oriented interface for this. The most common POST
  60. operation is to access a WWW form application:
  61. use LWP::UserAgent;
  62. $ua = new LWP::UserAgent;
  63. my $req = new HTTP::Request 'POST','http://www.perl.com/cgi-bin/BugGlimpse';
  64. $req->content_type('application/x-www-form-urlencoded');
  65. $req->content('match=www&errors=0');
  66. my $res = $ua->request($req);
  67. print $res->as_string;
  68. Lazy people use the HTTP::Request::Common module to set up a suitable
  69. POST request message (it handles all the escaping issues) and has a
  70. suitable default for the content_type:
  71. use HTTP::Request::Common qw(POST);
  72. use LWP::UserAgent;
  73. $ua = new LWP::UserAgent;
  74. my $req = POST 'http://www.perl.com/cgi-bin/BugGlimpse',
  75. [ search => 'www', errors => 0 ];
  76. print $ua->request($req)->as_string;
  77. The lwp-request program (alias POST) that is distributed with the
  78. library can also be used for posting data.
  79. =head1 PROXIES
  80. Some sites use proxies to go through fire wall machines, or just as
  81. cache in order to improve performance. Proxies can also be used for
  82. accessing resources through protocols not supported directly (or
  83. supported badly :-) by the libwww-perl library.
  84. You should initialize your proxy setting before you start sending
  85. requests:
  86. use LWP::UserAgent;
  87. $ua = new LWP::UserAgent;
  88. $ua->env_proxy; # initialize from environment variables
  89. # or
  90. $ua->proxy(ftp => 'http://proxy.myorg.com');
  91. $ua->proxy(wais => 'http://proxy.myorg.com');
  92. $ua->no_proxy(qw(no se fi));
  93. my $req = HTTP::Request->new(GET => 'wais://xxx.com/');
  94. print $ua->request($req)->as_string;
  95. The LWP::Simple interface will call env_proxy() for you automatically.
  96. Applications that use the $ua->env_proxy() method will normally not
  97. use the $ua->proxy() and $ua->no_proxy() methods.
  98. Some proxies also require that you send it a username/password in
  99. order to let requests through. You should be able to add the
  100. required header, with something like this:
  101. use LWP::UserAgent;
  102. $ua = new LWP::UserAgent;
  103. $ua->proxy(['http', 'ftp'] => 'http://proxy.myorg.com');
  104. $req = new HTTP::Request 'GET',"http://www.perl.com";
  105. $req->proxy_authorization_basic("proxy_user", "proxy_password");
  106. $res = $ua->request($req);
  107. print $res->content if $res->is_success;
  108. Replace C<proxy.myorg.com>, C<proxy_user> and
  109. C<proxy_password> with something suitable for your site.
  110. =head1 ACCESS TO PROTECTED DOCUMENTS
  111. Documents protected by basic authorization can easily be accessed
  112. like this:
  113. use LWP::UserAgent;
  114. $ua = new LWP::UserAgent;
  115. $req = new HTTP::Request GET => 'http://www.sn.no/secret/';
  116. $req->authorization_basic('aas', 'mypassword');
  117. print $ua->request($req)->as_string;
  118. The other alternative is to provide a subclass of I<LWP::UserAgent> that
  119. overrides the get_basic_credentials() method. Study the I<lwp-request>
  120. program for an example of this.
  121. =head1 HTTPS
  122. URLs with https scheme are accessed in exactly the same way as with
  123. http scheme, provided that an SSL interface module for LWP has been
  124. properly installed (see the F<README.SSL> file found in the
  125. libwww-perl distribution for more details). If no SSL interface is
  126. installed for LWP to use, then you will get "501 Protocol scheme
  127. 'https' is not supported" errors when accessing such URLs.
  128. Here's an example of fetching and printing a WWW page using SSL:
  129. use LWP::UserAgent;
  130. my $ua = LWP::UserAgent->new;
  131. my $req = HTTP::Request->new(GET => 'https://www.helsinki.fi/');
  132. my $res = $ua->request($req);
  133. if ($res->is_success) {
  134. print $res->as_string;
  135. } else {
  136. print "Failed: ", $res->status_line, "\n";
  137. }
  138. =head1 MIRRORING
  139. If you want to mirror documents from a WWW server, then try to run
  140. code similar to this at regular intervals:
  141. use LWP::Simple;
  142. %mirrors = (
  143. 'http://www.sn.no/' => 'sn.html',
  144. 'http://www.perl.com/' => 'perl.html',
  145. 'http://www.sn.no/libwww-perl/' => 'lwp.html',
  146. 'gopher://gopher.sn.no/' => 'gopher.html',
  147. );
  148. while (($url, $localfile) = each(%mirrors)) {
  149. mirror($url, $localfile);
  150. }
  151. Or, as a perl one-liner:
  152. perl -MLWP::Simple -e 'mirror("http://www.perl.com/", "perl.html")';
  153. The document will not be transfered unless it has been updated.
  154. =head1 LARGE DOCUMENTS
  155. If the document you want to fetch is too large to be kept in memory,
  156. then you have two alternatives. You can instruct the library to write
  157. the document content to a file (second $ua->request() argument is a file
  158. name):
  159. use LWP::UserAgent;
  160. $ua = new LWP::UserAgent;
  161. my $req = new HTTP::Request 'GET',
  162. 'http://www.sn.no/~aas/perl/www/libwww-perl-5.00.tar.gz';
  163. $res = $ua->request($req, "libwww-perl.tar.gz");
  164. if ($res->is_success) {
  165. print "ok\n";
  166. }
  167. Or you can process the document as it arrives (second $ua->request()
  168. argument is a code reference):
  169. use LWP::UserAgent;
  170. $ua = new LWP::UserAgent;
  171. $URL = 'ftp://ftp.unit.no/pub/rfc/rfc-index.txt';
  172. my $expected_length;
  173. my $bytes_received = 0;
  174. $ua->request(HTTP::Request->new('GET', $URL),
  175. sub {
  176. my($chunk, $res) = @_;
  177. $bytes_received += length($chunk);
  178. unless (defined $expected_length) {
  179. $expected_length = $res->content_length || 0;
  180. }
  181. if ($expected_length) {
  182. printf STDERR "%d%% - ",
  183. 100 * $bytes_received / $expected_length;
  184. }
  185. print STDERR "$bytes_received bytes received\n";
  186. # XXX Should really do something with the chunk itself
  187. # print $chunk;
  188. });
  189. =head1 COPYRIGHT
  190. Copyright 1996-1999, Gisle Aas
  191. This library is free software; you can redistribute it and/or
  192. modify it under the same terms as Perl itself.