You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

httptasks.html 18 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. <html>
  2. <head>
  3. <title>Http Tasks</title>
  4. </head>
  5. <body>
  6. <h2>Http Tasks</h2>
  7. Tasks do to make the basic HTTP requests: get, post, head, put, with authentication.
  8. There is also a task to configure the proxy settings of the http tasks.
  9. <p>
  10. These tasks significantly extend the basic <a
  11. href="../CoreTasks/get.html">get task</a>, but are split off into the optional section
  12. so that
  13. <ol>
  14. <li> The core ant file doesn't get so big
  15. <li> this implementation can move to using an optional jar (HttpClient) to work around
  16. limitations of the HTTP support built in to the Java platform.
  17. </ol>
  18. <h3>Core Functionality and Parameters</h3>
  19. <p>Common functionality to the core tasks tasks is </p>
  20. <ol>
  21. <li>Ability to name the remote url which the target of the request.</li>
  22. <li>Ability to name a local file as the local store of any returned
  23. content.</li>
  24. <li>Ability to name a property as the local store of any returned
  25. content.</li>
  26. <li>Ability to name a property to be set to &quot;true&quot; when a
  27. request succeeds.</li>
  28. <li>The option to list a number of parameters, each with a name and a
  29. value. Some methods (HttpGet, HttpHead) attach these parameters to the
  30. stated url to generate the url to actually fetch. Others (HttpPost) send
  31. the parameters up in the standard representation of form data.</li>
  32. <li>The option to state the authentication policy and then the username
  33. and password. Currently only basic authentication is used, which is
  34. utterly insecure except over an https link</li>
  35. <li>A 'verbose' option which provides extra information and progess
  36. information during a download.</li>
  37. <li>Timestamp control, using the <i>usetimestamp</i> flag. When set the
  38. timestamp of downloaded content is set to match that of the remote file
  39. (Java 1.2 or later only), and the local timestamp of the destination
  40. file (if it exists) used to set the if-modified-since header in the
  41. request, which will trigger optional download only. </li>
  42. </ol>
  43. <h3>Parameters</h3>
  44. <p> The <i>url</i> parameter specifies the URL to access. The optional
  45. <i>dest</i> parameter specifies a destination to which the retrieved
  46. page will be written. The optional <i>destinationproperty </i>parameter
  47. specifies a name of a property to save the content to, instead of a
  48. property. If neither <i>dest</i> nor <i>destinationproperty</i>
  49. specified, the contents of the specified URL are discarded (this is
  50. useful when accessing the URL for the purpose of causing some action on
  51. the remote server).</p>
  52. <p> When the <i>verbose</i> option is enabled, the task displays a '.' for every
  53. 64 KB retrieved. If the <i>blocksize</i> parameter is adjusted then files are
  54. uploaded or downloaded in a different size block from this, and progress markers
  55. appear appropriately. </p>
  56. The <i>usetimestamp</i> option enables you to control downloads so that
  57. the remote file is only fetched if newer than the local copy. If there
  58. is no local copy, the download always takes place. When a file is
  59. downloaded, the timestamp of the downloaded file is set to the remote
  60. timestamp, if the JVM is Java1.2 or later. NB: This timestamp facility
  61. only works on downloads using the HTTP protocol.
  62. <p>The <i>authtype</i>, <i>username</i>, and <i>password</i> options enable support
  63. for password protected pages. Currently only 'Basic' authentication is used,
  64. which is notoriously insecure except over an encrypted https channel.</p>
  65. <table border="1" cellpadding="2" cellspacing="0">
  66. <tr>
  67. <td valign="top"><b>Attribute</b></td>
  68. <td valign="top"><b>Description</b></td>
  69. <td align="center" valign="top"><b>Required</b></td>
  70. </tr>
  71. <tr>
  72. <td valign="top">authtype</td>
  73. <td valign="top">the HTTP authentication protocol to use, <i>none</i> or <i>basic</i>.</td>
  74. <td align="center" valign="top">No</td>
  75. </tr>
  76. <tr>
  77. <td valign="top">blocksize</td>
  78. <td valign="top">size (in kilobytes) of the data block used for upload and
  79. download. Default: 64.<br>
  80. Keep this to a multiple of the hard disk sector size for file IO performance.</td>
  81. <td align="center" valign="top">No</td>
  82. </tr>
  83. <tr>
  84. <td valign="top"><b>dest</b></td>
  85. <td valign="top">the file where to store the retrieved file.</td>
  86. <td align="center" valign="top">No</td>
  87. </tr>
  88. <tr>
  89. <td valign="top">destinationProperty</td>
  90. <td valign="top">the name of a property to fill with the returned content.
  91. Ignored if <i>dest</i> is set</td>
  92. <td align="center" valign="top">No</td>
  93. </tr>
  94. <tr>
  95. <td valign="top">failonerror</td>
  96. <td valign="top">stop the build if the request failed. default: true</td>
  97. <td align="center" valign="top">No</td>
  98. </tr>
  99. <tr>
  100. <td valign="top">password</td>
  101. <td valign="top">the password for authentication.</td>
  102. <td align="center" valign="top">No</td>
  103. </tr>
  104. <tr>
  105. <td valign="top">successProperty</td>
  106. <td valign="top">the name of a property to set to &quot;true&quot; if the
  107. request succeeds.<br>
  108. Set <i>failonerror</i> to false for this to be of use.</td>
  109. <td align="center" valign="top">No</td>
  110. </tr>
  111. <tr>
  112. <td valign="top"><b>url</b></td>
  113. <td valign="top">the URL from which to retrieve a file.</td>
  114. <td align="center" valign="top">Yes</td>
  115. </tr>
  116. <tr>
  117. <td valign="top">usecaches</td>
  118. <td valign="top">boolean to enable 'caching' of content during the fetch process.
  119. default:false</td>
  120. <td align="center" valign="top">No</td>
  121. </tr>
  122. <tr>
  123. <td valign="top">useresponsecode</td>
  124. <td valign="top">boolean to enable success/failure to be determined by result
  125. of the received response code. HTTP only. default=true.</td>
  126. <td align="center" valign="top">&nbsp;</td>
  127. </tr>
  128. <tr>
  129. <td valign="top">username</td>
  130. <td valign="top">the user name for authentication.</td>
  131. <td align="center" valign="top">No</td>
  132. </tr>
  133. <tr>
  134. <td valign="top">usetimestamp</td>
  135. <td valign="top">boolean flag to conditionally download a file based on the
  136. timestamp of the local copy. HTTP only</td>
  137. <td align="center" valign="top">No</td>
  138. </tr>
  139. <tr>
  140. <td valign="top">verbose</td>
  141. <td valign="top"> boolean flag to control progress information display.</td>
  142. <td align="center" valign="top">No</td>
  143. </tr>
  144. </table>
  145. <p> The <i>successProperty</i> names a property which will be set to "true" if
  146. the request was deemed successful. For any non-http protocol, success is defined
  147. as the request completing. For http and https, success is defined as the request
  148. completing, and the response code from the serving being one of the 'success'
  149. values -any number between 200 and 299 inclusive. The usual HTTP_OK (200) is
  150. therefore a success, as is HTTP_ACCEPTED (202). But failures such as BAD_REQUEST
  151. (400) and the ubiquitous HTTP_NOT_FOUND (404) are most definately errors. So
  152. an attempt to access a missing url may result 'failure',even though some content
  153. was download (such as, perhaps, the 'file not found' text). If this is not what
  154. you desire, then set <i>useresponsecode</i>=&quot;false&quot; for the system
  155. to interpret any data back as a success.
  156. <h3>Parameters specified as nested elements</h3>
  157. <p><b>param</b></p>
  158. <p>Specifies an HTTP request parameter to send as part of the request.
  159. For <i>get</i> and <i>head</i> request methods the parameters are
  160. encoded as part of the URL. For <i>post</i> request methods, the
  161. parameters are sent as the POST request data.</p>
  162. <table cellspacing="0" cellpadding="2" border="1">
  163. <tbody>
  164. <tr>
  165. <td valign="top"><b>Attribute</b></td>
  166. <td valign="top"><b>Description</b></td>
  167. <td valign="top" align="middle"><b>Required</b></td>
  168. </tr>
  169. <tr>
  170. <td valign="top">name</td>
  171. <td valign="top">the name of the request property to set.</td>
  172. <td valign="top" align="middle">Yes</td>
  173. </tr>
  174. <tr>
  175. <td valign="top">value</td>
  176. <td valign="top">the value of the request property. You may alternatively
  177. specify the value as text between the beginning and ending param tags.</td>
  178. <td valign="center" align="middle">Yes</td>
  179. </tr>
  180. </tbody>
  181. </table>
  182. <p><b>header</b></p>
  183. <p>Specifies an arbitrary HTTP request header that will be sent with the
  184. request.</p>
  185. <table cellspacing="0" cellpadding="2" border="1">
  186. <tr>
  187. <td valign="top"><b>Attribute</b></td>
  188. <td valign="top"><b>Description</b></td>
  189. <td valign="top" align="middle"><b>Required</b></td>
  190. </tr>
  191. <tr>
  192. <td valign="top">name</td>
  193. <td valign="top">the name of the HTTP request header</td>
  194. <td valign="top" align="middle">Yes</td>
  195. </tr>
  196. <tr>
  197. <td valign="top">value</td>
  198. <td valign="top">the value of the HTTP request header. You may alternatively
  199. specify the value as text between the beginning and ending header tags.</td>
  200. <td valign="center" align="middle">Yes</td>
  201. </tr>
  202. </table>
  203. <h3>Quirky Limitations of java.net classes</h3>
  204. Multiple HTTP headers can with the same name can <b>not</b> be set, even
  205. though the protocol permits it. Java1.1 and Java 1.2 <i>may</i> permit multiple
  206. cookies to be set, but this behaviour is explicitly not supported on Java1.3,
  207. as someone went and change the code to stop this (Java bug ID #4242254).
  208. You need to set multiple cookies in one go and hope the far end can handle it
  209. <p>
  210. Bug ID #4160499 covers another issue, to wit: some versions of Java throw
  211. exceptions when an error code is greater than 400 and the dest file isn't
  212. one of a few simple file types, but Java 1.2 and 1.3 do not. So there
  213. is no way to get the error text when a jsp page throws some exception.
  214. <p>
  215. Also, although this isnt going to be filed until we have a short
  216. test case, but if you recieve a short response with less content than
  217. the content-length header promises, the library seems to silently
  218. reduce the content length header to match, which seems the wrong action.
  219. <h2><a name="httpget">HttpGet</a></h2>
  220. <h3>Description</h3>
  221. <p>Accesses a URL to retrieve a file or to cause some action on the server.</p>
  222. <p> This task should be preferred above the <a href="#cvs">CVS task</a> when doing
  223. automated builds. CVS is significantly slower than loading a compressed archive
  224. with http/ftp. This task will also retrieve content using other supported protocols,
  225. such as ftp: and file:
  226. <p>All the attributes of httptask may be used. Note that a quirk of the implementation
  227. of the http client in java makes it impossible to reliably fetch the response
  228. details from any unsuccessful request against a URL which doesn't end in '.htm,.html
  229. or .txt'. This means that if the task is used to compile jsp pages by issuing
  230. request against them, the text details of any errors will not be picked up.
  231. <h3>Examples</h3>
  232. <pre> &lt;httpget url=&quot;http://jakarta.apache.org/&quot; dest=&quot;help/index.html&quot;/&gt;</pre>
  233. <p>Fetches the index page of http://jakarta.apache.org/, and stores it in the
  234. file <code>help/index.html</code>. </p>
  235. <pre> &lt;httpget src=&quot;http://jakarta.apache.org/builds/tomcat/nightly/ant.zip&quot;
  236. dest=&quot;optional.jar&quot;
  237. verbose=&quot;true&quot;
  238. usetimestamp=&quot;true&quot;
  239. &gt;
  240. &lt;header name=&quot;Cookie&quot; value=&quot;someid=43ff2b&quot;/&gt;
  241. &lt;/httpget&gt;</pre>
  242. <p> Retrieves the nightly ant build from the tomcat distribution, if the local
  243. copy is missing or out of date. Uses the verbose option for progress information.
  244. A cookie is supplied for the server's benefit.</p>
  245. <pre> &lt;httpget url="https://www.pizzaservices.com/prices.jsp"
  246. dest="pizza-prices.xml&quot;&gt;
  247. &lt;param name=&quot;zipcode&quot;&gt;57340&lt;/param&gt;
  248. &lt;param name=&quot;pizza&quot;&gt;pepperoni&lt;/param&gt;
  249. &lt;/httpget&gt;</pre>
  250. <p>Builds a URL by adding parameters (&quot;?zipcode=57340&amp;pizza=pepperoni&quot;)
  251. to the base URL and then fetches the contents (fictional example)</p>
  252. <h2><a name="httphead">HttpHead</a> </h2>
  253. <p>The http HEAD request is similar to the normal GET request , except it, by
  254. definition, returns no content, just a success code and http headers. Accordingly,
  255. the destination properties of the base class -<i>dest</i> and -, <i>destinationpropertyname</i>)
  256. are not supported -any attempt to use them will result in a build failure. Note
  257. also that the http and https protocols are the only protocols supported. </p>
  258. <p>
  259. HttpHead is useful for triggering server side actions, but note that many servers
  260. interpret a HEAD very differently from a GET. An HttpGet request with the
  261. return data discarded is often a more reliable approach.
  262. </p>
  263. <p> Where head can be useful is in testing for the availability and reachability
  264. of servers, such as in the following test for apache being reachable, which
  265. sets a variable on success:-
  266. <pre>
  267. &lt;httphead url="http://www.apache.org/"
  268. failonerror="false"
  269. successproperty="reachable.apache"
  270. /&gt;
  271. </pre>
  272. <p>Note that sometimes a missing file on a mis-configured server still generates
  273. a successful '200' response to a GET request -and returns a 'missing' file page,
  274. but a HEAD request will reliably pick up the 'missing file' error. </p>
  275. <h2><a name="httppost">HttpPost</a></h2>
  276. <p>This implements the POST request. Supplied parameter data is turned into form
  277. data and sent as the body of the request, rather than appended to the URL. If
  278. a file to upload is specified instead, using <i>uploadFile</i>, the parameter
  279. values are ignored. Instead the content type of the file is sent in the header
  280. -based on the <i>contentType</i> attribute or what the java runtime thinks the
  281. content type is based on the file extension. The file is uploaded. </p>
  282. <p>Like HttpGet, this command can return a content which can downloaded to a file,
  283. to a property, or just ignored.</p>
  284. <p>This task adds two new attributes to the base set. </p>
  285. <table cellspacing="0" cellpadding="2" border="1">
  286. <tr>
  287. <td valign="top" width="78"><b>Attribute</b></td>
  288. <td valign="top" width="559"><b>Description</b></td>
  289. <td valign="top" align="middle" width="62"><b>Required</b></td>
  290. </tr>
  291. <tr>
  292. <td valign="top" width="78">uploadFile</td>
  293. <td valign="top" width="559">a file to upload. when specified, all parameters
  294. are ignored.</td>
  295. <td valign="top" align="middle" width="62">
  296. <div align="center">no</div>
  297. </td>
  298. </tr>
  299. <tr>
  300. <td valign="top" width="78">contentType </td>
  301. <td valign="top" width="559">the type of the content (text/html, text/xml,
  302. application/binary, etc). Only of relevance when a file is being uploaded,
  303. and still optional in that case. </td>
  304. <td valign="center" align="middle" width="62">
  305. <div align="center">no</div>
  306. </td>
  307. </tr>
  308. </table>
  309. <h3></h3>
  310. <pre>
  311. &lt;httppost url=&quot;http://www.example.com/servlet/docserver&quot;
  312. authtype=&quot;basic&quot; username=&quot;joe&quot; password=&quot;silly&quot;&gt;
  313. &lt;param name=&quot;action&quot; value=&quot;getdoc&quot;/&gt;
  314. &lt;param name=&quot;ISBN&quot;&gt;038550120X&lt;/param&gt;
  315. &lt;param name=&quot;pages&quot;&gt;19-20&lt;/param&gt;
  316. &lt;header name=&quot;Accept-Language&quot; value=&quot;en-us&quot;/&gt;
  317. &lt;/httppost&gt;</pre>
  318. <p>Accesses a server at www.foo.com, passing a request to some servlet asking it
  319. to retrieve several pages from a stored book. An HTTP header specifying
  320. acceptable languages for the returned contents is also sent. Basic
  321. authentication is used with a user name of &quot;joe&quot; and a password of
  322. &quot;silly&quot;.</p>
  323. <p>
  324. <pre> &lt;httppost url="https://www.pizzaservices.com"
  325. uploadFile="pizza-order.xml"
  326. contentType="text/xml"&gt;
  327. </pre>
  328. <p>Sends a pre-prepared order for a pizza to a pizza vendor accepting orders using
  329. xml-rpc requests. (NB: fictional example) </p>
  330. <h2><a name="SetProxy"></a>SetProxy</h2>
  331. <p>This task configures the proxy settings for all http tasks which follow it
  332. in the build. That includes the original Get task, but not the telnet and FTP
  333. tasks. The proxy settings remain in place until changed or the build finishes,
  334. and will also hold for other ant build files invoked and even non-forked java
  335. invocations, and even URL resolutions of XML parsers running in the same JVM
  336. </p>
  337. <table cellspacing="0" cellpadding="2" border="1">
  338. <tr>
  339. <td valign="top" width="78"><b>Attribute</b></td>
  340. <td valign="top" width="559"><b>Description</b></td>
  341. <td valign="top" align="middle" width="62"><b>Required</b></td>
  342. </tr>
  343. <tr>
  344. <td valign="top" width="78">proxyHost</td>
  345. <td valign="top" width="559">hostname of a web/ftp proxy server</td>
  346. <td valign="top" align="middle" width="62">
  347. <div align="center">no</div>
  348. </td>
  349. </tr>
  350. <tr>
  351. <td valign="top" width="78">proxyPort </td>
  352. <td valign="top" width="559">integer; the port of the proxy server</td>
  353. <td valign="center" align="middle" width="62">
  354. <div align="center">no</div>
  355. </td>
  356. </tr>
  357. <tr>
  358. <td valign="top" width="78">socksProxyHost</td>
  359. <td valign="top" width="559">hostname of a SOCKS4 proxy server</td>
  360. <td valign="center" align="middle" width="62">
  361. <div align="center">no</div>
  362. </td>
  363. </tr>
  364. <tr>
  365. <td valign="top" width="78">socksProxyPort</td>
  366. <td valign="top" width="559">integer; port number of a SOCKS4 server (default=1080)</td>
  367. <td valign="center" align="middle" width="62">
  368. <div align="center">no</div>
  369. </td>
  370. </tr>
  371. </table>
  372. <h3></h3>
  373. <p>Turn off all proxies</p>
  374. <pre> &lt;setproxy proxyhost=&quot;&quot; socksProxyHost=&quot;&quot; /&gt;</pre>
  375. <p>Set web proxy to 'web-proxy:80'; do not make any changes to existing socks
  376. settings (if any)</p>
  377. <pre> &lt;setproxy proxyHost=&quot;web-proxy&quot; proxyPort=&quot;80&quot;/&gt;</pre>
  378. <p>Turn on socks</p>
  379. <pre> &lt;setproxy socksProxyHost=&quot;socks-server&quot; socksProxyPort=&quot;1080&quot;/&gt;</pre>
  380. <p>Do nothing</p>
  381. <pre> &lt;setproxy/&gt;</pre>
  382. <hr>
  383. <p align="center">Copyright &copy; 2000,2001 Apache Software Foundation. All rights
  384. Reserved.</p>
  385. </body>
  386. </html>