Java Reference
In-Depth Information
Recipe #7.2: Using HTTP POST Forms
The second recipe shows how to access an HTTP POST form. The form allows the user
to search the US states and capitals, in the same way as Recipe 7.1. You can see this page at
the following URL:
http://www.httprecipes.com/1/7/post.php
This page displays a simple form that allows the user to enter a search string and select
if it is searching states or capitals. You can see this recipe in Listing 7.3.
Listing 7.3: Using HTTP POST (FormPost.java)
package com.heatonresearch.httprecipes.ch7.recipe2;
import java.io.*;
import java.net.*;
import com.heatonresearch.httprecipes.html.*;
public class FormPOST
{
/** Advance to the specified HTML tag.
* @param parse The HTML parse object to use.
* @param tag The HTML tag.
* @param count How many tags like this to find.
* @return True if found, false otherwise.
* @throws IOException If an exception occurs while reading.
*/
private boolean advance(ParseHTML parse, String tag, int count)
throws IOException
{
int ch;
while ((ch = parse.read()) != -1)
{
if (ch == 0)
{
if (parse.getTag().getName().equalsIgnoreCase(tag))
{
count--;
if (count <= 0)
return true;
}
}
}
return false;
Search WWH ::




Custom Search