In most cases, servers that process POST requests won't reject your GET request. Still, sometimes you'll have to POST instead of GET, particularly when you upload a file. Wouldn't having a solution like the form from Example 2 be nice? The corresponding Java request could look like this:
InputStream serverInput = ClientHttpRequest.post(
new java.net.URL("hi.iq/register"),
new Object[] {
"name", "J.Doe",
"email", "abuse@spamcop.com",
"test.txt", new
File("C:\home\vp\tmp\test.txt")
});
Unfortunately, so far no such solution exists.
The Solution: ClientHttpRequest
If you search the Internet, you will find some partial or complicated solutions for using POST requests in Java. The best commercial solution probably is JScape's HTTP(S) component. Its well-designed API covers everything specified in HTTP (see RFC 2068). Other solutions are either too weird and complicated (see
Ronald Tschalär's HttpClient) or too literal to be useful (check out this post from JavaRanch).
Because of this lack of free, simple solutions, I had to develop my own (download the source code). It is of moderate complexity and has an obvious interface that I hope is both simple and relatively universal. It can be instantiated from a URL String, URL, or an already open URLConnection.
After a user creates an instance of ClientHttpRequest, the user can add request
parameters and cookies. Cookies are an important part of a request, especially
with servlets that use cookies to keep track of sessions.
One can add parameters one by one, using the following:
setParameter(String name, String value);
setParameter(String name, File file);
setParameter(String parametername, String filename, InputStream fileinput)
Or set them all at once, using the following:
setParameters(Map parameterMap)
or
setParameters(Object[] parameterArray).
The following method sets parameter as a string value or as a file depending on the argument type:
setParameter(String parametername, Object parameterdata).
This method works the same way for cookies. One can add cookies one by one, using the following:
setCookie(String name, String value)
Or set them all at once, using one of these:
setCookies(Map cookieMap)
setCookies(String[] cookieArray)
After the request is ready, the post() method posts the request and returns an input stream that will contain the server's response, the same way as in
URL.getInputStream().
For convenience, additional post methods are available:
post(Map parameters);
post(Object[] parameters);
post(String[] cookies, Object[] parameters);
post(Map cookies, Map parameters).
The following group of static post methods does all of this in one fell swoop:
InputStream serverInput = post(URL url, Map parameters);
InputStream serverInput = post(URL url, Map cookies, Map parameters);
InputStream serverInput = post(URL url, String[] cookies, Object[] parameters);
InputStream serverInput = post(URL url, Object[] parameters).
All these methods let the user post a complicated form and receive an input stream in one expression, like the one in Example 2 at the beginning of this article.
Answer to the Eternal Question
Now you know how an HTML form (GET or POST) gets passed to the server as an HTTP request and how you can reproduce this behavior in your Java programwithout overloading it with protocol details. You could say this solution answers the eternal question: How can one call a servlet or JSP from another servlet or JSP?