In a typical sequence using the HttpConnection interface for server communication by means of the HTTP GET method, after opening an HttpConnection using Connector.open(), openInputStream() is used to get an InputStream for reading the response data from the server, and getResponseCode() is used to get the response data status code. Then InputStream and HttpConnection are closed to break the HTTP connection.
On the other hand, when using an original specification for transactions in which the response from the server is not checked, there is a tendency simply to write send data to HttpConnection and then close the HTTP connection without requesting any response-related information. An example of such a program is shown below.
HttpConnection hc; OutputStream os; byte[] data; . . . // Set send data in data parameter hc = (HttpConnection)Connector.open( "http://..." ); // Get HttpConnection os = hc.openOutputStream(); // Get OutputStream os.write( data ); // Write send data to OutputStream os.close(); // Close OutputStream // (No information is requested here about the result) hc.close(); // Close HttpConnection
In the example above, send data is simply written to OutputStream, then OutputStream and HttpConnection are closed. In this sequence there is no guarantee that HttpConnection actually performed any HTTP communication. In HTTP communication, even if response data is not received, the getResponseCode() method should still be used to confirm the response from the server, just as in ordinary HTTP communication processing. An example is given below.
HttpConnection hc; OutputStream os; byte[] data; int rc; // HTTP response code . . . // Set send data in data parameter hc = (HttpConnection)Connector.open( "http://..." ); // Get HttpConnection os = hc.openOutputStream(); // Get OutputStream os.write( data ); // Write send data to OutputStream os.close(); // Close OutputStream rc = hc.getResponseCode() // Get the response code. Here HTTP communication takes place. hc.close(); // Close HttpConnection