createHttpRequest()
and all the helper class methods return a RestResult
object that allows access to various parts of the response. The most interesting and useful method on the RestResult is readInputStream()
. This is a convenience method which will return the response data into a String
public String readInputStream() throws IOException
After calling this method the String object which is returned will contain the JSON or XML with the content which was requested. The REST module uses the org.json.jar library
and the dom4j
XML library internally. You can also use these libraries, or comparable libraries, in your applications.
If the response data is large, you can access the input stream directly by calling getInputStream()
on the RestResult
.
Other useful methods on the RestResult
object are getResponseCode()
and getResponseMessage()
. These return the response’s response code and message, respectively. For more information, see HTTP Status Codes.
The Java client library uses the java.net.HttpURLConnection
class to communicate with servers. To access any functionality that the RestResult
does not expose, call the getConnection()
method to return the underlying HttpURLConnection
object.
When you are finished with the RestResult
object, it is good practice to call the close()
method. This releases any resources that the HttpURLConnection
might hold. If the connection is not closed, the next request to the server will close the HttpURLConnection
.