Java Reference
In-Depth Information
•
Server sockets
•
Secure sockets
•
Datagrams
•
Serial port access.
In the remainder of this chapter, we focus on the networking protocols
from the GCF. The local connectivity protocol (serial port access) is similar
in nature to the network protocols, but its connection string depends on
the target handset model, therefore you must study it separately.
2.6.1 HTTP and HTTPS Support
To open an HTTP connection we use the
Connector.open()
method
with a URL of the form
www.myserver.com
. Code to open an
Http-
Connection
and obtain an
InputStream
would look something like
this:
try
{
String url = "www.myserver.com";
HttpConnection conn = (HttpConnection)Connector.open(url);
InputStream is = conn.openInputStream();
...
conn.close()
}
catch(IOException ioe)
{
...
}
Under the MIDP security model, untrusted MIDlets can open an HTTP
connection only with explicit user confirmation. Signed MIDlets that
require access to an HTTP connection must explicitly request permission:
MIDlet-Permissions: javax.microedition.io.Connector.http, ...
Opening an HTTPS connection follows the same pattern as a normal
HTTP connection, with the exception that we pass in a connection
URL of the form
https://www.mysecureserver.com
and cast the returned
instance to an
HttpsConnection
object, as in the following example:
try
{
String url = "https://www.mysecureserver.com";
HttpsConnection hc = (HttpsConnection)Connector.open(url);
InputStream is = hc.openInputStream();
...
hc.close()