Java Reference
In-Depth Information
This recipe is very similar to Recipe 7.1. The only difference is that this recipe uses HTTP
POST and the previous recipe uses HTTP GET . Only the changes necessary to change to
HTTP POST will be covered here. For information on how this recipe parses the results,
refer to Recipe 7.1.
The process method for the recipe begins by building a URL object for the target
page.
// Build the URL and POST.
URL url = new URL("http://www.httprecipes.com/1/7/post.php");
Next, a URLConnection is opened. The setDoOutput method call specifies
that this will be an HTTP POST . An OutputStream is then opened.
URLConnection http = url.openConnection();
http.setDoOutput(true);
OutputStream os = http.getOutputStream();
The OutputStream object is passed to the FormUtility constructor. The form
data will be output to that stream.
FormUtility form = new FormUtility(os, null);
form.add("search", search);
form.add("type", type);
form.add("action", "Search");
form.complete();
Once all of the data has been sent for the form, a call to the complete method finishes
the transaction. Next, the resulting HTML will be parsed. Parsing the resulting HTML is
handled exactly as in Recipe 7.1.
Recipe #7.3: Using Multipart Forms to Upload
The third recipe shows how to upload a file to a form, using HTTP upload. The form
allows the user to upload a file to the HTTP recipes website. You can see this page at the fol-
lowing URL:
http://www.httprecipes.com/1/7/uploadmenu.php
From this page you have two options:
• Upload a file
• Check the status of a previous upload
You should begin by uploading a file. When you upload a file, you should supply a user id
and password. This will allow you to check the status of the upload later using the second op-
tion. For security reasons, the files that you upload are immediately deleted. However, their
size and type are recorded under your user name and password. This allows you to check to
see that your upload was successful.
Search WWH ::




Custom Search