⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch22_07.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<html><head><title>Win32::Internet (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch22_06.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch22_08.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">22.7. Win32::Internet</h2><p><a name="INDEX-3237" />The Win32::Internet extensionimplements the Win32 Internet APIs (found in<em class="emphasis">WININET.DLL</em>), providing support for HTTP, FTP,and Gopher connections.</p><p>All types of connections start as a basic Internet connection thatmust be opened with the following command<a name="INDEX-3238" />:</p><blockquote><pre class="code">use Win32::Internet;$Connection = Win32::Internet-&gt;new( );</pre></blockquote><p>This creates an Internet object in Perl on which you use thefunctions provided in this module to create more specific connectionobjects. The objects and functions that create them are:</p><ul><li><p>Internet connections (the main object, with <tt class="literal">new</tt>)</p></li><li><p>URLs (with <tt class="literal">OpenURL</tt>)</p></li><li><p>FTP sessions (with <tt class="literal">FTP</tt>)</p></li><li><p>HTTP sessions (with <tt class="literal">HTTP</tt>)</p></li><li><p>HTTP requests (with <tt class="literal">OpenRequest</tt>)</p></li></ul><p>This module provides different levels of implementation of the Win32Internet functions. Some routines use several Win32 API functions toperform a complex task in a single call; they are simpler to use, butless powerful. Other functions implement nothing more and nothingless than the corresponding API function, so you can use all of theirpower, but with some additional programming steps.</p><p>For example, the function <tt class="literal">FetchURL</tt> fetches thecontents of any HTTP, FTP, or Gopher URL with a simple command:</p><blockquote><pre class="code">$inet = new Win32::Internet( );$file = $inet-&gt;FetchURL("http://www.yahoo.com");</pre></blockquote><p>You can achieve the same result with this series of commands, whichis what <tt class="literal">FetchURL</tt> actually does:</p><blockquote><pre class="code">$inet = new Win32::Internet( );$url = $inet-&gt;OpenURL("http://www.yahoo.com");$file = $url-&gt;ReadFile( );$url-&gt;Close( );</pre></blockquote><a name="perlnut2-CHP-22-SECT-7.1" /><div class="sect2"><h3 class="sect2">22.7.1. General Internet Methods</h3><p>The methods described in this section are used on Internet<a name="INDEX-3239" />connection objects created with<tt class="literal">new</tt>:</p><blockquote><pre class="code">$inet = Win32::Internet-&gt;new( );</pre></blockquote><p>You can supply <tt class="literal">new</tt> with an optional list ofarguments (or a reference to a hash containing them) that looks likethis:</p><blockquote><pre class="code">Win32::Internet-&gt;new [<em class="replaceable"><tt>useragent</em>, <em class="replaceable">opentype</em>, <em class="replaceable">proxy</em>, <em class="replaceable">proxybypass</em>, <em class="replaceable">flags</tt></em>] Win32::Internet-&gt;new [<em class="replaceable"><tt>$hashref</tt></em>]</pre></blockquote><p>The parameters and their values are:</p><dl><dt><i><em class="replaceable"><tt>useragent</tt></em></i></dt><dd>The user agent string passed to HTTP requests. Default is<tt class="literal">Perl-Win32Internet/version</tt>.</p></dd><dt><i><em class="replaceable"><tt>opentype</tt></em></i></dt><dd>How to access the Internet (e.g., directly or using a proxy). Defaultis <tt class="literal">INTERNET_OPEN_TYPE_DIRECT</tt>.</p></dd><dt><i><em class="replaceable"><tt>proxy</tt></em></i></dt><dd>Name of the proxy server (or servers) to use. Default is none.</p></dd><dt><i><em class="replaceable"><tt>proxybypass</tt></em></i></dt><dd>Optional list of hostnames or IP addresses that are known locally.Default is none.</p></dd><dt><i><em class="replaceable"><tt>flags</tt></em></i></dt><dd>Additional flags affecting the behavior of the function. Default isnone.</p></dd></dl><p>If you pass a hash reference to the function, the following valuesare taken from the hash:</p><blockquote><pre class="code">%hash=(  "useragent"   =&gt; "useragent",  "opentype"    =&gt; "opentype",  "proxy"       =&gt; "proxy",  "proxybypass" =&gt; "proxybypass",  "flags"       =&gt; flags,);</pre></blockquote><p>The following methods can be used on Internet connection objects.</p><a name="INDEX-3240" /><a name="INDEX-3241" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>CanonicalizeURL</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">inet</em>-&gt;CanonicalizeURL(<em class="replaceable">URL</em>, [<em class="replaceable">flags</em>])</pre><p><a name="INDEX-3240" /><a name="INDEX-3241" />Converts a URL to acanonical format, which includes converting unsafe characters toescape sequences. Returns the canonicalized URL, or<tt class="literal">undef</tt> on error. For the possible values of<em class="replaceable"><tt>flags</tt></em>, refer to the Microsoft Win32Internet Functions documentation.</p></div><a name="INDEX-3242" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>Close</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">inet</em>-&gt;Close([$<em class="replaceable">obj</em>])</pre><p><a name="INDEX-3242" />Closes an Internet connection. This canbe applied to any Win32::Internet object. Note that it is notstrictly required to close the connections you create, since theWin32::Internet objects are automatically closed when the programends (or when such an object is destroyed by other means).</p></div><a name="INDEX-3243" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>CombineURL</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">inet</em>-&gt;CombineURL(<em class="replaceable">baseURL</em>, <em class="replaceable">relativeURL</em>, [<em class="replaceable">flags</em>])</pre><p><a name="INDEX-3243" />Combines a base and relativeURL into a single URL. Returns the (canonicalized) combined URL, or<tt class="literal">undef</tt> on error. For the possible values of flags,refer to the Microsoft Win32 Internet Functions documentation.</p></div><a name="INDEX-3244" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>ConnectBackoff</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">inet</em>-&gt;ConnectBackoff([<em class="replaceable">value</em>])</pre><p><a name="INDEX-3244" />Reads or sets the delayvalue, in milliseconds, to wait between connection retries. If novalue parameter is specified, the current value is returned;otherwise, the delay between retries is set.</p></div><a name="INDEX-3245" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>ConnectionRetries</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">inet</em>-&gt;ConnectRetries([<em class="replaceable">value</em>])</pre><p><a name="INDEX-3245" />Reads or sets the number oftimes a connection is retried before considering it failed. If novalue parameter is specified, the current value is returned;otherwise, the number of retries is set. The default value is<tt class="literal">5</tt>.</p></div><a name="INDEX-3246" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>ConnectTimeout</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">inet</em>-&gt;ConnectTimeout([<em class="replaceable">value</em>])</pre><p><a name="INDEX-3246" />Reads or sets the timeoutvalue (in milliseconds) before a connection is considered failed. Ifno value parameter is specified, the current value is returned;otherwise, the timeout is set to <em class="replaceable"><tt>value</tt></em>.The default value is infinity.</p></div><a name="INDEX-3247" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>ControlReceiveTimeout</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">inet</em>-&gt;ControlReceiveTimeout([<em class="replaceable">value</em>])</pre><p><a name="INDEX-3247" />Reads or sets the timeoutvalue (in milliseconds) to use for non-data (control) receiverequests before they are canceled. Currently, this value has meaningonly for FTP sessions. If no value parameter is specified, thecurrent value is returned; otherwise, the timeout is set. The defaultvalue is infinity.</p></div><a name="INDEX-3248" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>ControlSendTimeout</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">inet</em>-&gt;ControlSendTimeout([<em class="replaceable">value</em>])</pre><p><a name="INDEX-3248" />Reads or sets the timeoutvalue (in milliseconds) to use for non-data (control) send requestsbefore they are canceled. Currently, this value has meaning only forFTP sessions. If no value parameter is specified, the current valueis returned; otherwise, the timeout is set. The default value isinfinity.</p></div><a name="INDEX-3249" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>CrackURL</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">inet</em>-&gt;CrackURL(<em class="replaceable">URL</em>, [<em class="replaceable">flags</em>])</pre><p><a name="INDEX-3249" />Splits a URL into itscomponent parts and returns them in an array. Returns<tt class="literal">undef</tt> on error. The array will contain thefollowing values: (<em class="replaceable"><tt>scheme</tt></em>,<em class="replaceable"><tt>host</em>, <em class="replaceable">port</tt></em>,<em class="replaceable"><tt>username</tt></em>,<em class="replaceable"><tt>password</em>, <em class="replaceable">path</tt></em>,<em class="replaceable"><tt>extrainfo</tt></em>). For example, the URL<em class="emphasis">http://www.divinf.it/index.html#top</em> can besplit into:</p><blockquote><pre class="code">http, www.divinf.it, 80, anonymous, dada@divinf.it, /index.html, #top</pre></blockquote><p>If you don't specify a flags parameter,<tt class="literal">ICU_ESCAPE</tt> will be used by default; for thepossible values of <em class="replaceable"><tt>flags</tt></em>, refer to theMicrosoft Win32 Internet Functions documentation.</p></div><a name="INDEX-3250" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>CreateURL</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><table width="515" border="0" cellpadding="5"><tr><td align="left"><pre>$<em class="replaceable">inet</em>-&gt;CreateURL(<em class="replaceable">scheme</em>, <em class="replaceable">hostname</em>, <em class="replaceable">port</em>, <em class="replaceable">username</em>, <em class="replaceable">password</em>, <em class="replaceable">path</em>, <em class="replaceable">extrainfo</em>, [<em class="replaceable">flags</em>])$<em class="replaceable">inet</em>-&gt;CreateURL($<em class="replaceable">hashref</em>, [<em class="replaceable">flags</em>]) </pre></td><td align="right" /></tr></table><p><p><a name="INDEX-3250" />Creates a URL from itscomponent parts. Returns <tt class="literal">undef</tt> on error and thecreated URL if successful. If you pass a hash reference, thefollowing values are taken from the array:</p><blockquote><pre class="code">%hash=(  "scheme"    =&gt; "scheme",  "hostname"  =&gt; "hostname",  "port"      =&gt; port,  "username"  =&gt; "username",

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -