Java Reference
In-Depth Information
There's more...
Specifying the
location
property for
HttpRequest
can be tricky, especially when
composing URL with non-alphanumeric characters. These characters have to be encoded
using the URL-encoding MIME format. To facilitate this, JavaFX offers the
javafx.io.http.
URLConverter
class. This utility class provides several methods to convert string values into
URL-encoded format. For instance, to pull down the Wikipedia page for the Java Platform, the
following code snippet can be used:
var conv = URLConverter{};
var topic = conv.encodeString("Java_(programming_language)");
var url = "http://en.wikipedia.org/wiki/{topic}";
An instance of the
URLConverter
class is used to URL-encode string
"Java_
(programming language)"
, which gets encoded as
"Java_%28programming_
language%29"
. The non-alphanumeric characters are replaced with the
"%"
followed
by the character's hexadecimal value in the ISO Latin-1 character set (ISO-8859-1).
HTTP methods
The
HttpRequest
API supports several HTTP methods, through the property
method:String
, including GET, POST, PUT, and DELETE. The
HttpRequest
class will
behave differently, depending on the method set. For instance, GET is intended to retrieve
the specified resource from the server, while POST is intended to submit data back on the
server to be handled by the identified resource. You will see the use of both GET and POST
throughout this chapter.
For further information on some of the topics cov
ered see:
•f
HTTP—
http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol
•f
Java IO Tutorial—
http://java.sun.com/docs/books/tutorial/essential
/io/
See also
F
Saving data locally with the Storage API
Downloading images with HttpRequest
In the previous recipe,
Accessing remote data with HttpRequest
, we saw how to use the
HttpRequest
class to request and receive textual data from a remote server. How about
the next most popular resources on the web: Images? The answer is a resounding yes. In this
recipe, we will see how to pull down image binary data from the server specifically and display
it in your JavaFX application.





