| http://www.devx.com | Printed from http://www.devx.com/Java/Article/17679/1954 |
|
Send Form Data from Java: A Painless Solution
Sending multipart/form data from Java is a painful process that bogs developers down in protocol details. This article provides a simple, real-world solution that makes sending POST requests as simple as sending GET requests, even when sending multiple files of varying type.
by
Vlad Patryshev
|
|
ending simple GET requests from Java is extremely easy (be it an application or a servlet), but sending multipart forms, like when you upload files, is painful. The former can be done in one method call that does all the underground work. The latter requires explicitly creating an HTTP request, which may include generating multipart boundaries while being careful with the exact amount and selection of newlines (e.g., println() would not always do the right job).
This article provides a simple solution that will relieve the pain of sending form data (like POST requests) from Java in most real-world scenarios. The intention is to make POST requests as simple as GET requests, even when sending multiple files of varying type (archives, XML, HTML, plain text, etc.).
GET and POST Requests
Example 1. GET Request
It performs the same task as requesting the following URL:
If you were using Mozilla 5.0, the following would be the HTTP request sent to the server:
|
| Example 2. POST Request Now, let's replace 'GET' with 'POST' in the form HTML tag:
In this case, the HTTP request sent to the server looks like this:
Example 3. Multipart POST Request
This form will produce the following HTTP request when sent from Mozilla 5:
|
| Send POST Requests from Java Using the GET request from Java is very easy. You just create a URL with the request and then get an input stream:
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:
Unfortunately, so far no such solution exists.
The Solution: ClientHttpRequest 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:
Or set them all at once, using the following:
or
The following method sets parameter as a string value or as a file depending on the argument type:
This method works the same way for cookies. One can add cookies one by one, using the following:
Or set them all at once, using one of these:
After the request is ready, the
The following group of static post methods does all of this in one fell swoop:
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
Vlad Patryshev is an R&D engineer at the Java Business Unit of Borland. He recently started the myjavatools.com project. by e-mail.
|
| DevX is a division of Jupitermedia Corporation © Copyright 2007 Jupitermedia Corporation. All Rights Reserved. Legal Notices |