Java Reference
In-Depth Information
may wish to pass data in the body of the HTTP request.) For example, suppose the following
hypothetical URL maps to a servlet or some other server-side component of your application:
http://jonathanknudsen.com/simple
Adding a parameter is easy. If you want to pass a parameter with a name of user and a
value of “jonathan”, you would use the following URL:
http://jonathanknudsen.com/simple?user=jonathan
Additional name and value pairs can be added, separated by ampersands:
http://jonathanknudsen.com/simple?user=jonathan&zip=08540&day=saturday
The HEAD operation is identical to GET, but the server sends back only the headers of the
response.
POST is basically the same as GET, but parameters are handled differently. Instead of
being pasted on the end of the URL, as they are with GET, the parameters are passed as the
body of the request. They are encoded in the same way.
Making a Connection with HTTP GET
Loading data from a server is startlingly simple, particularly if you're performing an HTTP GET.
Simply pass a URL to Connector 's static open() method. The returned Connection will probably
be an implementation of HttpConnection , but you can just treat it as an InputConnection . Then
get the corresponding InputStream and read data to your heart's content.
In code, it looks something like this:
String url = "http://jonathanknudsen.com/simple";
InputConnection ic = (InputConnection)Connector.open(url);
InputStream in = ic.openInputStream();
// Read stuff from the InputStream
ic.close();
Most of the methods involved can throw a java.io.IOException . We've omitted the try
and catch blocks from the example for clarity.
That's all there is to it. You can now connect your MIDlets to the world. Network access is
subject to security policies on the device. We'll talk more about this near the end of this chapter,
in the section “Permissions for Network Connections.”
Passing Parameters
With HTTP GET, all parameters are passed to the server in the body of the URL. This makes it
easy to send parameters to the server. The following code fragment shows how two parameters
can be passed:
String url = "http://localhost/midp/simple?pOne=one+bit&pTwo=two";
InputConnection ic = (InputConnection)Connector.open(url);
InputStream in = ic.openInputStream();
Search WWH ::




Custom Search