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

📄 ch20_03.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 20.2. Automating Form Submission (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:45:55Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch20_01.htm"TITLE="20. Web Automation"><LINKREL="prev"HREF="ch20_02.htm"TITLE="20.1. Fetching a URL from a Perl Script"><LINKREL="next"HREF="ch20_04.htm"TITLE="20.3. Extracting URLs"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch20_02.htm"TITLE="20.1. Fetching a URL from a Perl Script"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 20.1. Fetching a URL from a Perl Script"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch20_01.htm"TITLE="20. Web Automation"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch20_04.htm"TITLE="20.3. Extracting URLs"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 20.3. Extracting URLs"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch20-41131">20.2. Automating Form Submission</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch20-pgfId-211">Problem<ACLASS="indexterm"NAME="ch20-idx-1000002591-0"></A><ACLASS="indexterm"NAME="ch20-idx-1000002591-1"></A><ACLASS="indexterm"NAME="ch20-idx-1000002591-2"></A><ACLASS="indexterm"NAME="ch20-idx-1000002591-3"></A></A></H3><PCLASS="para">You want to submit form values to a CGI script from your program.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch20-pgfId-217">Solution</A></H3><PCLASS="para">If you're submitting form values using the GET method, create a URL and encode the form using the <CODECLASS="literal">query_form</CODE><ACLASS="indexterm"NAME="ch20-idx-1000002601-0"></A> method:</P><PRECLASS="programlisting">use LWP::Simple;use URI::URL;my $url = url('http://www.perl.com/cgi-bin/cpan_mod');$url-&gt;query_form(module =&gt; 'DB_File', readme =&gt; 1);$content = get($url);</PRE><PCLASS="para">If you're using the <ACLASS="indexterm"NAME="ch20-idx-1000003769-0"></A>POST method, create your own user agent and encode the content appropriately:</P><PRECLASS="programlisting">use HTTP::Request::Common qw(POST);use LWP::UserAgent;$ua = LWP::UserAgent-&gt;new();my $req = POST 'http://www.perl.com/cgi-bin/cpan_mod',               [ module =&gt; 'DB_File', readme =&gt; 1 ];$content = $ua-&gt;request($req)-&gt;as_string;</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch20-pgfId-251">Discussion</A></H3><PCLASS="para">For simple operations, the procedural interface of the LWP::Simple module is sufficient. For fancier ones, the LWP::UserAgent module provides a virtual browser object, which you manipulate using method calls.</P><PCLASS="para">The format of a query string is:</P><PRECLASS="programlisting">field1=value1&amp;field2=value2&amp;field3=value3</PRE><PCLASS="para">In GET requests, this is encoded in the URL being requested:</P><PRECLASS="programlisting">http://www.site.com/path/to/script.cgi?field1=value1&amp;field2=value2&amp;field3=value3</PRE><PCLASS="para">Fields must still be properly escaped, so setting the <CODECLASS="literal">arg</CODE> form parameter to <CODECLASS="literal">&quot;this</CODE> <CODECLASS="literal">isn't</CODE> <CODECLASS="literal">&lt;EASY&gt;&amp;&lt;FUN&gt;&quot;</CODE> would yield:</P><PRECLASS="programlisting">http://www.site.com/path/to/script.cgi?arg=%22this+isn%27t+%3CEASY%3E+%26+%3CFUN%3E%22</PRE><PCLASS="para">The <CODECLASS="literal">query_form</CODE> method called on a URL object correctly escapes the form values for you, or you could use the <CODECLASS="literal">URI::Escape::uri_escape</CODE> or <CODECLASS="literal">CGI::escape_html</CODE> functions on your own. In POST requests, the query string is the body of the HTTP document sent to the CGI script.</P><PCLASS="para">We can use the LWP::Simple module to submit data in a GET request, but there is no corresponding LWP::Simple interface for POST requests. Instead, the HTTP::Request::Common module's POST function conveniently creates a properly formatted request with everything properly escaped.</P><PCLASS="para">If you need to go through a proxy, construct your user agent and tell it to use a proxy this way:</P><PRECLASS="programlisting">$ua-&gt;proxy(['http', 'ftp'] =&gt; 'http://proxy.myorg.com:8081');</PRE><PCLASS="para">That says that both HTTP and FTP requests through this user agent should be routed through the proxy on port 8081 at<EMCLASS="emphasis"> </EM><ICLASS="systemitem.example">proxy.myorg.com</I>.<ACLASS="indexterm"NAME="ch20-idx-1000002593-0"></A><ACLASS="indexterm"NAME="ch20-idx-1000002593-1"></A><ACLASS="indexterm"NAME="ch20-idx-1000002593-2"></A><ACLASS="indexterm"NAME="ch20-idx-1000002593-3"></A><ACLASS="indexterm"NAME="ch20-idx-1000002593-4"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch20-pgfId-279">See Also</A></H3><PCLASS="para">The documentation for the CPAN modules LWP::Simple, LWP::UserAgent, HTTP::Request::Common, URI::Escape, and URI::URL; <ACLASS="xref"HREF="ch20_02.htm"TITLE="Fetching a URL from a Perl Script">Recipe 20.1</A></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch20_02.htm"TITLE="20.1. Fetching a URL from a Perl Script"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 20.1. Fetching a URL from a Perl Script"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch20_04.htm"TITLE="20.3. Extracting URLs"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 20.3. Extracting URLs"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">20.1. Fetching a URL from a Perl Script</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">20.3. Extracting URLs</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>

⌨️ 快捷键说明

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