Java Reference
In-Depth Information
Listing 13-1. Specifying the HTTP Method
try {
hc = (HttpConnection)Connector.open(url);
hc.setRequestMethod("DELETE");
} catch (Exception e) {…}
You can actually pass any string to setRequestMethod , but you should of course stick to
the methods defined by HTTP, which I summarize for you in Table 12-4 in Chapter 12.
Another consideration is the need some web services have for message digests , which
are secure hashes of the web request or object body from the client to the server. I discuss
how to compute message digests using various options available to Java ME developers
in Chapter 15.
One final thing you may encounter when developing a client for a web service at the
HTTP layer is a need for a cookie to save session state. This is especially true for SOA-
derived web services; often these services require the client application to log in and
present some state data provided during the login sequence during each transaction. This
is usually done by means of a cookie —a bit of data provided by the server in an HTTP
header that encapsulates a session with the server. Web clients use cookies all the time to
store state data between the client and server; your application may be called upon to do
the same thing. The full details of how HTTP can use cookies to carry session state are
described in RFC 2965 and RFC 2109. However, you should know the following facts:
• Web servers deliver cookies to your application using the Set-Cookie header.
• Cookies come in several parts: the first part is the session ID of the cookie, which is
the data you need to replay to a server in subsequent requests, and the remainder
is metadata about the cookie, such as when it expires and to which domain it
applies. Semicolons separate these parts.
• You deliver a cookie to the web server by using the Cookie header.
To look for cookies from a server, you simply use the HttpConnection 's getHeaderField
method, as shown in Listing 13-2.
Listing 13-2. Looking for Cookies from a Server
String cookie = hc.getHeaderField("Set-Cookie");
if (cookie != null) {
int semicolon = cookie.indexOf(';');
/* session is at cookie.substring(0, semicolon); */
}
 
Search WWH ::




Custom Search