Leaked source code of windows server 2003
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.

308 lines
9.3 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 = LWP::UserAgent->new;
  30. $ua->agent("$0/0.1 " . $ua->agent);
  31. # $ua->agent("Mozilla/8.0") # pretend we are very capable browser
  32. $req = HTTP::Request->new(GET => 'http://www.linpro.no/lwp');
  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 = LWP::UserAgent->new;
  63. my $req = HTTP::Request->new(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 = LWP::UserAgent->new;
  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 = LWP::UserAgent->new;
  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 = LWP::UserAgent->new;
  103. $ua->proxy(['http', 'ftp'] => 'http://proxy.myorg.com');
  104. $req = HTTP::Request->new('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 = LWP::UserAgent->new;
  115. $req = HTTP::Request->new(GET => 'http://www.linpro.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 COOKIES
  122. Some sites like to play games with cookies. By default LWP ignores
  123. cookies provided by the servers it visits. LWP will collect cookies
  124. and respond to cookie requests if you set up a cookie jar.
  125. use LWP::UserAgent;
  126. use HTTP::Cookies;
  127. $ua = LWP::UserAgent->new;
  128. $ua->cookie_jar(HTTP::Cookies->new(file => "lwpcookies.txt",
  129. autosave => 1));
  130. # and then send requests just as you used to do
  131. $res = $ua->request(HTTP::Request->new(GET => "http://www.yahoo.no"));
  132. print $res->status_line, "\n";
  133. As you visit sites that send you cookies to keep, then the file
  134. F<lwpcookies.txt"> will grow.
  135. =head1 HTTPS
  136. URLs with https scheme are accessed in exactly the same way as with
  137. http scheme, provided that an SSL interface module for LWP has been
  138. properly installed (see the F<README.SSL> file found in the
  139. libwww-perl distribution for more details). If no SSL interface is
  140. installed for LWP to use, then you will get "501 Protocol scheme
  141. 'https' is not supported" errors when accessing such URLs.
  142. Here's an example of fetching and printing a WWW page using SSL:
  143. use LWP::UserAgent;
  144. my $ua = LWP::UserAgent->new;
  145. my $req = HTTP::Request->new(GET => 'https://www.helsinki.fi/');
  146. my $res = $ua->request($req);
  147. if ($res->is_success) {
  148. print $res->as_string;
  149. } else {
  150. print "Failed: ", $res->status_line, "\n";
  151. }
  152. =head1 MIRRORING
  153. If you want to mirror documents from a WWW server, then try to run
  154. code similar to this at regular intervals:
  155. use LWP::Simple;
  156. %mirrors = (
  157. 'http://www.sn.no/' => 'sn.html',
  158. 'http://www.perl.com/' => 'perl.html',
  159. 'http://www.sn.no/libwww-perl/' => 'lwp.html',
  160. 'gopher://gopher.sn.no/' => 'gopher.html',
  161. );
  162. while (($url, $localfile) = each(%mirrors)) {
  163. mirror($url, $localfile);
  164. }
  165. Or, as a perl one-liner:
  166. perl -MLWP::Simple -e 'mirror("http://www.perl.com/", "perl.html")';
  167. The document will not be transfered unless it has been updated.
  168. =head1 LARGE DOCUMENTS
  169. If the document you want to fetch is too large to be kept in memory,
  170. then you have two alternatives. You can instruct the library to write
  171. the document content to a file (second $ua->request() argument is a file
  172. name):
  173. use LWP::UserAgent;
  174. $ua = LWP::UserAgent->new;
  175. my $req = HTTP::Request->new(GET =>
  176. 'http://www.linpro.no/lwp/libwww-perl-5.46.tar.gz');
  177. $res = $ua->request($req, "libwww-perl.tar.gz");
  178. if ($res->is_success) {
  179. print "ok\n";
  180. }
  181. else {
  182. print $res->status_line, "\n";
  183. }
  184. Or you can process the document as it arrives (second $ua->request()
  185. argument is a code reference):
  186. use LWP::UserAgent;
  187. $ua = LWP::UserAgent->new;
  188. $URL = 'ftp://ftp.unit.no/pub/rfc/rfc-index.txt';
  189. my $expected_length;
  190. my $bytes_received = 0;
  191. my $res =
  192. $ua->request(HTTP::Request->new(GET => $URL),
  193. sub {
  194. my($chunk, $res) = @_;
  195. $bytes_received += length($chunk);
  196. unless (defined $expected_length) {
  197. $expected_length = $res->content_length || 0;
  198. }
  199. if ($expected_length) {
  200. printf STDERR "%d%% - ",
  201. 100 * $bytes_received / $expected_length;
  202. }
  203. print STDERR "$bytes_received bytes received\n";
  204. # XXX Should really do something with the chunk itself
  205. # print $chunk;
  206. });
  207. print $res->status_line, "\n";
  208. =head1 COPYRIGHT
  209. Copyright 1996-2001, Gisle Aas
  210. This library is free software; you can redistribute it and/or
  211. modify it under the same terms as Perl itself.